Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  [SOLVED] Platformer game, problem with collision detection calling  (Read 1032 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-09 16: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  Undecided

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."
Offline Axeman

Senior Member


Medals: 7



« Reply #1 - Posted 2013-05-10 01: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.
Offline alaslipknot
« Reply #2 - Posted 2013-05-10 02:21:42 »

thanx for answering
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."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Axeman

Senior Member


Medals: 7



« Reply #3 - Posted 2013-05-10 11: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.
Offline thebebinator

Senior Newbie


Medals: 1



« Reply #4 - Posted 2013-05-10 21: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.

Adam
Offline alaslipknot
« Reply #5 - Posted 2013-05-10 21: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 

any help please ?

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

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


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


« Reply #6 - Posted 2013-05-11 00: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.

Offline alaslipknot
« Reply #7 - Posted 2013-05-11 02: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.

 

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

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (20 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!