Understanding Collision Basics Pt.2

Yesterday I uploaded PART 1 of ‘Understanding Collision Basics’. In it, I explained how to handle simple collision with flat surfaces and slopes by iterating through single pixel translations.

Sub-Pixel Movement

As a few of you had mentioned, this approach does not support sub-pixel movement. What I mean by sub-pixel movement is that an object with a speed of 5.0, an object with a speed of 5.1, and an object with a speed of 5.9 will all appear to move at the same speed. Depending on the game you are making this may be ok, but I’d like to show you how to remove that crutch.

Here is an update to the collision code that incorporates sub-pixel movement.

'create event'
// Velocity
vx = 0;
vy = 0;

// Used for sub-pixel movement
cx = 0;
cy = 0;

onGround = OnGround();

You will notice that the only changes here are two new variables that will hold the rounded off values that accumulate from your velocity values.

'end step event'
var vxNew, vyNew;

// Handle sub-pixel movement
cx += vx;
cy += vy;
vxNew = round(cx);
vyNew = round(cy);
cx -= vxNew;
cy -= vyNew;

// Vertical
repeat(abs(vyNew)) {
    if (!place_meeting(x, y + sign(vyNew), oParSolid))
        y += sign(vyNew); 
    else {
        vy = 0;

// Horizontal
repeat(abs(vxNew)) {

    // Move up slope
    if (place_meeting(x + sign(vxNew), y, oParSolid) && !place_meeting(x + sign(vxNew), y - 1, oParSolid))
    // Move down slope
    if (!place_meeting(x + sign(vxNew), y, oParSolid) && !place_meeting(x + sign(vxNew), y + 1, oParSolid) && place_meeting(x + sign(vxNew), y + 2, oParSolid))

    if (!place_meeting(x + sign(vxNew), y, oParSolid))
        x += sign(vxNew); 
    else {
        vx = 0;

The basic idea is to store the sub-pixel value until the total accumulated is greater than one full pixel. During that frame, the object will move that extra pixel.

I’ll keep this one short and sweet! I don’t want to get burnt out now that I will be doing ‘somewhat daily’ DevLog posts.

Thanks again for all of the support,


Understanding Collision Basics Pt.2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s