How do I Lob an Object Like a Grenade?

1 favourites
  • 2 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)