Weird Collision Issue, Please Help

M

MrAmsel

Guest
So I've been working on a simple 2-player fighting game and I ran into an interesting bug when trying to detect the players hitting each other. Currently I have each player create a hitbox object when they press the attack button and then I have each player object check to see if they collide with that hitbox object in the step event, if they do collide they lose heath.

The problem is my player 2 object is able to detect the collision, and therefore lose heath, but my player 1 object is not able to detect the collision even though the code is identical for each player.

I ran some test to see what might be causing the issue and I found something really weird. I am able to detect the collision of the hitbox generated by either player object on a third object, and so I started to think maybe the issue was with how the objects were being loaded into the room. So I moved my player 2 object above my player 1 object in my object library and the problem was revered. So I am guessing there is something I don't quite understand with how objects are loaded into a room and how that effects possible collision checks. Can somebody help point me in the direction on how to make sure the objects can collide properly?

Also I tired checking for collision using place_meeting and position_meeting with the same result in both instances.
 
M

MrAmsel

Guest
Here is my code for my player 1 object

//react to inputs
move = key_right - key_left;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_mass))
{
vsp = key_jump * -jumpspeed;
}

if(position_meeting(x,y,obj_hitbox))
{
global.p1hit = true
}

if (global.p1hit = true)
{
global.p1hp -= 1;
global.p1hit = false;
}

if (global.p1hp = 0)
{
instance_destroy();
global.p1hp = 30;
}

if(key_atk1)
{
scr_attack();
}

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

//Verticle Collision
if (place_meeting(x,y+vsp,obj_mass))
{
while (!place_meeting(x,y+sign(vsp),obj_mass))
{
y += sign(vsp);
}
vsp = 0;
}

y += vsp;


Here is my scr_attack() script

instance_create(x+15,y-20,obj_hitbox);


And here is the create event for player 1(the create event is almost identical to it for player 2)
im replaceing the sprite name titles with '***' just so you don't see the names of the sprites.

scr_getinputp1();
global.p1hit = false;
grav = 0.5;
hsp = 0;
vsp = 0;
jumpspeed = 7;
movespeed = 4;
switch (global.character1_selection)
{
case 1: {
sprite_index = **********1
break;
}
case 2: {
sprite_index = **********2
break;
}
case 3: sprite_index = **********3; break;
case 4: sprite_index = **********4; break;
case 5: sprite_index = **********5; break;

}
 
Last edited by a moderator:

Stubbjax

Member
It is much more efficient if you have the "collider" object doing the collision checking, rather than the players. Here's some basic code that may help:

A player attacks:

var attack_id;
attack_id = instance_create(x, y, attack_object);
attack_id.player_id = player_id; // Set player_id to 1 for player 1; 2 for player 2, etc.

Collision object checking for a collision (probably step event):

var colliding_player;
colliding_player = place_meeting(x, y, player);
if (colliding_player)
{
if (colliding_player.player_id != player_id) // If the colliding player is not the player who created the attack
colliding_player.health -= 10;
}
 
A

Aura

Guest
Welcome to the GMC!

It'd be great if you share all the relevant code with us, makes it very easy for us to help you; you might also want to post the object information of both the player objects and the hitbox object.
 

chance

predictably random
Forum Staff
Moderator
Probably something very simple.

After the first player object suffers a collision, what code is executed? You may be accidentally doing something to both player objects, instead of just one.
 
M

MrAmsel

Guest
here is the step event for the player 2 object

scr_getinputp2();
//React to inputs
var attack_id;
move = key_right - key_left;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_mass))
{
vsp = key_jump * -jumpspeed;
}

if(position_meeting(x,y,obj_hitbox))
{
global.p2hit = true
}

if (global.p2hit = true)
{
global.p2hp -= 1;
global.p2hit = false;
}

if (global.p2hp = 0)
{
instance_destroy();
global.p2hp = 30;
}

if(key_atk1)
{
attack_id = instance_create(x, y, attack_object);
attack_id.player_id = 2; // Set player_id to 1 for player 1; 2 for player 2, etc.
}

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

//Verticle Collision
if (place_meeting(x,y+vsp,obj_mass))
{
while (!place_meeting(x,y+sign(vsp),obj_mass))
{
y += sign(vsp);
}
vsp = 0;
}

y += vsp;


for the hitbox obj i just created an instance_destroy function in the post draw event so that it is destroyed after it is made.
 
M

MrAmsel

Guest
Thank you for the advice @Stubbjax, but I don't see how the place meeting function can be used to check for collision between two objects independent of the object with the place meeting function. Doesn't the place meeting function check for collisions between the object with the function inside of it only?
 
S

sndfnts

Guest
I don't know too much about how a game works at the low-level, but my guess is that you have something like this:

1. The instance order determines which object runs their code in a specific order. If Player1 is before Player2, then Player1 will run their code first before Player2 in the Step event (for example).

2. If the hitbox creation is done during the Step event of these players, then Player1 will have created their hitbox (and player2 will later get hit by that hitbox) after Player1 runs their Step code. However, when Player2 has created their hitbox, Player1's code had already finished (so player1 can't check for the hitbox).

I think that you should separate the hitbox creation so that all the hitboxes are made first (put in BeginStep), and check for collisions next (put in Step).
 
M

MrAmsel

Guest
@sndfnts That fixed the problem! Thank you so much, I didn't even consider using the other step events because I have never needed to work with them before. You're a life saver!
 
A

Aura

Guest
Since the issue's been solved, you'd perhaps want to set the status of the topic as solved. To do that, go to Edit Title/Thread Tools >> Edit Title >> Set the topic prefix to Solved.
 
Top