• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Collision Line Troubles

B

Blaize

Guest
Ok, so right now, I'm using collision_line() in a Megaman.exe-style game as part of a sensor-like function.
So, let's have a look at this image:
Ok, so the one with the green square is the player and everything else is an enemy. The white rectangle is our player's collision line. It's origin is from the left or right bbox, depending on which way it's facing (ie. Right uses bbox_right, left uses bbox_left).
Now, when facing right (as in the image), collision_line will return 100007, as expected.
However:
In this situation, collision_line will still return 100007 instead of the one closer to the player.

I've been wrestling with this for a while, but still can't get my head around it.
Here's my code:
Code:
#macro COMBATGRID 64 (This is in a Macros script)
if (_attack && AttackTimer == 0)
{
    var _xOrigin = x+(Facing==1?bbox_right:bbox_left); //Facing == 1 means right; -1 means left. 0 is disregarded
    var _hit = collision_line(_xOrigin,y+(COMBATGRID/2), (Facing==1? room_width : 0),y+(COMBATGRID/2),pCombatActor,false,true);
with (_hit)
            show_debug_message("Hit "+string(id));
}
I have a feeling that it is picking up both, but returning the instance with the lower ID? Could be wrong
Much appreciated.
 

Attachments

A

Alex_Beach

Guest
Your collision line is is hitting 2 objects, but the function can only return 1 ID at a time, that's the problem (it probably priorities the lower value). So, I would make your line extend 1 box at a time until it hits an object, or misses offscreen. Example: Make short line, check for collision. If no collision, make line longer and check again. I had a similar issue with a puzzle game, so I approached it this way and it worked.
 
B

Blaize

Guest
Your collision line is is hitting 2 objects, but the function can only return 1 ID at a time, that's the problem (it probably priorities the lower value).
Yeah I thought as much.

So, I would make your line extend 1 box at a time until it hits an object, or misses offscreen. Example: Make short line, check for collision. If no collision, make line longer and check again. I had a similar issue with a puzzle game, so I approached it this way and it worked.
Hm, that might work, though it may become taxing on the game if there were multiple characters doing the same calculations. Eventually, there will be some enemies which will have similar abilities to the player themselves.
I'll definitely try it out, though.
 

hogwater

Member
There is a script called "collision_line_list" or something that returns a list of the instances colliding with the collision_line function. Pretty sure it's on GMLscripts.com, that may help.
 
B

Blaize

Guest
There is a script called "collision_line_list" or something that returns a list of the instances colliding with the collision_line function. Pretty sure it's on GMLscripts.com, that may help.
Oh yes I found it, here: http://www.gmlscripts.com/script/collision_line_list
I wonder if it will impact the speed though. I'm sure it won't since I don't plan to have more than 5 objects on screen (enemies + player), but thanks, I'll try it out.
 
B

Blaize

Guest
So I've tried both out and, after making the appropriate tweaks to collision_line_first() for my needs, this is solved!

Cheers, folks!
 
Top