GMS 2 Platformer Bullet Ricochet/bouncing/reflecting

Ayziak

Member
Hi all,

So for a while, I've been trying to make reliable platformer bullet ricochet. Essentially, the player would shoot a projectile that would bounce/reflect off a designated object (that could be at any angle) following the ai = ar (angle of incidence = angle of reflection) formula. (as seen here):


I have an existing system, but it's very broken. Bullets sometimes pass all the way through or get caught inside the reflector object. I'm hoping to redo this system from the ground up but I'm not sure where to start..

Let me know if I need to clarify anything.

Any thoughts/ideas?

Either way, thanks for reading!
 

Bingdom

Googledom
Code:
if collision_hor {
    hspeed*=-1
}
Do the same for vertical collisions

If you want something more precise (Like angled surfaces), then there's a script somewhere on gmlscripts.com
 

Ayziak

Member
Code:
if collision_hor {
    hspeed*=-1
}
Do the same for vertical collisions

If you want something more precise (Like angled surfaces), then there's a script somewhere on gmlscripts.com
Yes, I do intend to use angled surfaces. I'll have a look for that script– thanks!
 

obscene

Member
First, your issue sounds like some bad collision scripting. That's a whole other topic. First, find a way to get that working.

Second, here's a script for getting the normal of a platform at any point. You'll need to get your bullet to the point of impact then run this script.
http://www.gmlscripts.com/script/collision_normal

Third, figure out your angle of reflect as is demonstrated in your image. This is what seemed to work for me...

normal=scr_normal_detect(x,y,par_solid,16,4)
var diff=direction-(normal+180);
direction=normal-diff;
 

John Andrews

Living Enigma
Hello! I have the solution for you, I developed a formula where you can have a bullet going in an angle, a surface that has another angle, and the bullet would be reflected just in the correct angle, for example if the bullet is going in a direction of 45 degrees and the surface is a vertical 90 degrees wall, it will end going with a direction of 135 degrees, another example, the bullet goes with 45 degrees angle, collides with the cieling, it ends with a 315 degrees direction (I don't have the formula here but if you still need help I can give it to ya ;)) No scripts needed, just a plain formula.
 
Last edited:

John Andrews

Living Enigma
This... Is the formula:


Code:
if direction<surface.direction
    direction=direction+(180-(180-(abs(surface.direction-direction)*2)))
else
    direction=direction+(180-(180-(abs((surface.direction+360)-direction)*2)))
 
First, your issue sounds like some bad collision scripting. That's a whole other topic. First, find a way to get that working.

Second, here's a script for getting the normal of a platform at any point. You'll need to get your bullet to the point of impact then run this script.
http://www.gmlscripts.com/script/collision_normal

Third, figure out your angle of reflect as is demonstrated in your image. This is what seemed to work for me...

normal=scr_normal_detect(x,y,par_solid,16,4)
var diff=direction-(normal+180);
direction=normal-diff;

Thanks very much for this :)
 
Top