Author Topic: Adding relative velocity (vectors)  (Read 1443 times)

I am quite stuck with some problem and i have searched everywhere but i am getting pretty confused on how to do this:
I have a vector but i want it to go the way of the other vector.
Even more specific: Vector for speed needs to be altered to go into direction of upVector.

I have tried to figure something out by looking at the code of Event_addRelativeVelocity but it didn't really help. :C
I also looked at the axisToEuler and EulerToAxis things but i can't really think of a way to do this.
It has been breaking my head a long time.

Can anyone please help me on my way?
« Last Edit: October 07, 2011, 11:35:37 AM by lordician »

I'm not entirely sure what you're trying to do here, but if you're doing what I think you're trying to do...
Code: [Select]
function Player::AddZVelocity(%obj, %amt)
{
%vel = setWord(%obj.getVelocity(), 2, getWord(%vel, 2) + %amt);
%obj.setVelocity(%vel);
}

I'm not entirely sure what you're trying to do here, but if you're doing what I think you're trying to do...
Code: [Select]
function Player::AddZVelocity(%obj, %amt)
{
%vel = setWord(%obj.getVelocity(), 2, getWord(%vel, 2) + %amt);
%obj.setVelocity(%vel);
}
Sorry, it must've been the time.

Thanks, but it is nothing what i need. :P

At the moment i have one vector being the static velocity upwards ("0 0 6" for example) but i want to have it applied 'dynamically'.
For example, a vehicle would instead of just go up no matter what go up in it's upvector.
So if it is angled, it will add velocity at that angle.

Clockturn made a script that does this for me because I was doing something similar and completely baffled. Here you go:
Code: [Select]
//by clockturn
function Player::getRelativePosition(%player,%vec,%slot)
{
if(getWordCount(%vec) != 3 || !isObject(%player))
{
return;
}
// X is left/right
// Y is forward/back
// Z is up/down
%x = getWord(%vec,0);
%y = getWord(%vec,1);
%z = getWord(%vec,2);

%scale = %player.getScale();
%sx = getWord(%scale,0);
%sy = getWord(%scale,1);
%sz = getWord(%scale,2);

%x *= %sx;
%y *= %sy;
%z *= %sz;

if(%slot $= "eye")
{
%yv = vectorNormalize(%player.getEyeVector());
} else {
%yv = vectorNormalize(%player.getForwardVector());
}
%zv = vectorNormalize(%player.getUpVector());
%xv = vectorNormalize(vectorCross(%yv,%zv));

if(%slot $= "eye")
{
%pos = %player.getEyePoint();
} else if(%slot !$= "") {
%pos = getWords(%player.getSlotTransform(%slot),0,2);
} else {
%pos = %player.getHackPosition();
}

%pos = vectorAdd(%pos,vectorScale(%xv,%x));
%pos = vectorAdd(%pos,vectorScale(%yv,%y));
%pos = vectorAdd(%pos,vectorScale(%zv,%z));

return %pos;
}
function Player::getRelativeVector(%player,%vec,%slot)
{
%pos2 = %player.getRelativePosition(%vec,%slot);
if(%slot $= "eye")
{
%pos = %player.getEyePoint();
} else if(%slot !$= "") {
%pos = getWords(%player.getSlotTransform(%slot),0,2);
} else {
%pos = %player.getHackPosition();
}
return vectorNormalize(vectorSub(%pos2,%pos));
}
So for the example you just said, you would use %obj.addVelocity(%obj.getRelativeVector("0 0 6"));.
« Last Edit: October 08, 2011, 05:51:52 PM by Amade »

Thanks for giving me this example.
Working it out without a good example is brain damaging. :P

Hmm, Amade's example didn't really help after all.
Amade, could you explain what this was for?
Maybe i didn't understand the script right but so far i got that it got like the vector that is like the direction between 2 slots.

Another time,
I have 2 vectors.
Vec1: "0 0 6" being the velocity i want to add
Vec2: "1 5 7" being the direction i want the velocity to be added.
How to?

The script was made with players in mind. For example, if you wanted to move a player forward (relative to the direction the player is facing) with it you would use a vector scaled from the vector "0 1 0", though a simpler way to do that would be to scale the eye vector.

The script by Clockturn is made to convert an absolute vector to one relative to the location of a slot on a player, which is assumed to be the eye node if no node is specified.

I'm still not understanding what you're trying to do, though.

The script was made with players in mind. For example, if you wanted to move a player forward (relative to the direction the player is facing) with it you would use a vector scaled from the vector "0 1 0", though a simpler way to do that would be to scale the eye vector.

The script by Clockturn is made to convert an absolute vector to one relative to the location of a slot on a player, which is assumed to be the eye node if no node is specified.

I'm still not understanding what you're trying to do, though.
Vehicle goes up no matter how it is angled.
So even if the vehicle is upside down it will still go up if i add the vector "0 0 6".
But i want the vehicle to get this 6 upward velocity for what it's upward is.
So like, if that thing is upside down, he will move downwards because he is upside down.
Better?

Vehicle goes up no matter how it is angled.
So even if the vehicle is upside down it will still go up if i add the vector "0 0 6".
But i want the vehicle to get this 6 upward velocity for what it's upward is.
So like, if that thing is upside down, he will move downwards because he is upside down.
Better?
Much better, yes. In that case you would just modify the coding from Clockturn I posted to work with vehicles. If you don't feel like modifying you can do a slightly hacky thing to get around its being defined for players:
Code: [Select]
Player::getRelativeVector(%vehicle, "0 0 6", "eye");