Game Development Community

Problem Rotating Vector (or matrix)

by Vinh Tran · in Torque Game Engine · 08/16/2005 (8:50 pm) · 4 replies

I have a function that allows a player to throw a disc or frisbee projectile. The engine has been modified with realistic frisbee physics.

In the script I have the following in my disc.cs projectile file:

//-----------------------------------------------------------------------------
// Function: ThrowDisc
// This function throws the disc by creating a projectile and shooting it.
//-----------------------------------------------------------------------------
function DiscImage::ThrowDisc(%this, %obj, %slot)
{
    echo("Throwing Disc Using Projectile");
    %projectile = %this.projectile;

    %eye = %obj.getEyeVector();   
    %vector = vectorScale(%eye, %this.disc_power);
    echo("Aim Vector" @ %vector);

    // Get the Directional Vectors
    %aimvec = %obj.getEyeTransform();
    %aa = getWords(%aimvec, 3, 6);
    %tmat = VectorOrthoBasis(%aa);

    // Get up/forward/right vectors
    %uv = getWords(%tmat, 6, 8);
    %rv = getWords(%tmat, 0, 2);
    %fv = getWords(%tmat, 3, 5);
   
    // Get our players velocity. We must ensure that the players velocity is added   
    // onto the projectile   
    %vector = vectorAdd(%vector,%obj.getVelocity());

   // Create the projectile object
   %p = new (%this.projectileType)() {
      dataBlock        = %projectile;
      initialVelocity  = %vector;
      initialPosition  = getBoxCenter(%obj.getWorldBox());
      initialUpVector      = %uv;
      initialForwardVector = %fv;
      initialRightVector   = %rv;
      initialPitch     = 0;
      initialRoll      = 0;
      initialSpin      = %this.disc_power;
      initialSpinVel   = %this.disc_power;
      sourceObject     = %obj;
      sourceSlot       = %slot;
      client           = %obj.client;
   };
   MissionCleanup.add(%p);
   return %p;
}

As you can see I've added some properties to the creation of a projectile, namely the Up and Forward vectors of the disc. These vectors are used in the rendering and the flight of the disc. If you think about a disc flying it has an Up vector that is perpendicular to the plane of the disc and a forward vector on the plane of the disc in the direction of its initial velocity.

Here is my problem, I want to add in the ability to curve the disc on the throw. What this means is I need to take my Up vector and rotate it along the Forward vector to allow the disc to curve left or right. So far I've just tried to hardcode a 15degree rotation into the Up vector.

I've noticed the following in my tests during the game. When i'm standing in one direction and throw the disc with a 15degree positive rotation it will curve to the right (good). When i turn the player around 180 degrees and throw the disc it is now curving to the left (bad). When I turn 90degrees from the initial position the disc goes straight (bad). It seems like i'm rotating with respect to world coordinates or something . I'm pretty new to torque so i'm still learning how this works.

I've spent the last 2 days searching the forums but I have not come up with much.

I've tried multiplying the vector by a rotation matrix and i've also tried taking my initial transform and creating a quat using the forward vector and an angle of rotation. I take this and multiply it by the original transform then use that matrix to find up/forward vectors, however I still get the same results...

Thanks!

#1
08/16/2005 (9:01 pm)
Someone just threw up some good vector rotation code like the other day. Let me find it!
#3
08/16/2005 (9:13 pm)
Hi jeremy,

I saw that the other day and gave it a try. I tried using this to rotate my Upvector by the Y or X axis, however my disc always went straight into the ground. Then I realized that I don't want to rotate by a defined axis, such as the X or Y because the disc could already be angled up into the air. I would need to rotate my Up vector around the Forward Vector which will be perpendicular to it...
#4
08/16/2005 (9:16 pm)
Also, at the most basic level what I want to do is take the eye transform of the player, and rotate it along its forward vector to the right or left. I guess this would be considered Roll. Then I should be able to extract an Up/Forward/Right vector from this new transorm, yea?