[Tutorial] 2D Collisions For Any Game & Language

Started by
3 comments, last by Nicholas Kong 10 years, 4 months ago

Hey everyone! So I saw a lot of people asking how to make collisions and no one was really answering their questions with a solid answer and solution so I thought I would.

I whipped up this video really quickly to give you guys an example on how to implement 2D collisions very easily without any headaches that may come from it.

If any of you have any questions about how to implement it please comment below and also let me know if this has helped you because I would love to know! Hope you guys like it!

Video:

Advertisement

Why check a desired location beforehand rather than the current location of the player?

Just curious, I never done it your way before.

It seems like you created a bug when you sent the desired position by to the last location.

It is not a graphical error.

I am pretty sure the bug is below here. It seems like juggling too many variable states, right?

why are you changing desired back to the current position?

x = dx;

y = dy;

// why are you setting current position back to the last position?

lx = x;

ly = y;

Why check a desired location beforehand rather than the current location of the player?

Just curious, I never done it your way before.

It seems like you created a bug when you sent the desired position by to the last location.

It is not a graphical error.

I am pretty sure the bug is below here. It seems like juggling too many variable states, right?

why are you changing desired back to the current position?

x = dx;

y = dy;

// why are you setting current position back to the last position?

lx = x;

ly = y;

No it was a graphical error. If you don't believe me, try it with floats. And I think that you arent understanding the order in which code is executed. So first I set:


x = dx;
y = dy;

Then after that I set:


lx = x;
ly = y;

Which is the same as:


lx = dx;
ly = dy;

because I have already set the position variables equal to the desired variables. Might want to read up on some things before you start throwing false claims ;)

The video isn't bad so not to knock you down or anything.
However, 2D collision can be a bit more complex. Just consider moving vehicles colliding or an object falling. It all depends on what you are trying to achieve. If you are looking to move a mouse through a maze or character what you got will probably work fine.

Also your collision method seems to rely on moving +1 position in whatever direction. If the player was moving at a rate of 5 they would be left with a barrier around any object they collide with using your method. Upon a collision provided you are not showing bounce effects you would want the player to be moved to the closest distance to the object not left 4 or 5 space away.

Again the point isn't to knock you down but to emphasize 2D collsion detection can be a bit more complex than you are making it.

Why check a desired location beforehand rather than the current location of the player?

Just curious, I never done it your way before.

It seems like you created a bug when you sent the desired position by to the last location.

It is not a graphical error.

I am pretty sure the bug is below here. It seems like juggling too many variable states, right?

why are you changing desired back to the current position?

x = dx;

y = dy;

// why are you setting current position back to the last position?

lx = x;

ly = y;

No it was a graphical error. If you don't believe me, try it with floats. And I think that you arent understanding the order in which code is executed. So first I set:


x = dx;
y = dy;

Then after that I set:


lx = x;
ly = y;

Which is the same as:


lx = dx;
ly = dy;

because I have already set the position variables equal to the desired variables. Might want to read up on some things before you start throwing false claims ;)

I was merely questioning why you are checking a desired location when you can be doing it in a current location. I am not saying your code is wrong. I was curious about your approach. I am not making any false claims about anything...

Confused about why I was down-voted for my own curiosity...

Edit: I tested your code in my code base that is similar to yours and it can ruin an object initial position upon its creation in the game.

The bug will happen as long as you are checking collisions regardless if a rectangular intersections happens.

Basically when I tested your code, the position I created for my object was changed upon loading my game.

Strange how you did not encounter a bug like I did.

My code:

position.setX(dx);
position.setY(dy);
lx = (int) position.getX();
ly = (int) position.getY();

Your code:

x = dx;

y = dy;

lx = x;

ly = y;

This topic is closed to new replies.

Advertisement