# Turn to heading w/ dot product

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

## 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 on other sites
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 on other sites
I'll try the cross product. Stand by...

##### 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

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632167
• Total Posts
3004533

×