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

  • Home
  • SEARCH
  • 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 7692697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:54:44+00:00 2026-05-31T20:54:44+00:00

this is the second game that I’m having issues with in the same area…

  • 0

this is the second game that I’m having issues with in the same area…
the first one I don’t even know how I fixed it exactly. The coordinate system was also in doubles.

This is the “update” code for the physics class that will be inherited by all the objects in the world (players, enemies, items).
All these objects will have to be able to obey collisions with the world, so… when they have a xSpeed or ySpeed this part of the code will run to apply and “snap” the object into the correct space if it hits a solid (dubbed state 2) world tile. Each world tile is 20×20 pixels.

Problem:
It works perfectly fine except…

if you snap to the right tile, then you cannot move up or down. (though you can move left, and then move up or down)

if you snap to the bottom tile, then you cannot move left or right. (though you can move up and then move left or right)

    if(xSpeed<0){
        try{
            int dest = hitbox.x + (int)(xSpeed/20*tick);
            if(Ids.tiles[world[dest/20][hitbox.y/20]].getState() == 2 || Ids.tiles[world[dest/20][(hitbox.y+hitbox.width)/20]].getState() == 2){
                hitbox.x = (int)Math.ceil(dest/20.)*20;
                xSpeed = 0;
            }else{
                hitbox.x += (int)(xSpeed/20*tick);
            }
        }catch(Exception e){
            hitbox.x += (int)(xSpeed/20*tick);
        }
    }else if(xSpeed>0){
        try{
            int dest = hitbox.x+hitbox.width+(int)(xSpeed/20*tick);
            if(Ids.tiles[world[dest/20][hitbox.y/20]].getState() == 2 || Ids.tiles[world[dest/20][(hitbox.y + hitbox.width)/20]].getState() == 2){
                hitbox.x = dest/20*20 - hitbox.width;
                xSpeed = 0;
            }else{
                hitbox.x += (int)(xSpeed/20*tick);
            }
        }catch(Exception e){
            hitbox.x += (int)(xSpeed/20*tick);
        }
    }

    if(ySpeed<0){
        try{
            int dest =  hitbox.y + (int)(ySpeed/20*tick);
            if(Ids.tiles[world[hitbox.x/20][dest/20]].getState() == 2 || Ids.tiles[world[(hitbox.x + hitbox.width)/20][dest/20]].getState() == 2){
                hitbox.y = (int)Math.ceil(dest/20.)*20;
                ySpeed = 0;
            }else{
                hitbox.y +=  (int)(ySpeed/20*tick);;
            }
        }catch(Exception e){
            hitbox.y +=  (int)(ySpeed/20*tick);;
        }
    }else if(ySpeed>0){
        try{
            int dest = hitbox.y + hitbox.height +  (int)(ySpeed/20*tick);
            if(Ids.tiles[world[hitbox.x/20][dest/20]].getState() == 2 || Ids.tiles[world[(hitbox.x + hitbox.width)/20][dest/20]].getState() == 2){
                hitbox.y = dest/20*20 - hitbox.height;
                ySpeed = 0;
            }else{
                hitbox.y +=  (int)(ySpeed/20*tick);;
            }
        }catch(Exception e){
            hitbox.y +=  (int)(ySpeed/20*tick);;
        }
    }
  • 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-05-31T20:54:45+00:00Added an answer on May 31, 2026 at 8:54 pm

    Consider what happens on a right-edge (moving rightward) collision. This condition passes:

    Ids.tiles[world[dest/20][hitbox.y/20]].getState() == 2
    

    Suppose hitbox.width is 5. Then this will return true as soon as hitbox.x is 15. But you then reset hitbox.x to dest/20*20 - hitbox.width, which works out to be exactly where it already is. Then, in the vertical direction, if the object moves then its rightmost edge is already colliding with tiles, so it can’t move.

    To fix this, subtract 1 pixel from the hitbox size when using it to look up grid positions — before the division, of course.

    If you were working in floating-point coordinates, then you would use Math.ceil((hitbox.x + hitbox.width)/20.0) - 1 to produce a similar effect without having an arbitrary epsilon.

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

Sidebar

Related Questions

This is my second question on Bamboo ( My First One ). My understanding
I've got a Perl script that needs to execute another Perl script. This second
I've a application that must work after another application. This second application has a
I hope it's the heat, this is the second question today and it's one
This most be the second most simple rollover effect, still I don't find any
Since this is just my second game, I'm still a little unsure of how
I'm creating a mobile game that that has two parts. The first is slow,
I currently have a game loop that fires off about 20 times per second
I am programming an OpenGL game for the iPhone, and have this code that
There is one thing that bothers me, C++ function pointers. I'm asking this question

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.