Ah, tell us what's printing out from your print statements as well next time (They're giving some important debug data there.)

Further, this code snippet doesn't tell us everything we need to know:How are you figuring out the center? Is it grid based, something else? What's the units for it? How does it equate to the number of pixels on the screen.

General advice: If you're allowing diagonal movement, separate the X intersections from the Y intersections, otherwise, you're liable to have the Xs become unreachable.

Well, I think I need to get rid of this moving when collision detected on Y-axis. In my game player will be able to change the gravity direction, so there shouldn't be any auto-move while standing on the ground.

Look at the second gif again.Using WASD I can change the gravity's direction. When player's rectangle collides with left or right wall it works well, doesn't move and stay near (not in) the wall. When gravity is changed on up or down, player's rectangle stops before wall, but it's automatically moving to right or left.You probably still don't understand me, so here's the "game":http://www.mediafire.com/?adrhfc1uw2vscfo

One think to try out if it is a general collision problem inside one of these methods or another problem is: Try to switch the calls to checkX() and checkY() so you call them in a differnent order.

Also one little thing: please rename some of your methods and variables. I don't quite understand what lvl_CenterX is, and I don't think this is the variable for describing the x mid of the whole level And I think setX() should be named moveX(). But all this is not a must do. Other then that this is really good code

Also one little thing: please rename some of your methods and variables. I don't quite understand what lvl_CenterX is, and I don't think this is the variable for describing the x mid of the whole level

lvl_CenterX (and Y) is a center X of every rectangle on the map. When we'll get rid of the problem I should do some refactoring.

Now I'm calling checkY() before checkX() and this is happening:

Upper and lower platform collision works well, but left-right makes player's rectangle... you see, it shouldn't be like that.

Sorry... I really have no Idea why this is happening... could you maybe post the code for the key handling? maybe there is somewhere a bug... any maybe also post where you do the checkX() and checkY().

You would benefit from having more debugging information on screen. You should use drawString to print the current direction of the gravity and maybe a few other variables or statistics like x velocity, y velocity, etc.

So, when the 'man' collides with the box-border on the right, the movement that follows (man starts to move towards bottom) indicates two things:1) x movement stops, because gravity and the pushback caused by the horizontal border collision code cancel each other out.2) y movement starts, AS IF the pushback from the vertical border collision code is giving the man a +y movement.

I'm guessing (2) happens again and again because with (1), the collision is triggered over and over each frame.

This is based on the assumption that during the demo action:(a) you NEVER change the gravity(b) the only things which move the man are gravity and the pushback when colliding with a wall.

I also observe that the movement, upon collision, is either up or down based on whether the man collides with the right wall above or below its midpoint, which adds to the evidence that the vertical movement is caused by the repeatedly triggered y-checks.

Looking at your "intersection" test: you seem to be using the same test for xIntersects and yIntersects:

1

game.man.getBounds().intersects(game.level1.getBounds(i));

The only way you might not be getting BOTH an x & y intersect = true each time would be if there were two lists of "bounding rectangles" -- one that can only result from a vertical and one that can only result a horizontal collision, and the segregated collision tests only test for intersection with the appropriate list of boundary boxes.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org