I've been working on physical jumping in a 3D environment, I've got it working with a static force. The problem with this is that the force on each axis should be different due to the rotation from the player.
A few things to keep in mind:
- A total rotation of 360 degrees can be made;
- Forces should never be greater than 0.2 or lower than -0.2;
- There is no framework/engine behind the game, it's all hardcore codings.
I've been trying something like;
velocity.x = x - ( x + ( 10 * math.cos ( ( rz + 90 ) * math.pi / 180 ) ) );
velocity.y = y - ( y + ( 20 * math.cos ( ( rz + 90 ) * math.pi / 180 ) ) );
where x,y is the spawned location and rz is the rotation from the player. This way, I'll get the location from the player 20 units in front of him. By subtracting the actual starting position from it, I should've gotten a velocity. However, this turned out to be wrong. It's not going towards the direction the user is facing, it's just doing it's own simple thing.
The jumping/throwing into 1 direction is working perfectly fine. The calculations I made so far are totally messed up and not working as expected.
The results which I am expecting to get is something like;
force.x = 0.1;
force.y = 0.2;
this can be negative or positive, this should be dynamic though.
Answer
So first, in the formula:
velocity.x = x - ( x + ( 10 * math.cos ( ( rz + 90 ) * math.pi / 180 ) ) );
The two 'x's cancel out each other:
x - (x + n)
is same as
x - x - n
Then to convert an angle to a vector you need to use sin AND cos.
velocity.x = speed * math.cos ( rz * math.pi / 180 );
velocity.y = speed * math.sin ( rz * math.pi / 180 );
In this case rz will indicate a counter-clockwise rotation if y increases vertically (clockwise if not), with 0 degrees being in the positive x direction (usually, to the right).
To have rz rotate the other way:
velocity.x = speed * math.cos ( rz * math.pi / 180 );
velocity.y = speed * math.sin ( rz * math.pi / -180 );
No comments:
Post a Comment