Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Collision response disaster - what do?  (Read 560 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Junior Devvie

Exp: 3 years

« Posted 2014-07-10 00:21:31 »

So I've been coding off and on for about 2-3 years now. I've had this ongoing project that is a 2D game engine made with LWJGL. Its in a pretty finished state, but has a few quirks. One of those, happens to be collision response!

This is how I handle collisions between an entity and some other rectangle:
1. Check if the two rectangles (java.awt.Rectangle rectangles that is) are intersecting
2. Determine the position of the entity relative to the other rectangle (above, below, to the right, or to the left of it)
3. Set the velocity of the entity to move in the opposite of the other rectangle, thus correcting the collision.

If I move the entity straight at a wall using the above technique to handle the collision, this can result in the entity moving back and forth rapidly, even at high framerates, instead of appearing still. Sometimes one of the rectangles can even clip into the other, and be stuck (this happens especially on corners).

Obviously this system isn't sufficient, so what is a better way to do 2D collision response? Thank you!
Offline temu

Junior Devvie

Medals: 2
Exp: 2 years

« Reply #1 - Posted 2014-07-10 00:39:39 »

So I've been coding off and on for about 2-3 years now..

If you want to move your entity back when collision happens it's probably a good idea to post the piece of code where you handle the movement in case of collision. If instead you just want to avoid collision and don't let entity end rectangle intersect you can easily use this approach:

1) Update the entity position after have stored the precedent position
2) Check if a collision happens, if so you just change the entity position to its precedent value
3) If there are no collisions you just let the values as they are now and proceed to rendering

public void update(){
float oldX = entity.getX();
float oldY = entity.getY();


if(checkForCollision() == true){
Offline The Lion King
« Reply #2 - Posted 2014-07-10 08:32:58 »

if(checkForCollision() == true)


no need for the "== true".

"You have to want it more than you want to breath, then you will be successful"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline temu

Junior Devvie

Medals: 2
Exp: 2 years

« Reply #3 - Posted 2014-07-10 15:02:23 »

no need for the "== true".

I perfectly know that ç_ç But this cose is without any sort of comment so I wanted to make sure that checkForCollision() returns a true value in case of collision..
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (35 views)
2015-10-08 03:11:46

BurntPizza (18 views)
2015-10-08 00:30:40

BurntPizza (20 views)
2015-10-07 17:15:53

BurntPizza (33 views)
2015-10-07 02:11:23

KaiHH (38 views)
2015-10-06 20:22:20

KaiHH (16 views)
2015-10-06 19:41:59

BurntPizza (32 views)
2015-10-06 19:04:48

basil_ (48 views)
2015-09-30 17:04:40

shadowstryker (27 views)
2015-09-29 15:55:06

TheSpaceHedgehog (32 views)
2015-09-29 01:58:48
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!