Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (563)
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 282 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Senior Newbie


Exp: 1 year



« 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

Senior Newbie


Medals: 1
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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void update(){
float oldX = entity.getX();
float oldY = entity.getY();

entity.setX(newPosition.x);
entity.setY(newPosition.y);

if(checkForCollision() == true){
entity.setX(oldX);
entity.setY(oldY);
}
}
Offline The Lion King
« Reply #2 - Posted 2014-07-10 08:32:58 »

1  
if(checkForCollision() == true)


1  
2  
3  
if(checkForCollision()){
...
}


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

Senior Newbie


Medals: 1
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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (16 views)
2014-09-12 09:08:26

Norakomi (50 views)
2014-09-10 13:57:51

TehJavaDev (58 views)
2014-09-10 06:39:09

Tekkerue (27 views)
2014-09-09 02:24:56

mitcheeb (50 views)
2014-09-08 06:06:29

BurntPizza (34 views)
2014-09-07 01:13:42

Longarmx (20 views)
2014-09-07 01:12:14

Longarmx (23 views)
2014-09-07 01:11:22

Longarmx (22 views)
2014-09-07 01:10:19

mitcheeb (31 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
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!