InfamousSabre Posted June 23, 2014 Share Posted June 23, 2014 (edited) I'm currently trying to get a model to face the direction it is moving.I've tried this: Obj.Rotation = Quaternion.RotationMatrix(Matrix.LookAtRH(Vector3.Zero, Obj.Velocity, Vector3.WorldUp)).ToRotation(); But it isn't working quite right. Probably because the Matrix given is not a Rotation Matrix.Anyone know how to do this? Edited June 23, 2014 by InfamousSabre Link to comment Share on other sites More sharing options...
InfamousSabre Posted June 23, 2014 Author Share Posted June 23, 2014 I'm not sure what you are asking for I have an object moving through the air, and I want to rotate it to face the direction its moving. Link to comment Share on other sites More sharing options...
Jitnaught Posted June 23, 2014 Share Posted June 23, 2014 I'm not sure what you are asking for but you can try this if (!Exists(obj)) { myObject = World.CreateObject("your model", (Player.Character.Position + Player.Character.Direction)); if (Exists(obj)) { myObject.AttachToPed(Player.Character, Bone.YourBone, new Vector3(0.2, 0.02, 0.07), new Vector3(0, Helper.DegreeToRadian(49), 0)); }} You can try adjust the offset at each Vector 3 section What is this? He's trying to get the model to face the direction it is moving, not attach an object to the player. Link to comment Share on other sites More sharing options...
InfamousSabre Posted June 23, 2014 Author Share Posted June 23, 2014 (edited) Annnnd heres the answer! Obj.RotationQuaternion = Quaternion.Invert(GenerateRotationFromDirectionVector(Vector3.Normalize(Obj.Velocity))); public Quaternion GenerateRotationFromDirectionVector(Vector3 vDirection) //Use Vector3.Normalize(Obj.Velocity) as your Vector3 parameter! { // Step 1. Setup basis vectors describing the rotation given the input vector and assuming an initial up direction of (0, 1, 0) Vector3 vUp = new Vector3(0, 1.0f, 0); // Y Up vector Vector3 vRight = Vector3.Cross(vUp, vDirection); // The perpendicular vector to Up and Direction vUp = Vector3.Cross(vDirection, vRight); // The actual up vector given the direction and the right vector // Step 2. Put the three vectors into the matrix to bulid a basis rotation matrix // This step isnt necessary, but im adding it because often you would want to convert from matricies to quaternions instead of vectors to quaternions // If you want to skip this step, you can use the vector values directly in the quaternion setup below Matrix mBasis = new Matrix(); mBasis.M11 = vRight.X; mBasis.M12 = vRight.Y; mBasis.M13 = vRight.Z; mBasis.M14 = 0.0f; mBasis.M21 = vUp.X; mBasis.M22 = vUp.Y; mBasis.M23 = vUp.Z; mBasis.M24 = 0.0f; mBasis.M31 = vDirection.X; mBasis.M32 = vDirection.Y; mBasis.M33 = vDirection.Z; mBasis.M34 = 0.0f; mBasis.M41 = 0.0f; mBasis.M42 = 0.0f; mBasis.M43 = 0.0f; mBasis.M44 = 1.0f; // Step 3. Build a quaternion from the matrix Quaternion qrot = new Quaternion(); qrot.W = (float)Math.Sqrt(1.0f + mBasis.M11 + mBasis.M22 + mBasis.M33) / 2.0f; double dfWScale = qrot.W * 4.0; qrot.X = (float)((mBasis.M32 - mBasis.M23) / dfWScale); qrot.Y = (float)((mBasis.M13 - mBasis.M31) / dfWScale); qrot.Z = (float)((mBasis.M21 - mBasis.M12) / dfWScale); return qrot; } Edited June 23, 2014 by InfamousSabre Link to comment Share on other sites More sharing options...
Silent Posted June 23, 2014 Share Posted June 23, 2014 Calling it solved then. If it needs to be reopened, PM me. Link to comment Share on other sites More sharing options...
Recommended Posts