Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (622) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [SOLVED] Platformer game, problem with collision detection calling  (Read 1537 times) 0 Members and 1 Guest are viewing this topic.
alaslipknot
 « Posted 2013-05-09 14:58:25 »

EDIT :
i changed the title from "A better way than AABB collision to solve precision problem" to this new one cause i found that my AABB method is working fine and the real problem is due when i call it

Hello,
i don't know if this is happening because of the collision method i wrote, or this is a common problem due of AABB use,
the main problem is precision, the collision doesn't always happens in the correct "time", for example in a platformer game, there is always those time when the player get under the "ground" a little bit, also when it comes to find from which direction the object it's colliding, i always found myself stacked

the method i use  :
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `public boolean aabb(double x1, double y1, double w1, double h1, double x2,         double y2, double w2, double h2) {      double cntrX1 = x1 + w1 / 2;      double cntrY1 = y1 + h1 / 2;      double cntrX2 = x2 + w2 / 2;      double cntrY2 = y2 + h2 / 2;      double distanceX = Math.abs(cntrX1 - cntrX2);      double distanceY = Math.abs(cntrY1 - cntrY2);      double sumWidth = (w1 / 2 + w2 / 2);      double sumHeight = (h1 / 2 + h2 / 2);      if (distanceX < sumWidth && distanceY < sumHeight ) {                     return true;               }      return false;   }`

the way i call it :
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31 `for (int y = 0; y < height; y++) {         for (int x = 0; x < width; x++) {            int blockX = x * 32;            int blockY = y * 32;            String block = myWorld[y][x];            // testing if hitting the ground from the buttom            if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),                  hero.getHeight(), blockX, blockY, 32, 32)) {               if (block.equals("1")) {                  onGround = true;               } else {                  onGround = false;               }            }            // testing if from the right            if (tool.aabb(hero.getX() + hero.getWidth(), hero.getY(),                  hero.getWidth(), hero.getHeight(), blockX, blockY, 32,                  32)) {               if (block.equals("4")) {                  System.out.println("right");                  draw.fillRect(hero.getX() + hero.getWidth() / 2,                        hero.getY(), 5, 5);               }            }         }      }`

what am trying to do is :
-checking if the bottom of the player is colliding with the ground which cause him to stop falling (done)
-checking if the upper of the player is colliding with  the ground which can stop the jump
-checking if the player is colliding from left/right with the ground/wall which will stop the movement in the current direction
PS :
i saw demos about jBox2D, do you suggest that i start using it to solve problems like these or is it too advanced for me now ??

thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Axeman

Senior Devvie

Medals: 7

 « Reply #1 - Posted 2013-05-09 23:23:02 »

Let´s say we have object A and B. If object A is 1 pixel away from object B and the speed of A is 5 pixels per frame you will only get a "true" from the collision check when A is overlapping with 4 pixels. What you want to do is to correct the overlap by the minimum amount. I wrote a bit about this in your last thread about collision detection so I won´t repeat myself, but read it and hopefully you´ll get some ideas.
alaslipknot
 « Reply #2 - Posted 2013-05-10 00:21:42 »

the problem seems to be more (or less) than that, please take a look to these pictures .
this one is a nice working collision (after failed with making AABB collision with the whole player quad, i said maybe using different rectangles for each side would help )

and this when the collision doesn't works

notice that here am not trying to make collision from left/right sides, actually i already done this and it works perfecty, my problem is when it collides with the ground, see when am in this state :

"onGround" is returning true
but when am in this state (i paused the game) :

"onGround" is returning false

i also tried to use 2 quads on the buttom one for the right and other for the left, but the results is always the same, and sometimes it returns true and false immediately and it still doesn't works...
that's why am trying to improve the collision method.

thanx alot

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Axeman

Senior Devvie

Medals: 7

 « Reply #3 - Posted 2013-05-10 09:32:45 »

I think you´re complicating it and thereby introducing more bugs that´s gonna be harder to debug. Try to keep it simple. If you need two rectangles and two AABB you´re doing it wrong and should go back to the drawing board. For example, you shouldn´t need to do two intersection test since you can get all the information you need from one test. In your case you check which axis has the smallest overlap and then the collision will occur on that axis.
thebebinator

Senior Newbie

Medals: 1

 « Reply #4 - Posted 2013-05-10 19:46:17 »

One thing I found that helped with my collision detection, is that if the rectangle moves more than half it's smallest dimension, I would split the moves into parts that equaled less than half that distance. So if the rectangle is (WxL) 10x10, and it moved 50 units that cycle, then I would split it up into 10 smaller moves, and run a collision detection on each one. One problem with that is that it does increase the amount of calculations considerably, but it lets you catch cases where you get too far past the bounds before catching it. I'm not sure if this answers your question exactly, but might help out a bit.

alaslipknot
 « Reply #5 - Posted 2013-05-10 19:54:17 »

thank you all for your help but i think the real trouble is not because of the collision method but is because of the way i called it,
this code
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `for (int y = 0; y < height; y++) {         for (int x = 0; x < width; x++) {            int blockX = x * 32;            int blockY = y * 32;            String block = myWorld[y][x];            // testing if hitting the ground from the buttom            if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),                  hero.getHeight(), blockX, blockY, 32, 32)) {               if (block.equals("1")) {                  onGround = true;               } else {                  onGround = false;               }            }`

which mean :
"if the rectangle is colliding with any of the grid rectangle then, check if the grid rectangle is a ground, if it is then stop moving, but if it's not a ground, then you can move "
now the problem happens when the rectangle is colliding with a ground grid and another type of the grid, it gives true and false in the same time, the strange thing is that it works if am on the left edge of the ground grid but if am on the right then the player keep falling

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

Not a glitch. Just have a lil' pixelexia...

 « Reply #6 - Posted 2013-05-10 22:01:25 »

It is an interesting problem alaslipknot. But the way to solve it is just to always collide based on the true variable. As long as a collision is happening, you should be reacting to it. Only when there are absolutely no collisions happening should you actually fall back to gravity.

alaslipknot
 « Reply #7 - Posted 2013-05-11 00:38:49 »

It is an interesting problem alaslipknot. But the way to solve it is just to always collide based on the true variable. As long as a collision is happening, you should be reacting to it. Only when there are absolutely no collisions happening should you actually fall back to gravity.
THANK YOU !! you know that moment when ...

all i had to do is make all the "else" result before the for loop, so it's always falling unless it collides with the correct grid block

thank you all for your help

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (27 views) 2015-04-23 03:42:11 theagentd (29 views) 2015-04-22 16:23:07 Riven (44 views) 2015-04-16 10:48:47 Duke0200 (52 views) 2015-04-16 01:59:01 Fairy Tailz (38 views) 2015-04-14 20:13:12 Riven (41 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (61 views) 2015-04-10 00:51:04 BurntPizza (60 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 30x BurntPizza 16x wessles 15x kingroka123 11x 65K 11x Rayvolution 11x alwex 11x KevinWorkman 9x kevglass 8x phu004 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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