How do I Lob an Object Like a Grenade?

Get help using Construct 2

Post » Thu Oct 27, 2016 9:47 pm

Hello all and thanks for looking.

Im trying to lob an object with a parabola like a grenade does. Using angle of two points is resulting in the object moving in a straight line. I tried angleLerp but with no success. Does anyone know how to do this? Thanks again :)
B
25
S
7
Posts: 79
Reputation: 2,267

Post » 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.
B
4
S
1
Posts: 3
Reputation: 276


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests