PDA

View Full Version : Car relative vector to world relative vector


Cookie
2002.09.30, 08:22 AM
hi everyone

I'm trying to make a simple 3D racing game, and I need to convert a car-relative velocity vector into a world co-ordinate velocity vector.

For my car-relative velocity vector, the Z axis is in straight out the windsheild, the x-axis cuts across the car, and the y axis points up out of the car.
I need to convert this velocity vector into a world coordinate velocity vector.

Thanks

OneSadCookie
2002.09.30, 08:33 AM
I remember this! It's a basis transformation!

Um...

Yeah.

OneSadCookie
2002.09.30, 08:48 AM
Oh boy, linear algebra!

Anton, "Elementary Linear Algebra", 7th ed.

Solution of the Change of Basis Problem. If we change the basis for a vector space V from some old basis B = {u1, u2, ..., un} to some new basis B' = {u'1, u'2, ..., u'n}, then the old coordinate matrix [v]B of a vector v is related to the new coordinate matrix [v]B' of the same vector v by the equation

[v]B = P[v]B'

where the columns of P are the coordinate matrices of the new basis vectors relative to the old basis; that is, the column vectors of P are

[u'1]B, [u'2]B, ..., [u'n]B

OneSadCookie
2002.09.30, 08:53 AM
So (if I've got this right, and it's 12.45 AM so take it with a grain of salt), what you need to do is make a matrix with the vectors for the x direction of the car in world space, the y direction of the car in world space, and the z direction of the car in world space, and multiply that matrix by the car-relative velocity vector.

OneSadCookie
2002.09.30, 08:54 AM
I have no idea whether that's right though. I should really leave the math questions to the people who actually remember these things...

kainsin
2002.09.30, 10:20 AM
Sounds just right to me.

Cookie
2002.09.30, 10:39 AM
thanks onesadcookie, that sounds easy enough, although I'd rather not use matrices.

although after playing around I came up with this for 2D transformation:

velwc.x=-sin(yawamount)*velocity.z+cos(yawamount)*velocity. x
velwc.z=-cos(yawamount)*velocity.z+sin(yawamount)*velocity. x



Is there a similar way for 3D transformation? I attempted to work it out, but now my brain hurts. I'd prefer to stay away from matrices as I really have no idea what i am doing.

ededed
2002.09.30, 11:07 AM
Just a hint that may not help in your situation but It is worth noting.

DotProduct(x, y) = (Sin(theta), Cos(theta))
theta = the bearing of (x, y) to the origin

DotProduct(x, y){
d = sqrt(x*x + y*y)
x /= d
y /= d
}

Out of intrest why might you need to convert a relative velocity to a world velocity, why not just use a world velocity for a car?

Cookie
2002.09.30, 11:52 AM
I'm implementing some car physics, and some of these equations deal with the relative velocity of the car, and the wheel angle and such. These forces are acting on the car in relation to the car, not world coordinates, hence the need to convert.

Anyway, I'll see if I can get some of this matrix stuff workin. Even though I did matrices last semester in school, im still a bit rusty.. but it sounds easy enough.

Thanks everyone!

ededed
2002.10.01, 01:49 PM
Just a second, you seem to be making this very complicated, using matrices and things. You must rotate the car to draw it properly... So cant you just to the opposite rotatation on the vector of the car.

OneSadCookie
2002.10.01, 05:56 PM
It's a 3x3 matrix multiplication. That's nine multiplies and six adds -- it's going to be easier and faster than some yucky trigonometric solution.

codemattic
2002.10.01, 06:30 PM
You have a car that is rotated and translated. How are you currently storing that rotation now? A quaternion? A 3x3 rotation matrix?

Originally posted by ededed
Just a second, you seem to be making this very complicated, using matrices and things.

I would argue he is making his life *harder* by avoiding using matricies and things.

David
2002.10.01, 06:48 PM
Originally posted by Cookie
thanks onesadcookie, that sounds easy enough, although I'd rather not use matrices.

although after playing around I came up with this for 2D transformation:

velwc.x=-sin(yawamount)*velocity.z+cos(yawamount)*velocity. x
velwc.z=-cos(yawamount)*velocity.z+sin(yawamount)*velocity. x



Is there a similar way for 3D transformation? I attempted to work it out, but now my brain hurts. I'd prefer to stay away from matrices as I really have no idea what i am doing.

My dorotation method:
XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang){
XYZ newpoint;
if(xang){
xang*=6.283185;
xang/=360;
}
if(yang){
yang*=6.283185;
yang/=360;
}
if(zang){
zang*=6.283185;
zang/=360;
}


if(yang){
newpoint.z=thePoint.z*cos(yang)-thePoint.x*sin(yang);
newpoint.x=thePoint.z*sin(yang)+thePoint.x*cos(yan g);
thePoint.z=newpoint.z;
thePoint.x=newpoint.x;
}

if(zang){
newpoint.x=thePoint.x*cos(zang)-thePoint.y*sin(zang);
newpoint.y=thePoint.y*cos(zang)+thePoint.x*sin(zan g);
thePoint.x=newpoint.x;
thePoint.y=newpoint.y;
}

if(xang){
newpoint.y=thePoint.y*cos(xang)-thePoint.z*sin(xang);
newpoint.z=thePoint.y*sin(xang)+thePoint.z*cos(xan g);
thePoint.z=newpoint.z;
thePoint.y=newpoint.y;
}

return thePoint;
}

where xyz is a class of 3 floats, x,y and z
xang is rotation around x axis, etc.

I hope this helps.

Cookie
2002.10.05, 02:16 AM
Actually, I see what you mean now OSC and codemattic, so I'll go with matrices.

Currently i am storing the orientation of my car in a quarternion. How would I store it in a 3x3 matrix?

Thanks.

OneSadCookie
2002.10.05, 06:55 AM
Oh, if you're storing the orientation as a quaternion, there's probably an even simpler solution than we've proposed...

Unfortunately, I know nothing about quaternions, so I can't be of assistance.

If you can turn it into the three vectors that my matrix method needed, you could still go that route...

ededed
2002.10.06, 01:16 PM
What are quaternions, aside from being 4 part complex numbers. How do you use them?