Jump to content
• Advertisement

# Another collision question

This topic is 1262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello,

I tried to make a top-down collision system, but it (again) didn't work... :(

The problem is, when I'm colliding from the left / right and then press up / down, it will correct the Y position, and the player would jump to the top or the bottom of the colliding rectangle.When I change the code to be X first, then the same problem happens for Y collisions.

Here's my code:

if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
player.y--;
dirY = 2;
} else if (dirY == 2) {
dirY = -1;
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
player.y++;
dirY = 3;
} else if (dirY == 3) {
dirY = -1;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
player.x--;
dirX = 0;
} else if (dirX == 0) {
dirX = -1;
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
player.x++;
dirX = 1;
} else if (dirX == 1) {
dirX = -1;
}

if (player.overlaps(rect)) {
switch (dirX) {
case 0: // left
player.x += ((rect.x + rect.width) - player.x);

break;
case 1: // right
player.x -= (player.x + player.width - rect.x);
break;
}
}

if (player.overlaps(rect)) {
switch (dirY) {
case 2: // down
player.y += rect.y + rect.height - player.y;
break;
case 3: // up
player.y -= player.y + player.height - rect.y;
break;
}
}



(I use Java and LibGDX, but the code should be understandable)

As you can see, collisions aren't my strongest skill.

If anyone could help me, I would greatly appreciate it.

MatejaS

#### Share this post

##### Share on other sites
Advertisement

Can anyone help me please? I'm stuck.. I've been debugging it for the last two hours and still have no luck...

#### Share this post

##### Share on other sites

Doesn't matter, solved it using pre-processing. Here's my new code, hopefully it'll help someone :)

if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
offY = -1.0f;
}

if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
offY = 1.0f;
}

if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
offX = -1.0f;
}

if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
offX = 1.0f;
}

Rectangle np = new Rectangle(player.x, player.y + offY, 12, 11);

if (np.overlaps(rect)) {
if (offY > 0.0f) {
offY -= np.y + player.height - rect.y;
} else if (offY < 0.0f) {
offY += rect.y + rect.height - np.y;
}
}

np.setPosition(player.x + offX, player.y);

if (np.overlaps(rect)) {
if (offX > 0.0f) {
offX -= np.x + player.width - rect.x;
} else if (offX < 0.0f) {
offX += rect.x + rect.width - np.x;
}
}

player.x += offX;
player.y += offY;


(You can optimize this by not creating new Rectangle every frame)

#### Share this post

##### Share on other sites
Try doing the collison check right after each movement.

#### Share this post

##### Share on other sites

Next step: use a vector math library. It will make the debug process a lot easier.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5
• Advertisement

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632660
• Total Posts
3007693
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!