Friday, April 2, 2010

Jumping (part 2)

Hello everyone!

Ok, I'm going to talk about gravity right now. If a character is standing straight up (ie his Y axis is aligned/parallel to the world's Y axis) and then he jumps, gravity will only affect his Y axis, since it's a force that only applies to the world's Y, so, since they are parallel, it's the only axis it will affect.

However, what happens when a character is rotated? Here's a small image that shows, in blue, the character's axes and, in red, the world's axes:


We can see that, not only are the Y axes not parallel to each other, but neither are Z and X.
Normally, these would be the values for each axis:
X = (1,0,0)
Y = (0,1,0)
Z = (0,0,1)
They are unit vectors (the length of each axis is just 1). When a matrix is rotated, these values will change. They will reflect the angle in which they were rotated in a certain direction.

In this case, we're interested in the angle of the Y component of each axis. So, for example, to get the angle in which the Z axis has rotated in the Y direction, we get the arc-sine of the Y component of the Z axis.
After getting this angle, we can then adjust the velocity of the jumping object:
horizontal velocity - (gravity * sine(angle) * time)
So, the sine of the angle is the Y component of the Z axis, right? We just plug it into the formula.
We do the same wit the X axis, except that we don't put a horizontal velocity, since the player is jumping on his forward vector (ie Z axis).

For the Y axis, we just use the vertical velocity instead of the horizontal one and use cosines instead of sines (ie an arc-cosine of the Y component of the Y axis to get the angle and then a cosine of that angle in the formula explained above) and voila, we have applied the corresponding gravity in all three axes!

No comments:

Post a Comment