Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Trick-or-Treat: Collision Code for Game Maker

Greetings, fellow GameMaker enthusiasts and coding wizards! 🧙‍♂️✨

Today, we're delving into the arcane world of collision detection. In the spirit of Halloween, I've prepared a special treat for you, a very special version of the collision code I commonly use with GameMaker. But be wary, for there lies a trick—a bug I've cunningly concealed within the incantations of code for you to uncover and vanquish. Watch the video above for a full breakdown and the secret to the fix!

//Move the object based on the input X and Y velocity function move(deltaX, deltaY){
//If we are moving left or right (-1 or 1) 
var xDir = sign(deltaX) 
if (xDir != 0) { 
    //Check if our collision box is going to overlap our collision object 
    if (place_meeting(x + deltaX, y, obj_block)) { 
        //Check every pixel between us and the block to make sure we don't skip over anything 
        while(!place_meeting(x + xDir, y, obj_block)) { 
            //Move one pixel closer 
            x += xDir; 
        }
        //Stop our velocity 
        deltaX = 0; 
    } 
}
//If we are moving up or down (-1 or 1) 
var yDir = sign(deltaY) 
if (yDir != 0) { 
    //Check if our collision box is going to overlap our collision object 
    if (place_meeting(x, y + deltaY, obj_block)) { 
        //Check every pixel between us and the block to make sure we don't skip over anything 
        while(!place_meeting(x, y + yDir, obj_block)) { 
            //Move one pixel closer 
            y += yDir; 
        }
        //Stop our velocity 
        deltaY = 0; 
    } 
}
//Apply our leftover velocity to our x and y position 
x += deltaX; 
y += deltaY; 
}

Until our paths cross again in the ethereal plane of game development, Happy Coding and Happy Halloween! 🎃

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.