Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:13:54+00:00 2026-06-13T12:13:54+00:00

I am making a platform game, but i have a problem with my collision

  • 0

I am making a platform game, but i have a problem with my collision detection. I’ve made a function that draws a tile on the screen/map. In that function is my collision detection, it works fine when only one tile is drawn, but when i created “stairs” with three tiles, the first tile doesnt work propertly. The player is just being “pushed” up on the tile. The side detection isn’t working. And the other tiles are working just fine.

Here is the code for collision detection and tile drawing:

//Function that allows you to draw a tile
function drawTile(type, x, y, collision){
    var tileImg = new Image();
    tileImg.onload = function(){
        ctx.drawImage(tileImg, x, y)
    };
    tileImg.src = "images/" + type + ".png";

    if (collision){
        //Worst collision detection ever.
        if((player_x + player_width == x) && (player_y + player_height > y)){
            canMoveRight = false;
        }else if((player_x == x + 32) && (player_y + player_height > y)){
            canMoveLeft = false;
        }else if((player_y + player_height > y) && (player_x + player_width >= x) && (player_x + player_width <= x + 64)){
            player_y = y - player_height;
        }else{
            canMoveRight = true;
            canMoveLeft = true;
        }
    }
}

//Draw the map
function drawMap(){
    drawTile("block", 96, 208, true);
    drawTile("block", 128, 208, true);
    drawTile("block", 128, 176, true);
};

As you can see, the collision detection code kinda sucks. So it also would be good if you showed me better ways of making it.

Just say if you need to know something. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:13:55+00:00Added an answer on June 13, 2026 at 12:13 pm

    With box collision detection, it’s important to compare the x and y separately. Here’s the pseudo code for how I’ve done something similar the past:

    var o1 = {x:100, y:229, w:30, h:30};
    var o2 = {x:100, y:200, w:30, h:30};
    
    //Amount of overlap
    leftDist    = (o2.x - o2.w/2) - (o1.x + o1.w/2);
    rightDist   = (o1.x - o1.w/2) - (o2.x + o2.w/2);
    topDist     = (o2.y - o2.h/2) - (o1.y + o1.h/2);
    bottomDist  = (o1.y - o1.h/2) - (o2.y + o2.h/2);
    
    
    if( leftDist    < 0 &&
        rightDist   < 0 &&
        topDist     < 0 &&
        bottomDist  < 0 ){
    
        //Get the closest collision
        var closest;
        var direction; //0 = left, 1 = right, 2 = top, 3 = bottom
    
    
        var xDist = o1.x - o2.x;
        var yDist = o1.y - o2.y;
    
        if(xDist < 0) {
            closest = leftDist;
            direction = 0;
        } else {
            closest = rightDist;
            direction = 1;
        }
    
    
        if(yDist < 0) {
            if(closest < yDist) {
                closest = topDist;
                direction = 2;
            }
        } else {
            if(closest < yDist) {
                closest = bottomDist;
                direction = 3;
            }
        }
    
    
    
        //Last, jump to the contact position
        switch(direction) {
    
            case 0:
                o1.x += closest;
                break;
            case 1:
                o1.x -= closest;
                break;
            case 2:
                o1.y += closest;
                break;
            case 3:
                o1.y -= closest;
                break;
    
        }
    
    
    }
    

    Let me know if you have any questions about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a basic platform game for the iPhone and I have encountered
im making a platform type of game. I made my main character a rectangle
I'm making a game for the Android platform and I need to access some
I have a conceptual question... I am making an Intranet application (Web platform) for
I'm making a simple 2d game for the android platform, which works perfectly from
I'm making a game that will allow content development and I'd like it to
I am writing a simple platform game, and I've found that when removing 'ghost'
I am making a 2D game for the Android platform. The character is supposed
I am making a game in AS3 / Adobe AIR that requires the user
I'm interested in making a cross-platform Facebook, but non-browser based application. Therefor I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.