Certain point is to my right or to my left?

I’ve been trying to set this AI for a while so I figured I’d asked this question.

I want to know if my vehicle needs to turn left (player2.steer(-.5f):wink: or right (player2.steer(-.5f);).

right now Im using this

[java] slope = (rankersArray[1].getWorldTranslation().getZ() - player2.getPhysicsLocation().getZ())
/ (rankersArray[1].getWorldTranslation().getX() - player2.getPhysicsLocation().getX());

        if (slope > 0) 
            steeringValue2 = .5f;
        else if (slope < 0) 
            steeringValue2 = -.5f;


which only works if the car is facing the destination point and is not driving away.

Also, this makes the car face the destination point:

[java] // rotate to face
Quaternion rotTo = player2.getPhysicsRotation().clone();
rotTo.lookAt(new Vector3f(-rankersArray[14].getWorldTranslation().getX(),-rankersArray[14].getWorldTranslation().getY(),-rankersArray[14].getWorldTranslation().getZ()), Vector3f.UNIT_Y);


but it doesn’t allow the car to naturally steer over time, its just fixated looking at the destination point.

So, how do I know if I need to turn right or left in order to face the destination point?

// Find the vector from us to the target
Vector3f targetToUs = target.getWorldTranslation().subtract( us.getWorldTranslation() );

// Dot that with the “left hand” vector in our space
float leftOrRight = targetToUs.dot( us.getWorldRotation().mult( Vector3f.UNIT_X );

If leftOrRight is positive, turn left. It’s negative, turn right.

Edit: adjust rotation as necessary for physics or whatever… that was for raw spatials but I assume it still works.


Thanks @pspeed, it works like a charm. Works so well it makes me wanna cry.;.;

Your code snippet sure is more optimized than my snippet which required lots of checks and balances.

1 Like

dot products are magic from the ancient days of the first Doom engine.

…worth knowing them.