• 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!

GML Platformer collision off a few pixels

D

Dylaniza

Guest
I used matharoo's basic platformer tutorial on The GameDev Palace. The tutorial used GameMaker: Studio and I used Studio 2, I don't know if this matters or not, but when you jump and hit the ceiling or walk to a wall it is off a few pixels in certain places but not in others. The code:

Code:
//Create Event
gravity = 1;

//Step Event
if keyboard_check(ord("A")) && place_free(x - 3,y){
    hspeed = -3;
}
else{
    if keyboard_check(ord("D")) && place_free(x + 3,y){
    hspeed = 3;
    }
else{
    hspeed = 0;
    }
}

if keyboard_check(vk_space) && !place_free(x,y+1){
    vspeed = -13;
}

//Collision Event
vspeed = 0;
I am pretty hopeless when it comes to platformers and collision but have been trying to learn and want to know why this might not be working.

 
Last edited:
T

TimothyAllen

Guest
Let's see if I can explain this. (Example)

Let's say your character is moving in the positive x direction and approaching a wall. Your characters right edge is at pixel 30 and the walls left edge at pixel 32. As you can probably see, your code will check 3 pixels to the right of the character and "see" the wall, which means your character will not move from its current position, leaving a 2 pixel gap.

Its an easy fix, but I'll let you attempt a fix now that you (might) understand the issue.

Edit if your not sure how place_free works, it basically places your characters mask at the given position and check for overlap.
 

jo-thijs

Member
I used matharoo's basic platformer tutorial on The GameDev Palace. The tutorial used GameMaker: Studio and I used Studio 2, I don't know if this matters or not, but when you jump and hit the ceiling or walk to a wall it is off a few pixels in certain places but not in others. The code:

Code:
//Create Event
gravity = 1;

//Step Event
if keyboard_check(ord("A")) && place_free(x - 3,y){
    hspeed = -3;
}
else{
    if keyboard_check(ord("D")) && place_free(x + 3,y){
    hspeed = 3;
    }
else{
    hspeed = 0;
    }
}

if keyboard_check(vk_space) && !place_free(x,y+1){
    vspeed = -13;
}

//Collision Event
vspeed = 0;
I am pretty hopeless when it comes to platformers and collision but have been trying to learn and want to know why this might not be working.

This is what happens:
Just before collision, the player instance is a couple of pixels below the ceiling.
The next step, vspeed is high enough to put the player inside the ceiling.
Because of this, the collision event of the player with the ceiling is triggered.
Just before executing your collision code, GameMaker notices that the ceiling is a solid object and decides to execute the following 2 lines for the player:
Code:
x = xprevious;
y = yprevious;
This teleports the player to its location of the previous step, a couple of pixels below the ceiling.
Then it executes your collision code:
Code:
vspeed = 0;
which makes the player slowly fall back from this position.

So in short, why doesn't the player touch the ceiling at collision, because you never told him to, you never used any code for this.

You can partially fix this by changing your collision event to:
Code:
if place_meeting(x, y + vspeed, other) {
    if vspeed < 0 {
        y += other.bbox_bottom + 1 - bbox_top;
    }
    if vspeed > 0 {
        y += other.bbox_top - bbox_bottom - 1;
    }
}

vspeed = 0;
To thoroughly fix this, you'll probably have to start the project over again.
Using GameMaker's solid system causes trouble.
Using hspeed and vspeed in a project that becomes as complex as this one causes trouble.
Not having well defined collision behavior, well defined shapes for walls, well defined positions for walls, etc. causes trouble.

EDIT: Got ninja'd.
 
D

Dylaniza

Guest
Thank you both for a reply, helped me understand a little more and had a go of coding this without following a tutorial. I got a few things working using my own variables for gravity and movement but collision was completely broken. I have followed other tutorials before and got a working player engine but I dislike not understanding the code I am writing. Going to leave making a platformer for a while and go back to making topdown stuff as I am able to code that without problem.(but if you have any good sites that might help me learn the logic for a platformer I would appreciate you sharing them.)
 
Top