###

» Fri Oct 28, 2016 10:47 am

Assuming that you are using a side-on perspective, calculate the x and y velocities for the grenade - you can use the trig functions to determine these values, based upon how you control the grenade:

1) If your grenade is being thrown at a speed of S pixels per tick at an angle of A degrees then the x velocity is S*cos(A) and the y velocity is S*sin(A).

2) Every tick move your grenade at velocity X in the X axis and -velocity Y in the Y axis (the Y value is negative because the Y axis starts at zero at the top of the screen). Also you may need to reverse the X velocity if you are throwing the grenade to the left instead of the right.

3) You now have to account for gravity, otherwise your grenade will keep moving in a straight line. Gravity affects your y velocity. Create a global variable "gravity" and give it a small value. Each tick add the gravity value to the y velocity; you can play around with the gravity value until you find something that feels good.

You should now have a nice arc to your grenade. If you want the grenade to bounce on hitting the ground, multiply the y velocity by -1 on collision with the ground.

If you want to bounce the grenade off of a vertical wall multiply the x velocity by -1.

You will also need to apply some damping to the x and y velocities to reduce the grenade's speed each time it bounces, so also multiply both velocities on collision with the ground by a number less than 1.

One more thing - don't forget to multiply through by dt if you want the movement to be framerate independent.

Hope that works for you.