GML How can I make a grenade throw path more natural?

B

Buttonsinker

Guest
Been messing around making a platformer were I can shoot bullets when I press left mb and throw a grenade with right mb. But in order to make my grenade throw have some sort of arc rather than a straight bullet, I made two paths using the path editor. One for when I'm aiming to the right and the other for aiming left. It looks pretty good but it feels very static and un-natural. Because no matter how high right or low left I'm aiming, it will always do the same path and go the same distance, which is normal since it follows a preseted path I made.

I was wondering if there was another way around were it could react to a gravity I set just for the grenade because I'm not using "Room Physics". So it could shoot straight where I'm aiming like a bullet but then fall down as if gravity was pushing it down.

I'm happy with my grenade throw so far but I'm not fully satisfied and really wanna get better at this.

Any idea on how I could make this work?
 

Binsk

Member
Program your own pseudo-physics. You don't have to use the built-in physics system, just do a little simple math to fake the general idea.

You said it is a platformer which makes me assume you know how to fake gravity (since you had to create your player character). Just do so with the grenade. Optionally add a little friction and give it the starting velocity in the direction your mouse is aiming. To make it "bounce" just have it invert and half the vspeed for vertical collisions and invert and half the hspeed for horizontal collisions. Once it goes below a certain velocity set the h/vspeed to 0 so that it comes to rest.

This makes a pretty convincing throw and is relatively simple to implement.
 

NightFrost

Member
What Binsk says above is exactly how I've done it before. In pseudocodeish:
Code:
// Throw code in player step event
// Assumes ThrowSpeed variable exists
if(throwing a grenade check here){
    var ThrowDir = point_direction(x, y, mouse_x, mouse_y); // Find relative mouse direction
    var Grenade = instance_create(x, y, obj_grenade); // Spawns the grenade at your feet so you'll want to give it an accurate spawn position
    Grenade.MoveY = -lengthdir_y(ThrowSpeed, ThrowDir); // Initial vertical speed
    Grenade.MoveX = lengthdir_x(ThrowSpeed, ThrowDir); // Initial horizontal speed
    ...
}
The grenade's code then moves it and reacts to collisions
Code:
// CREATE
MoveX = 0;
MoveY = 0;

// STEP
MoveY += your gravity constant here;
// Collision logic is the same as for every other moving object so you can mostly copypaste
// However on vertical collision after collision has been handled you need to
MoveY= -MoveY / 2;
// Same for horizontal
MoveX = -MoveX / 2;
// Also you don't zero your movement deltas after collisions as it needs to remain in constant motion
 
Top