Author Topic: Get Relative Velocity  (Read 1771 times)

I figured it out!

Code: [Select]
function ShapeBase::getForwardVelocity(%this) //the ShapeBase class contains all physical objects
{
%forwardVel = vectorDot(%this.getVelocity(),%this.getForwardVector());
return %forwardVel;
}


Here is code to get the relative velocity vector.

Code: [Select]
function relativeVelocity(%dirVec, %velVec)
{
%pi = 4 * mAtan(1, 1);
//get the magnitude of the horizontal componenet of the velocity
%horMag = VectorLen(getWords(%velVec, 0, 1) SPC 0);

//the angle the velocity is being calculated from
%dirAng = mATan(getWord(%dirVec, 1), getWord(%dirVec, 0));

//the angle of the velocity with respect to the world
%velAng = mATan(getWord(%velVec, 1), getWord(%velVec, 0));

//get the relative angle of the velocity
%relAng = %velAng - %dirAng + %pi/2;

//Get the cartesian (x, y) values from the angle, scale it by the horizontal magnitude of the velocity, add the vertical component
%relVec = VectorAdd(VectorScale(mCos(%relAng) SPC mSin(%relAng) SPC 0, %horMag), "0 0" SPC getWord(%velVec, 2));
}

Edit: Made a small fix to the method.
« Last Edit: February 25, 2013, 02:04:37 AM by Nitramtj »

What would a relative velocity vector be
« Last Edit: February 23, 2013, 07:37:48 PM by Zeblote »

A relative velocity vector is normally one velocity relative to another, which is a concept you would learn in a physics class. In this thread, it's basically converting a vector from one coordinate system (the world) to another (the player's.) It's basically rotating the vector.


function Player::getRelativeVelocity(%this)
{
   %xyz = %this.getVelocity();
   %x = getWord(%xyz,0);
   %y = getWord(%xyz,1);
   %z = getWord(%xyz,2);
   %forwardVector = %this.getForwardVector();
   %forwardX = getWord(%forwardVector,0);
   %forwardY = getWord(%forwardVector,1);
   return(-mround((%x * -%forwardY + %y * %forwardX)*1000)/1000 SPC mround((%y * %forwardY - %x * -%forwardX)*1000)/1000 SPC mround((%z*1000)/1000));
}

it works for me, idk about you
also, without mround, if check it, x will be like -2.6e-005 if you aren't moving

wait, crap :C

I figured it out!

Code: [Select]
function ShapeBase::getForwardVelocity(%this) //the ShapeBase class contains all physical objects
{
%forwardVel = vectorDot(%this.getVelocity(),%this.getForwardVector());
return %forwardVel;
}

:C why
« Last Edit: February 23, 2013, 10:32:12 PM by MARBLE MAN »

A relative velocity vector is normally one velocity relative to another, which is a concept you would learn in a physics class. In this thread, it's basically converting a vector from one coordinate system (the world) to another (the player's.) It's basically rotating the vector.
I copied it in, but im very confused atm how to use it. srry im sick also

Here is code to get the relative velocity vector.

Code: [Select]
function relativeVelocity(%dirVec, %velVec)
{
%pi = 4 * mAtan(1, 1);
//get the magnitude of the horizontal componenet of the velocity
%horMag = VectorLen(getWords(%velVec, 0, 1) SPC 0);

//the angle the velocity is being calculated from
%dirAng = mATan(getWord(%dirVec, 1), getWord(%dirVec, 0));

//the angle of the velocity with respect to the world
%velAng = mATan(getWord(%velVec, 1), getWord(%velVec, 0));

//get the relative angle of the velocity
%relAng = %velAng - %dirAng + %pi/2;

//Get the cartesian (x, y) values from the angle, scale it by the horizontal magnitude of the velocity, add the vertical component
%relVec = VectorAdd(VectorScale(mCos(%relAng) SPC mSin(%relAng) SPC 0, %horMag), "0 0" SPC getWord(%velVec, 2));
}

Edit: Made a small fix to the method.
Yes, this would be the more complicated trig method I had mentioned. If both methods work equally well, I would advise you to use greek's because it takes less computation

Done. What I'm looking to do now is rotate the player 90°, relative to the direction he/she is facing. I'll make a new topic.