Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Criminal Enterprises
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

*DO NOT* SHARE MEDIA OR LINKS TO LEAKED COPYRIGHTED MATERIAL. Discussion is allowed.

Vector Math: Velocity based Rotation


InfamousSabre
 Share

Recommended Posts

InfamousSabre

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 by InfamousSabre
Link to comment
Share on other sites

InfamousSabre

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

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

InfamousSabre

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 by InfamousSabre
Link to comment
Share on other sites

Calling it solved then. If it needs to be reopened, PM me.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.