Working from the XNA aiming sample and trying to convert it to 3d...
I have a rotating gun turret that I'm trying to turn to point at a moving ship, but it's not quite working as I expected it to. I'm taking the angle between two Vector3s (representing the turret and ship positions) along each of the 3 axes, create a rotation matrix and generate a quaternion that I can apply to the turret to point it at the ship. Two questions:
What am I doing wrong?
Is there an easier way to do this?
position = turret position
aimAtThis = ship position
Thanks.Code:Quaternion Target3D(Vector3 position, Vector3 aimAtThis) { float x = aimAtThis.X - position.X; float y = aimAtThis.Y - position.Y; float z = aimAtThis.Z - position.Z; float xAngle = WrapAngle((float)Math.Atan2(z, y)); float yAngle = WrapAngle((float)Math.Atan2(z, x)); float zAngle = WrapAngle((float)Math.Atan2(y, x)); Matrix m = Matrix.CreateRotationX(xAngle) * Matrix.CreateRotationY(yAngle) * Matrix.CreateRotationZ(zAngle); Quaternion q = Quaternion.CreateFromRotationMatrix(m); return q; } float WrapAngle(float radians) { while (radians < -MathHelper.Pi) { radians += MathHelper.TwoPi; } while (radians > MathHelper.Pi) { radians -= MathHelper.TwoPi; } return radians; }




Reply With Quote