xna - Rotate the least amount with two vectors -


i've gone through lot of questions online last few hours trying figure out how calculate rotation between 2 vectors, using least distance. also, difference between using atan2 , using dot product? see them often.

the purpose rotate in y axis only based on differences in x/z position (think top down rotation in 3d world).

  1. option 1: use atan2. seems work well, except when switchs radians positive negative.

        float radians = (float)math.atan2(source.position.x - target.position.x, source.position.z - target.position.z);      npc.modelposition.rotation = new vector3(0,         npc.modelposition.rotation.y + (radians),          0); 

this works fine, except @ 1 point starts jittering swings other way.

  1. use dot product. can't seem make work well, turns in same direction - possible swing in least direction? understand ranges 0 pi - can use that?

        float dot = vector3.dot(vector3.normalize(source.position), vector3.normalize(target.position));     float angle = (float)math.acos(dot);      npc.modelposition.rotation = new vector3(0,          npc.modelposition.rotation.y + (angle),          0); 

thanks, james

you calculate 2 different angles:

visualization

angle alpha angle calculated atan2 (which equals opposite side / adjacent side). angle beta 1 calculated dot product.

you can achieve similar results (as atan) dot product with

angle = acos(dot(normalize(target - source), -z)) 

(-z unit vector in negative z direction)

however, lose sign.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -