Note that there are some explanatory texts on larger screens.

plurals
  1. POFully Rotate a Quaternion to a Vector
    primarykey
    data
    text
    <p>I have two objects (target and player), both have Position (Vector3) and Rotation (Quaternion). I want the target to rotate and be facing right at the player. The target, when it shoots something should shoot right at the player. </p> <p>I've seen plenty of examples of slerping to the player, but I don't want incremental rotation, well, I suppose that would be ok as long as I can make the slerp be 100%, and as long as it actually worked.</p> <p>FYI - Im able to use the position and rotation to do plenty of other things and it all works great except this last piece I cant figure out.</p> <p><strong>EDIT</strong></p> <p>Code samples run in the Target's class, Position = the targets position, Avatar = the player.</p> <p>Using the value of 1 for the Slerp isn't work. This code below rotates some, but I think something is way off becuase when it's drawn the target scales up and then down as the player gets closer.</p> <pre><code>var A = new Vector3(Position.X, Position.Y, Position.Z); var B = new Vector3(GameState.Avatar.Position.X, GameState.Avatar.Position.Y, GameState.Avatar.Position.Z); A.Normalize(); B.Normalize(); var angle = Math.Acos(Vector3.Dot(A, B)); var axis = Vector3.Normalize(Vector3.Cross(A, B)); var rotOnAngle = new Quaternion(axis, (float)angle); var newRot = Quaternion.Slerp(Quaternion.Identity, rotOnAngle, 1f); Rotation = newRot; Cannon.Shoot(Position, Rotation, this); </code></pre> <p>I tried using this and it doesn't quite work either...the target does rotate, but not to face the player. But at least the scaling problem goes away.</p> <pre><code>Quaternion q = new Quaternion(); var pos = Vector3.Normalize(Position); var pos2 = Vector3.Normalize(GameState.Avatar.Position);% var a = Vector3.Cross(Position, GameState.Avatar.Position); q.X = a.X; q.Y = a.Y; q.Z = a.Z; q.W = (float)Math.Sqrt(((int)Position.Length() ^ 2) * ((int)GameState.Avatar.Position.Length() ^ 2)) + Vector3.Dot(Position, GameState.Avatar.Position); q.Normalize(); Rotation = q; Cannon.Shoot(Position, Rotation, this); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload