GML Another fast bullet collission thread [SOLVED]

Strobosaur

Member
Sorry if the answer has already been posted, i've looked around but haven't found any good solutions.

It's a classic case of a projectile traveling too fast to collide with the intended target. I do have a script in place to check for a line collision every step, and it works EXCEPT in the first step after the bullet is created. Here's some code:
Bullet Step event runs the following script:

var inst = collision_line(x, y, x + lengthdir_x(speed, direction), y + lengthdir_y(speed, direction), argument0, false, false);

var incr = 0;

if (inst != noone) && (inst != shooter)
{
while (!place_meeting(x, y, inst)) && (incr <= speed)
{
x += lengthdir_x(1, direction);
y += lengthdir_y(1, direction);

incr++;
}
}
argument0 is a parent object for solid objects which is what the bullet should hit, shooter is the id of the instance that fired the bullet (which should of course, not be hit).

It works fine except when the target object is within one length of the bullets speed from the shooter. I've tried a script that instead of checking one length of speed forward checks from one length of speed backwards and up to the current position, which worked but displays the bullet sprite past the collision point for one frame which looks bad. I've tried creating the bullet at the center of the shooting object instead of the tip of the weapon, but same problem... Tried running the line script in the create event of the bullet, or in the shooters 'with instance_create' block for the bullet, but no luck.

Kind of feels like i've missed something obvious here but for the past few days i still haven't been able to figure it out so i now humbly place myself at your mercy :) Anyone have any ideas?

If there are any other pieces of code that could be of interest just let me know.

Thanks!
 

Strobosaur

Member
The coordinates are only moved manually in the pixel-for-pixel collision checking loop in the script, otherwise the object travels with the built in speed & direction. Which is a bit lazy, but i thought it would be fine for bullets traveling in straight lines... I can't really see why it would make a difference in this case, but it would be easy enough to change to a manual movement system since all other moving instances use my own movement scripts... Just for future reference though - would it make a difference, and if so, why?
 
B

Blackened

Guest
I can't really see why it would make a difference in this case, but it would be easy enough to change to a manual movement system since all other moving instances use my own movement scripts... Just for future reference though - would it make a difference, and if so, why?
Built-in variables get updated after the Step Event. The End Step event is when everything has been updated. Speed, hspeed and vspeed change the position of the instance at the end of the step event, every time, and there is no way to prevent that. This is irritating as this can produce unusual results when you program collisions, which is why many people do not care to use them. As a matter of preference, I tend to not use built-in variables. Instead, I opt for more custom control versus allowing game maker to handle it under-the-hood. This does not mean that built-in-variables don't have use, I just wouldn't suggest using speed for collisions. Hope this helps.
 

dannerz

Member
repeat(20){ x += 20; if place_meeting(x,y,enemy_obj) { enemy_obj.HP -= 2; exit; } }

That wont work perfectly but it is an idea.
You can use repeat to cover and check a wide range of space.
 

Strobosaur

Member
Hope this helps.
It did help :)

I set the bullets to use the manual move-scripts, put the line-collision script in the step event and the move script that updates x&y in the end step, and i have not seen any teleporting bullets since.

I must say though i still don't really get exactly what situations are good for begin step/step/end step, or exactly when they occur in the timeline of things... I chose the end step for the move-script because the line collision script doesn't actually activate any collision event, it just moves the projectile to the collision point. I thought that maybe putting the move script in the same step event might move the instance further beyond that point before the collision event activates, but i really don't know :) The manual is kind of vague on this point imo... Any thoughts on this?

Thanks for the help anyway, it's good to know that the build in physics variables can behave differently.
 
Top