5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ I assume you want to get the colliding face's normal so you can add a bullet hit or some other effect, right? $\endgroup$
    – David
    Commented Sep 29, 2015 at 16:12
  • $\begingroup$ @David♦ Yes, that is correct. $\endgroup$
    – blackhole
    Commented Sep 30, 2015 at 1:11
  • $\begingroup$ ..."according to an object's face normals" would be a more accurate title. If I understand what you're after. $\endgroup$
    – Mentalist
    Commented Sep 30, 2015 at 3:42

1 Answer 1

6
+50
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Monster
    Commented Oct 6, 2015 at 8:48
  • $\begingroup$ @Monster thanks for pointing me to that function, much simpler. edited my answer. $\endgroup$
    – David
    Commented Oct 6, 2015 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .