• Advertisement
Sign in to follow this  

Turn to heading w/ dot product

This topic is 4152 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a vehicle with a given position, facing, and max turn rate (radians/tick). I want to calculate how much to increment or decrement the facing each tick so that the vehicle will turn to face a given target. The following code yields the angle between the vehicle's facing and the direction to the target.
# calculate direction from vehicle to target
toTarget = targetPos - vehiclePos
toTarget.Normalize()

# get the angle (in radians) between the vehicle facing vector and toTarget vector
facingTargetOffset = math.acos( vehicleFacing.DotProduct( toTarget ) )

Unfortunately, it doesn't give me a direction, so I don't know if I should increase or decrease the vehicle's facing. The target could be to the left or to the right of the vehicle's facing, but facingTargetOffset will be the same in either case. Thoughts?

Share this post


Link to post
Share on other sites
Advertisement
You can either use cross product to figure out which side of the facing direction the target is on (i.e the sign will differ, depending on the side), or you can just use atan2(x,y). Remember SOHCAHTOA.

Share this post


Link to post
Share on other sites
The cross product method seems to work. I'm guessing it is faster than calculating the arctangent, so I'll stick with it.

Thanks!

- Mike

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement