5

I have an empty that is at the center of the screen, it move to whatever you point at. An explanation on how to do so can be found here.

I am looking to have a plane or empty always face outwards of whatever object it is colliding with. If this can't be done with an empty, I would be fine with a plane.

Here's a picture of what I don't want: (like this but in-game)

pic1

Here's a picture of what I do want: (like this but in-game, used wireframe as you cannot see in textured) See the plane is rotated to match the cube's face.

pic2

I am basically looking for the empty/plane (preferably empty) to face the outside of whatever it is colliding with.

blackhole
  • 2,390
  • 4
  • 26
  • 64

1 Answers1

6

logic bricks
Set up the logic bricks as pictured above.

demo Cone object, showing its axes

This script will move an object (the cone) to the location of ray.hitPosition and then rotate it to point along the ray.hitNormal using alignAxisToVect().
For this example, the cone is pointing along the positive z axis. If it was along a different axis, I would have to pass in the second parameter to alignAxisToVect().

import bge

cont = bge.logic.getCurrentController() scene = bge.logic.getCurrentScene()

ray = cont.sensors["Ray"] cone = scene.objects["Cone"]

if ray.hitObject and cone is not ray.hitObject: cone.worldPosition = ray.hitPosition cone.alignAxisToVect(ray.hitNormal)

Example gif, showing the cone moving and rotating to match the underlying geometry in the game engine.

animated gif

Blend File

David
  • 49,291
  • 38
  • 159
  • 317
  • There is no need to arctan anything. You can use KX_GameObject.alignAxisToVect(ray.hitNormal) which aligns the object's z-axis along the normal. – Monster Oct 06 '15 at 08:48
  • @Monster thanks for pointing me to that function, much simpler. edited my answer. – David Oct 06 '15 at 21:02