• 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 Colliding timing issues.

S

Swork1

Guest
I am trying to have 2 blocks able to move right and left without colliding through each other when hitting a wall. The problem is whenever I move right, the left block wont move unless I hit the right key again which leaves a gap between the 2 blocks. (which I don't want).

Starting Position


If I Move Right From the Starting Position


If I Move Left From the Starting Position



Create
Code:
hsp = 0;
vsp = 0;
grv = 0;
walksp = 96;
Step
Code:
//Get Player Input
key_left = keyboard_check_pressed(vk_left);
key_right = keyboard_check_pressed(vk_right);
key_down = keyboard_check(vk_down);

//Calculates Movement
var move = key_right - key_left;

hsp = move * walksp;

//Horizontal Collison
if (place_meeting(x+hsp,y,ObjectBoundry))
{
    hsp = 0;
}

if(place_meeting(x+hsp,y,ObjectBlock_1))
{
    hsp = 0;
}

x = x + hsp;
 

toxigames

Member
My guess is that the left block object is first in the step order. This could mean that if the distance between the two objects are <= hps then the code if(place_meeting(x+hsp,y,ObjectBlock_1)) will return true when run in the left object (because the right object was too close so place_meeting found a collision), thus setting it's hsp to 0, thus the left object will not move. But in the right object the code if(place_meeting(x+hsp,y,ObjectBlock_1)) will return false because of course there is no object to the right of it, the left object is to the left. Thus its hsp variabel will not be set to 0, and the right object will move. Then if you press the vk_right key again, the left object can move because the right object had moved away so place_meeting check will return false and hsp wont be set to 0.

The left block being first in the step order would also explain why there is no issue when moving left; the left object will have moved away first and then the right object is free to move to the left.
 
Last edited:

toxigames

Member
So how to solve this? Maybe you could use a controller object for the move and collision code, to make sure the checks and moves are done in the same predictable and controllable manner.
 
S

Swork1

Guest
Thanks for the in depth response. Would you mind explaining how to create a controller object?
 

toxigames

Member
sorry I couldn't answer before I have just been real busy. A controller object is like any other object. What I mean by "controller" is simply that you control other objects and variables with it. Think of it as a sort of "command post". So in this controller object you put the code that control the movement of the blocks and the collision checks; instead of doing it in each object.
 
S

Swork1

Guest
Making a controller object doesn't seem to fix it. It still seems like its loading the left block before the right when i want them to move at exactly the same time
 

toxigames

Member
Hey, sorry again for not replying sooner. With the code you have in place now I am thinking that to make it work you would have to make an additional collision check in an end step in case there was a collision in the first check that prevented the block from moving. For example the left block saw collision with the right block, therefore left object didn't move. Then the right block moved, but now there is room for the left block to move. You then will need to catch that fact, and you can do it by making the left object check for a collision again (for example by making a final collision check in the end step, that will guarantee all blocks have made their first collision check and potential move), and so if there is now no collision in the end step, then it can move. Now I have no idea whether this is actually a good idea to go down this path of doing collision checking twice, or if you should rewrite an entirely new script. But with the current code that is how I perceive a fix that could make it work.

Technically you don't need to use a controller object, it's just what I would do to keep things centralized. Also you might consider parenting too for these blocks. If you're new to programming in general I would suggest reading up on parenting in the Gamemaker manual.
 
Top