Legacy GM I have a good solution for fast bullets

X

xorphinindi

Guest
I've seen a lot of threads about this problem where setting bullets to high speeds in scrolling shooter games causes the bullets to skip over objects. In my game, I have arrows that fire in the direction of my cursor at any angle and the high speed caused them to partially move through the wall before stopping (and sometimes before hitting the wall), but I think it was essentially the same problem. The only difference would be that instead of just stopping when hitting the wall, the bullet would destroy itself.
My solution primarily came from this video. What I have now certainly isn't the perfect solution, but I feel that looking for much more accuracy would be pushing the limits of the software. I hope this helps anyone with a similar problem.

The arrow create event:
Code:
/// Handle Motion

var dir = point_direction(x, y, mouse_x, mouse_y);

image_angle = dir;
direction = dir;
arrowSpeed = 50;
hsp = cos(degtorad(dir)) * arrowSpeed;
vsp = -sin(degtorad(dir)) * arrowSpeed;
The arrow step event:
Code:
/// Perfect collision with wall

// Horizontal Collision
if (place_meeting(x + hsp, y, objWall))
{
    while (!place_meeting(x + sign(hsp), y, objWall))
    {
        x += sign(hsp);
    }
    hsp = 0;
    vsp = 0;
}
x += hsp;

// Vertical Collision
if (place_meeting(x, y + vsp, objWall))
{
    while (!place_meeting(x, y + sign(vsp), objWall))
    {
        y += sign(vsp);
    }
    vsp = 0;
    hsp = 0;
}
y += vsp;
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Hi there! This is a pretty standard solution to the problem, and one that many people will benefit from as it's possibly not the most obvious way to do things, especially when starting out. Another way to do this is to make "hitscan" bullets, where you calculate the trajectory and any hits encountered all at once (instantly) and then either mark the hits or create a bullet to move to the point so it looks like it happened over time. You'll find this script interesting if you want to approach such a system for yourself: http://www.gmlscripts.com/script/collision_line_first

Anyway, thanks for sharing!
 
Top