GameMaker Fast bullets not colliding

D

Dibidoolandas

Guest
As it says in the title, I have very fast bullets in my game and I'm focusing first on having them destroy terrain/collide with walls. The problem is they're still flying through things. I've read up on the subject but for whatever reason the solution I see people reference doesn't appear to be working for me.

Create event just sets variables for hsp, hdir (speed and direction)

End Step event (I've been instructed that this should take place on end step)
var hit_wall;
hit_wall = (collision_line(xprevious, yprevious, x, y, par_impassible, false, false))

if (hit_wall != noone)
{
if (hit_wall.destructible == true)
{
with (hit_wall)
{
hp --;
}
}
instance_destroy();
}

Step event (The bullet's regular movement)
hsp = acc * hdir; //speed * direction = horizontal speed

x += hsp;

Any thoughts? Generally it seems like the bullet still only destroys things it comes into contact with. However every once in a while it seems like it's destroying more than that? Additionally this sometimes destroys the wall AFTER the one closest to the player, then destroys the wall closest.
 
R

Ratsha

Guest
Do you remember to update xprevious and yprevious in the End Step Event after the code?
Code:
xprevious = x;
yprevious = y;
Nothing in the code indicates walls are destroyed. Do have any collision events that interferes with this?
 
D

Dibidoolandas

Guest
Do you remember to update xprevious and yprevious in the End Step Event after the code?
Code:
xprevious = x;
yprevious = y;
For some reason Game Maker Help isn't loading for me so I can't confirm, but don't xprevious and yprevious basically check this by default as a built-in variable? In any case I added those two lines at the end of the end step event and it didn't make a difference.

Nothing in the code indicates walls are destroyed. Do have any collision events that interferes with this?
I'm not entirely sure what you mean by this. The variable hit_wall is meant to check if it's hitting a wall (par_impassible), and if it is, it does a check to see if that wall is destructible. If it is, it subtracts 1 hp from the wall (which destroys it) and then the bullet destroys itself.
 

Relic

Member
Collision_line returns an instance id that is along the line passed. You cannot be sure which id is returned if more than one instance exists along the line. You will need an algorithm that will find the nearest instance to xprevious,ypreviois. This may give you some clues:

https://yal.cc/gamemaker-collision-line-point/

You can skip thenpart about finding the exact collision point and just return the closest instance (or just use r[0] as this is the id you require.
 
R

Ratsha

Guest
For some reason Game Maker Help isn't loading for me so I can't confirm, but don't xprevious and yprevious basically check this by default as a built-in variable? In any case I added those two lines at the end of the end step event and it didn't make a difference.
You are correct, my mistake.

I'm not entirely sure what you mean by this. The variable hit_wall is meant to check if it's hitting a wall (par_impassible), and if it is, it does a check to see if that wall is destructible. If it is, it subtracts 1 hp from the wall (which destroys it) and then the bullet destroys itself.
Ok that's fine. Just checking as there is no way to know without the code.

Try to set the last argument of collision_line to true (whether to ignore self, the bullet). The collision line might current always return an object which may be either the bullet itself or the wall. If it returns the bullet, nothing happens which may result in the seemingly random behavior:
Code:
hit_wall = collision_line(xprevious, yprevious, x, y, par_impassible, false, true)
 
D

Dibidoolandas

Guest
Collision_line returns an instance id that is along the line passed. You cannot be sure which id is returned if more than one instance exists along the line. You will need an algorithm that will find the nearest instance to xprevious,ypreviois. This may give you some clues:

https://yal.cc/gamemaker-collision-line-point/

You can skip thenpart about finding the exact collision point and just return the closest instance (or just use r[0] as this is the id you require.
This worked perfectly. Thanks for taking the time! Appreciate the help, all.
 
Top