Getting Velocity relative to a Body

Hi,

Can somone tell my how to get velocity relative to my ship? That is the velocity as if my ship were at (0,0,0).

Thanks

Say you move at 50 units per second along the x-axis… chasing another entity running along the x-axis with a speed of 48ups. Then the relative (to you) speed (along the x-axis) of that entity is -2 ups. If it were moving at 60ups the relative speed would be 10ups.

Thats all there is to it.

No thats not reall what I am talking about. I’ll explane. My body has a velocity of (x, y ,z) where x, y, z are the change in position relative to the origin(0,0,0), what I want is my body’s velocity relative to itself not the origin. So to give you an example say my body is facing down the X axis and is traveling at 1 unit per second my velocity will be (0,0,1) now lets say I turn my ship 180 degress so its going straight down the X axis in the other direction my velocity is now (0,0,-1) that is relative to the origin. The velocity in both cases relative to my body is (0,0,1) because I am moving forwards in both cases. So does anyone know a calculation that can convert a velocity so its relative to my body no the origin?

What about this?


Vector3f velocity; //the velocity of your ship in world coordinates (rotation)
Matrix3f rotation; //the orientation of your ship 
rotation.invert();
shipVel = rotation.mul(velocity); //shipVel is the velocity of your ship relative to the direction the ship is pointing at

You make little sense to me. Just save your ships position from one step back, then compare to its current position, using your time step you can calculate the speed or what ever you want with it. Relative speed to itself… are you going all ‘theory of relativity’ on us? :slight_smile:

Thanks arne thats exactly what I was looking for :).

The problem I have is I’m not used to the way javax.vecmath does rotation. I’m my program I mainly deal with 4x4 matrix and euler values. I’m sure I get used to it as I use it more.

[quote] Relative speed to itself… are you going all ‘theory of relativity’ on us?
[/quote]
lol :slight_smile: no I just want to know my speed, I think if the speedo in a car read your speed relative to the origin rather than relative to your car you would have problems.

lol :slight_smile: no I just want to know my speed, I think if the speedo in a car read your speed relative to the origin rather than relative to your car you would have problems.
[/quote]
aaah, I think I get it.

but in a car I would expect the world coordinate speed vektor to be aligned with my car’s orientation (momentarily), if not, then I’m sliding a lot and my forward speed would be the least of my worries.

my suggestion would be:

Vector3f worldVelocity;
Vector3f carForward; // - normalized!
Transform3D carTransform;
float forwardVelocity;

carTransform.transform( carForward );
forwardVelocity = worldVelocity.dot( carForward );

arne I have one question about your code example. rotation.mul(velocity) does not seem to work mul does not accept a Vector3f, it looks like it only takes another Matrix3f, do you think you could help me fix this please?

[quote]but in a car I would expect the world coordinate speed vektor to be aligned with my car’s orientation (momentarily), if not, then I’m sliding a lot and my forward speed would be the least of my worries.
[/quote]
Yeah that sliding is exactly what I am trying to counter act. I dont think setting a velocity will work to well as it will counter collisions so I will go straight through things I shouldn’t. I am applying a force to my body not a velocity.

Currently I do this:


Vector3f relForce = getRelForce(speedForce); //get the force to apply to the body relative to the world
Vector3f  FinalForce = FinalForce.sub(relForce, myBody.getLinearVel());  // removes the current velocity from the body which stops sliding
myBody.addForce(finalForce); //apply forces to body

This works fairly well as long as the body is moving forwards however if my body hits and object and bounces backwards my linera velocity is negative and FinalForce.sub(relForce, myBody.getLinearVel()); add the 2 forces together which creates a situation where I now have more force than I’m supposed to. I think I can fix this using the example arne provided.

But otelo I have seen other examples around of what you describe and it sounds interesting. My question is from your code what is carForward and what does carTransfrom do and do your think it would apply the same way to force not velocity?

Vector3f worldVelocity; //your global velocity vector
Vector3f carForward; // - normalized! your car’s local forward vector e.g. (0,0,1)
Transform3D carTransform; // your car’s current world transform
float forwardVelocity; // your relative velocity

How do I get local velocity though thats what I’m after?

ohh yeah Matrix3f(4f).mul only multiplies Matrices…I believe it’s called transform, if you want to multiply Vectors with matrix.

I had it wrong … I was going for all 3 velocity components, but it wasn’t right anyway. I made some corrections to the posts above. take a look.