Legacy GM Transfer local impulse or force

XirmiX

Member
I belive I've made a thread about this before, although I did not get any help there really and it seems like it was a tough nut to really comprehend. I'm talking in terms of recoil and impact force which get applied by shot projectiles at a target or basically an enemy or yourself. Obviously, this is taking physics into mind.

My projectile object, currently has Create event setting off two alarms; alarm 1 and alarm 2. Alarm 1 is activated after 3 frames (reason being that the gun/turret you're shooting from is attached to a hull and so when you shoot your projectile from the turret, the turret and the spawn point of the projectile on the turret might overlap the hull that the turret is sitting on, so having a delay before any impact force and collisions with the hull happen is necessary) and Alarm 2 gets activated as soon as the projectile is created (because this alarm is supposed to apply recoil). This is the code I have for the according alarms as it stands:

Alarm 1:
Code:
//Applying impact force
if place_meeting(x,y,obj_hull)
{
    instance_create(x,y,obj_ricochet_splat)
    //motion_add(phy_rotation,phy_mass)
    //move_bounce_solid(true);
    with(obj_hull)
    {
        var tipx, tipy;
        projectile_angle = obj_ricochet_projectile.direction;
        tipx = x + dcos(projectile_angle) * 0.1;
        tipy = y + -dsin(projectile_angle) * 0.1;
        physics_apply_local_impulse(projectile_angle,0,-tipx,-tipy)
    }
    instance_destroy();
}
else
{
    alarm[1] = 1;
}

Alarm 2:

Code:
//Applying recoil
with(obj_hull)
{
    var recoil = phy_mass * acceleration;
    var tipx, tipy;
    turretAng = obj_turret.image_angle;
    tipx = x + dcos(turretAng) * recoil;
    tipy = y + -dsin(turretAng) * recoil;
    physics_apply_local_force(-96,0,-tipx,-tipy)
}
The problem for me is that the stuff in the first alarm, upon impact with a projectile instance make the hull move to its down-left or south-west a bit instead of in the direction that the projectile hit it from and the recoil does absolutely nothing to the hull (it's as if the force isn't even applied). I know what I'm doing wrong for the first one, and it's in terms of the whole "projectile_angle = obj_ricochet_projectile.direction;" because the projectile is physics based, so it has an impact force moving it forward and not direction, however it would not make sense by GML logic to make the code be something like "projectile_angle = obj_ricochet_projectile.local_force;" because, well, for one local force isn't something defined, but something that can only be applied (the projectile detaches on walls, btw, so this is why you could hit your own hull for the tank, which is what I'm trying to make happen atm). So, question is, how could I do the first alarm code so that it works as intended (impact force) and why does recoil not get applied for the hull and how could I apply it?

P.S Ricochet splat is basically this neat little thing I put into the game that spawns in when the projectile hits a wall or something, which just makes it look pretty :p
 
H

heyimdandan

Guest
Alarm 1: I might be wrong, but the value being returned by dsin and dcos for tipx and tipy might be too small to notice any difference in movement. I could be wrong, but you've even gone as far as multiplying it by 0.1 - so it's making it a 10th of the figure being returned on top of that.

Alarm 2: Another thing (and this may even apply to alarm 1), you're calling the direction from obj_ricochet_projectile - the outcome of the function you've created also depends largely on the inputs coming from the external objects. Your maths may be sound in what you've written up, but who knows what numbers might be coming in externally from the other instance.

Your explanation of what you're trying to achieve is very wordy and I'm trying my best to look at what you're doing with the code, but I can't visualise what's going on easily! What you haven't explained is whether the gun turret is part of the sprite of the vehicle, or whether it's a separate sprite being drawn in the draw event, or even something else! Some screen grabs may help as it's not clear from your description of the game play mechanic either!
 

XirmiX

Member
Alarm 1: I might be wrong, but the value being returned by dsin and dcos for tipx and tipy might be too small to notice any difference in movement. I could be wrong, but you've even gone as far as multiplying it by 0.1 - so it's making it a 10th of the figure being returned on top of that.

Alarm 2: Another thing (and this may even apply to alarm 1), you're calling the direction from obj_ricochet_projectile - the outcome of the function you've created also depends largely on the inputs coming from the external objects. Your maths may be sound in what you've written up, but who knows what numbers might be coming in externally from the other instance.

Your explanation of what you're trying to achieve is very wordy and I'm trying my best to look at what you're doing with the code, but I can't visualise what's going on easily! What you haven't explained is whether the gun turret is part of the sprite of the vehicle, or whether it's a separate sprite being drawn in the draw event, or even something else! Some screen grabs may help as it's not clear from your description of the game play mechanic either!
Oh, emm, yeah, the turret is a separate object. It can be rotated and follows the hull. The hull can be rotated to left and right (along with the turret) and move forwards and backwards (along with the turret), so kind of like how a real tank moves irl.

The density of the projectile is 0.05 and its acceleration is 60000, equating to 3000 force exerted, which is also what the recoil variable is set to in these equations of the second alarm:
tipx = x + dcos(turretAng) * recoil;
tipy = y + -dsin(turretAng) * recoil;

And for some reason there is no force given to the hull when the projectile is shot :/ I believe the problem might be in this:
physics_apply_local_force(-96,0,-tipx,-tipy)
Because the location of the force that is being applied for recoil is always the same, which is why it might not be applied. But then again, it might be something entirely different.

As for the impact force and Alarm 2...
Yeah, it seems it might be the same issue:
physics_apply_local_impulse(projectile_angle,0,-tipx,-tipy)
I've no idea how to fix this really.

Also here's an image of the tank (the turret has not been turned in here, however):
 

XirmiX

Member
Leaning off a bit from the main issue, I'm kind of surprised there's no tutorials or anything in terms of recoil for game maker studio and impact force for top-down shooter like games as far as physics go, or even non-physics based tutorials. But I really need this fixed, otherwise I will never be able to release the game or release the game without impact force and recoil being in place, which would suck :(

Edit: Btw, it seems I have created a similar topic before (here's the topic), but at the time I didn't have as much understanding of forces and impulses... although it still seems I don't really have that much (can't apply a force to an object from another object), I know what exactly I want to do, which is to make it so that an object (projectile instance) upon collision with another object (hull instance) to give it force in the direction it was last travelling at and in the opposite direction the moment it is created.
 
Last edited:
Top