Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Only checking for 2D collision that matters  (Read 2818 times)
0 Members and 1 Guest are viewing this topic.
Offline Quillion

Senior Newbie





« Posted 2010-04-12 17:31:54 »

Hello, I am a complete newb in this forum and in java gaming in general. Reason why I started java gaming is because JAVA IS THE FUTURE
ok so with that out of the way I have a question.
I have made simple 2D java game, and for collision detection I use AABB(bounding box) method, and it was working just fine, as long as the amount of objects I used was below 30. But now that my map got bigger, the method checks for collision of every single object with main character and its slowing down the gameplay greatly.
So I was wondering how to do collision detection with objects that are in close proximity of main character rather than all the objects on map.
If someone could guide me to a great collision detection tutorial, I would be ever so thankful.
All my collision detection knowledge came only from http://www.metanetsoftware.com/technique/tutorialA.html

Thanks in advance to all who help ^^
Offline SimonH
« Reply #1 - Posted 2010-04-12 17:38:02 »

Think buckets! Break your play area down into regions (or 'buckets') and keep a separate list of objects for each. That way you only have to check nearby buckets for collision, rather than every object in the game.

People make games and games make people
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2010-04-12 17:53:00 »

You want some kind of spatial tree, probably something like a quad tree.

Although 30 objects is nothing, you should be able to brute-force the collisions with 1000+ objects without breaking a sweat, so it sounds like you're doing something wrong. Try posting code so we can have a look at it.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Quillion

Senior Newbie





« Reply #3 - Posted 2010-04-12 17:58:43 »

This is my code
Its probably pretty crazy, hope you guys will understand it.
But yeah pretty much my for loop is too long

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
   public void move(Wall walls[]])
   {
      standing = false;
      for(int i = 0; i<walls.length; i++)
      {
         // sideways collision
        if(walls[i].getTopY()<this.getBottomY()-6 && walls[i].getBottomY()>this.getTopY())
         {
            if(walls[i].getRightX()>=this.getLeftX() && walls[i].getRightX()<=this.getLeftX()-dx)
            {
               //System.out.println("Hit from right (object to your left) on a wall");
              x = walls[i].getRightX()+5-leftOffset;
               dx = 0;
            }
            else if(walls[i].getLeftX()<=this.getRightX() && walls[i].getLeftX()>this.getRightX()-dx)
            {
               //System.out.println("Hit from left (object to your right) on a wall");
              x = walls[i].getLeftX()-5-image.getWidth(null)+rightOffset;
               dx = 0;
            }
         }
         //if statement for falling down and detecting collision from top or bottom of object
        if(walls[i].getLeftX()<this.getRightX() && walls[i].getRightX()>this.getLeftX())
         {
            // hitting a wall from top
           if(walls[i].getTopY()<this.getBottomY() && walls[i].getTopY()>this.getBottomY()-dy && dy>=0)
            {
               //System.out.println("Landed on a wall");
              standing = true;
               hit = false;
               y = walls[i].getTopY()-this.getHeight()+1+bottomOffset;
               dy = 0;
            }
            else if(walls[i].getBottomY()>=this.getTopY() && walls[i].getBottomY()<this.getTopY()-dy && dy<=0)
            {
               //System.out.println("Hitting head on ceiling of a wall");
              y = walls[i].getBottomY()-topOffset;
               dy = 0;
            }
            else if(walls[i].getTopY()<this.getBottomY() && walls[i].getTopY()>this.getTopY() && dy>=0)
            {
               //System.out.println("Standing");
              standing = true;
               hit = false;
               y = walls[i].getTopY()-this.getHeight()+1+bottomOffset;
               dy = 0;
            }
         }
      }
     
      if(!standing)
      {
         //System.out.println("Falling");
        dy = dy+GRAVITY;
      }
      y += dy;
      x += dx;
   }



And what did you mean by buckets SimonH?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #4 - Posted 2010-04-12 18:07:22 »

Ok, so you're representing your walls in a 1d array, when you say you're having slowdown with 30 objects, do you mean 30 walls or 30 objects? How big is your Wall[] array?

Your walls seem to be rectangles - are they positioned freely or are they tiles in a grid? Because if they're tiles you should use a 2d array so you can directly index into them. If not then a quad tree is probably a good choice, but it depends what your levels actually look like and what your collision shapes are likely to look like.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Quillion

Senior Newbie





« Reply #5 - Posted 2010-04-12 18:16:11 »

Yeah I meant 30 walls pretty much, anything beyond that shows a great slowdown. My wall is just an object which has an image, and x and y and width and height, and from there I get the rest. Also they are located freely, so I will probably end up using Quad trees.
By any chance would happen to have an example code or some explanation of how that might speed up things? Because I looked at it, and got the idea but I am still not understanding how its going to speed up the process. Because how I see it, is that quad tree would first check everything to see where I am located and then check the objects in that vicinity for collision, which still pretty much does what my for loop does. I mean probably by employing it, I will be able to go from 30 walls to maybe 60 walls without a slowdown, but is that the limit I will have to face? I mean how in the world do games like maple story (sorry for bad example) or mario have so many objects working all at once. I am pretty sure they got more than 60 objects.
Or would you suggest that I switch to the array method to speed things up.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2010-04-12 18:37:37 »

30 walls still sounds very low. Could you share the entire class that method is in to something like pastebin.com ?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Quillion

Senior Newbie





« Reply #7 - Posted 2010-04-12 18:52:52 »

Ok so this is my main character class

http://pastebin.com/1pzgGTVq

And this is my main class where I draw all the images, and as soon as I get more than 30 lag begins.

http://pastebin.com/0yvS3Nhs

And this is my Wall class just in case you were wondering

http://pastebin.com/mz6ra3Kd

Sorry I am off to write exam on java, so I will read this once I get back home in about 7 hours.

But thank you very much for all of your help Orangy Tang, you are awesome.
The only reason why I tried to tackle this project is  because I want to make a mario based game for my younger brother's birthday, but I hit a brick wall.
Thanks a lot again
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2010-04-12 19:02:14 »

After a quick look through I wonder if your cause of slowness is not the collision with the walls but actually drawing them. If you replace your g.drawImage(wall.getImage... with a g.fillRect, do you still get the same slowdown with 30 walls? If not then it's your drawing and you probably need to fix your image loading. Search for usage of 'createCompatibileImage' to load your sprites rather than using ImageIcon should do it.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #9 - Posted 2010-04-12 19:11:22 »

umm, coulndt u just check if something is within a certain number of pixels of it(to answer OP)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #10 - Posted 2010-04-12 19:41:51 »

I agree with everybody here already. I'm pretty positive that the collision is not your bottleneck - it's probably you're rendering. Do you know how to profile?

See my work:
OTC Software
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #11 - Posted 2010-04-12 21:59:09 »

Just comment out all the rendering except for a FPS counter.

Offline Quillion

Senior Newbie





« Reply #12 - Posted 2010-04-13 05:11:03 »

So which rendering method should I use?
I kinda threw it together, but having had no classes in my university that deal with drawing to screen, I do not know anything.
Thanks a lot to everyone's help, my brother will be very happy
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.

E.R. Fleming (15 views)
2014-07-29 03:07:13

E.R. Fleming (5 views)
2014-07-29 03:06:25

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

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

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

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

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

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

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

MustardPeter (44 views)
2014-07-16 23:30:00
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!