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.

  • \$\begingroup\$ It's not clear to me what you're trying to do here. Maybe try including a diagram or video? Also, I've written several previous answers on calculating launch vectors for ballistic motion — maybe some of those would be useful to you? \$\endgroup\$
    – DMGregory
    May 14 '16 at 16:46
  • \$\begingroup\$ I got the functionality to jump, that's working. but it's only working with static values. I declared a velocity.x and velocity.y somewhere to get the script working. these values will make the player jump a certain direction. I, however, don't want to force the users to be only able to jump into 1 direction. So, the force for X and Y should be calculated depending on the rotation from the user. \$\endgroup\$
    – Tosfera
    May 14 '16 at 16:53

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 );
  • 1
    \$\begingroup\$ After changing it a little bit, I got it working. Had to change a few things in the rz since that gave me some wrong rotations. But you sir, have just saved my week! Can't thank you enough! will award the bounty in 21h! \$\endgroup\$
    – Tosfera
    May 14 '16 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.