• 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] Object collision from object's step event

R

Raff

Guest
Hello,

I have the following code in the player step event that works:

Code:
if(moverY > 0)
{
    var col1 = instance_position(x, bbox_bottom+moverY, ob_block);
    var col2 = instance_position(bbox_left, bbox_bottom+moverY, ob_block);
    var col3 = instance_position(bbox_right, bbox_bottom+moverY, ob_block);
                
    var col = 0;
    var colX = 0;
    if(col1)
    {
        col = col1;
        colX = x;
    }
    if(col2)
    {
        col = col2;
        colX = bbox_left;
    }
    if(col3)
    {
        col = col3;
        colX = bbox_right;
    }
                    
    if(col)
    {
    while(!instance_position(colX, bbox_bottom+sign(moverY), col))
    {
        y += sign(moverY);
    }

    if(bbox_bottom < col.bbox_top)
    {
        moverY = 0;
        if(state == states.falling) || (state == states.jumping || state == states.pillando)
        {
            image_index = 0;
            if(keyDown)
            {
                state = states.crouched;
            }
            else
            {
                state = states.normal;
            }
        }
    }
    if(state == states.attackAir) state = states.attack;
}
But i wanted to try it in the object step event, so i tried this:

Code:
if(ob_prota1Idle.moverY > 0)
{
    var col = instance_position(x, bbox_top-ob_prota1Idle.moverY, ob_prota1Idle);
    if(col)
    {
        while(!instance_position(x, bbox_top-sign(ob_prota1Idle.moverY), ob_prota1Idle))
        {
            ob_prota1Idle.y += sign(ob_prota1Idle.moverY);
        }

        if(ob_prota1Idle.bbox_bottom < bbox_top)
        {
            ob_prota1Idle.moverY = 0;
            if(ob_prota1Idle.state == states.falling) || (ob_prota1Idle.state == states.jumping) || (ob_prota1Idle.state == states.pillando)
            {
                ob_prota1Idle.image_index = 0;
                if(ob_prota1Idle.keyDown)
                {
                    ob_prota1Idle.state = states.crouched;
                }
                else
                {
                    ob_prota1Idle.state = states.normal;
                }
            }
        }
        if(ob_prota1Idle.state == states.attackAir) ob_prota1Idle.state = states.attack;
    }
}
ob_prota1Idle is my player and ob_block is the object. Can someone, please, tell me what am i doing wrong ? Thankyou in advance.
 

HayManMarc

Member
You should name your variables better. With only a few extra key strokes, you (and anyone else) will be able to read and understand the code a thousand times easier.
 
R

Raff

Guest
Sorry, but not understanding what you say. I have a player-object vertical collision working on the player step event and i want it in the object step event instead. Sorry if i misunderstand your reply, english is not my languaje, if you need some other piece of code or something else, please, let me know, thankyou.
 
R

Raff

Guest
Sorry, some body knows how to have that code working on ob_block step event instead of in player step event ?
 
This may have worked when done in your player object, because only one object (the player) was testing the conditions.

Which part is it not doing when you do it in the objects step event?
 
R

Raff

Guest
In the objects step, the player just pass through the object with, apparently, not collision. I say "apparently" because it collides sometimes just a little bit and passes through anyway. Can't figure how to solve it.
 

HayManMarc

Member
What is "moverY"? What is "col", "colX", "col1", "col2", "col3"? Please define these variables so we can understand what you're doing.
 
R

Raff

Guest
"moverY" is "y" velocity,"col1", "col2", "col3" are to check the collision because ob_block is 32 x 32 pixels and player is 160 pixels height and 96 pixels width, so "col1" stores the collision with the center of the player, "col2" stores the collision with "bbox_left" and "col3" stores the collision with "bbox_right". "col" stores "col1" or "col2" or "col3" to check in the "while" loop. " colX" stores "x" , "bbox_left" or "bbox_right" to check in the "while" loop.
 

HayManMarc

Member
Ok, I've been stufying your code and I think I see what you're doing. You're moving the player up or down on the screen and looking for collisions with the object "ob_block", correct?

Actually, I don't think it's a good idea to be controlling the movement of your player in the code of the object it's colliding with. It would be better to make a parent object for your different block objects and use the parent for collision detection in your player object.

But I'm still having trouble wrapping my brain around what you are doing. Wouldn't it be easier to just use instance_place and not worry about bounding boxes at all?
 
R

Raff

Guest
Well, the game is a platformer. I read somewhere that is better to take collision from the object parent because it has to check only with the player, if i check from the player it has to check all the platforms. But it's a try. I have it working in the player's step event. Just want to try in the object's step event.
 

TheouAegis

Member
Well, the game is a platformer. I read somewhere that is better to take collision from the object parent because it has to check only with the player, if i check from the player it has to check all the platforms. But it's a try. I have it working in the player's step event. Just want to try in the object's step event.
Either all objects check the player or the player checks all objects. It's still the same.
 
R

Raff

Guest
Ah !, so doesn't matter. Ok , thankyou for your time. I have it working then. Tanks everybody again!.
 
Top