Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  Collision Detection confusion  (Read 2590 times)
0 Members and 1 Guest are viewing this topic.
Offline glurion

Junior Newbie





« Posted 2009-12-22 21:42:24 »

Alright so first things first hi all Grin

I'm having some issues when it comes to collision detection, i understand the theory of that when objects touch the program will do something....that bits easy, so for a university assignment i've been given some skeleton code and asked to modify it to create a different game, the game i'm creating is similar to pacman (using same looking player), the character will be "eating" circles(munchies), so i've got all my code working up till collision detection, i'm having issues figuring where to place the call (CollisionDetection(); ) and also how to actually get the program to detect the collision and then delete the object.

So heres all of my code (in notepad cause it wouldn't fit all on here http://cid-3eb98c6f7b408c38.skydrive.live.com/self.aspx/Public/java%20pacman.txt)

just a few notes when the collision occur i want the monster to increase in size, and also increment the score int by 1, (these both work when for loop is implemented), oh and obviously delete the munchie which it collided with, the println is just for testing reasons.

Any help would be great.
thanks matt.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2009-12-22 22:00:51 »

well, for starters:

the if statement in the collision detection method is messed up because you cannot compare a single int to an array of them, you could do a for loop to go through them if you wanted, but this is nto the right way for collision anyways.


here is how to do collision:

you create 2 rectangles for the two entities, you then check if the rectangles intersect.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2009-12-22 22:55:40 »

Notepad? I hope you are using an IDE for development. If not, start. http://eclipse.org

As h3ckboy said, you have two shapes and you see if they overlap. If so, they are colliding. To test if rectangles A and B are colliding, if any vertex for rectangle A is inside rectangle B, they are colliding. So you would write a method isPointInRect and call it four times. You can also do circle-circle collision:
1  
boolean isColliding = (x2 - x1) * (x2 - x1)  + (y2 - y1) * (y2 - y1) < (radius1 + radius2) * (radius1 + radius2);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xyle
« Reply #3 - Posted 2009-12-23 01:30:50 »

Heres a collision method I found from somewhere around here and been using ever since.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
    public boolean collidesWith(Sprite tSprite){
      //Checks for collisions with another sprite
     private Rectangle me = new Rectangle();
      private Rectangle him = new Rectangle();
     
       me.setBounds((int)x, (int)y, this.getWidth(), this.getHeight());
       him.setBounds((int)tSprite.x, (int)tSprite.y, 50, 70);
       
       return me.intersects(him);
    }


This is a method inside my sprite class which defines the image, height, width, name and coordinates of a sprite in my games. Calling this method from the main class would return a true if colliding or false if not colliding.

it creates 1 rectangle using parameters within the calling sprite then creates a second rectangle based off the info of the second sprite. The method then uses the rectangle1.interects(rectangle2) method to see if they intersect.

I've been thinking about how to check if the mouse is over an object. Right now all I can come up with is to iterate through all objects and pass that object into the collidesWith() method to see if the mouse is over it or not. Not a very good routine, but once I hit that point I do more research.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2009-12-23 04:11:31 »

Xyle, only (minor) issue with your approach is it allocates two Rectangle objects every time the method is called.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2009-12-23 08:23:27 »

oh eyah and another way to make it more efficient you may want to check the distances before you ake the rectangle and check the overlap.

becasue if the sprite is 300 pixles away then you wouldnt ever collide with it.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2009-12-23 23:15:27 »

Checking the distance would be the same as just writing the intersects method yourself, no Rectangles needed. This is what I would recommend.

Offline Xyle
« Reply #7 - Posted 2009-12-24 01:52:03 »

Thanks for the input Nate,

The rectangles are actually created with the sprite, I declared them in the method just for this post without thinking. So each of my sprites contains a me and a him rectangle whos coords, height and width get changed to the parameters of the method when checking for collision.

1  
boolean isColliding = (x2 - x1) * (x2 - x1)  + (y2 - y1) * (y2 - y1) < (radius1 + radius2) * (radius1 + radius2);

This demonstrates your circle colliding check, but you mentioned the isPointInRect() method which you would need to call 4 times? Do you have an example?

h3ckBoy is on the right track. I was looking at implementing a routine to check if objects are on screen or near the user before I run through every object to see if there is a collision.

Thanks again.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline zoto

Senior Member


Medals: 4



« Reply #8 - Posted 2009-12-24 06:01:28 »

Letting java do it.
1  
2  
3  
boolean isPointInRect(Point point, Rectangle rect){
   return rect.contains(point);
}

manually.
1  
2  
3  
boolean isPointInRect(Point point, Rectangle rect){
   return point.X >= rect.X && point.X <= (rect.X + rect.Width) && point.Y >= rect.Y && point.Y <= (rect.Y + rect.Height);
}

But this way only need to be called once
1  
2  
3  
4  
5  
6  
7  
8  
boolean collides(Rectangle rect1, Rectangle rect2){
   int rect1MaxX = (rect1.X  + rect1.Width);
   int rect1MaxY = (rect1.Y  + rect1.Height);
   int rect2MaxX = (rect2.X  + rect2.Width);
   int rect2MaxY = (rect2.Y  + rect2.Height);
   return (rect1.X >= rect2.X && rect1.X <= rect2MaxX && rect1.Y >= rect2.Y && rect1.Y <= rect2MaxY) ||
          (rect1MaxX >= rect2.X && rect1MaxX <= rect2MaxX && rect1MaxY >= rect2.Y && rect1MaxY <= rect2MaxY);
}


This is all just from memory so please forgive any stupid mistakes.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #9 - Posted 2009-12-24 08:03:32 »

there is an actual intersects(Rectangle) method you know right?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Member


Medals: 4



« Reply #10 - Posted 2009-12-24 09:07:17 »

Yes, the method I actually use looks like:
1  
2  
3  
boolean collides(Sprite other){
   return getBounds().intersects(other.getBounds());
}


Offline h3ckboy

JGO Coder


Medals: 5



« Reply #11 - Posted 2009-12-24 09:13:12 »

Yes, the method I actually use looks like:
1  
2  
3  
boolean collides(Sprite other){
   return getBounds().intersects(other.getBounds());
}



yeah that is good.
Offline gouessej
« Reply #12 - Posted 2009-12-24 10:56:34 »

Xyle, only (minor) issue with your approach is it allocates two Rectangle objects every time the method is called.
Allocate them once for all and it solves this problem. Moreover, Java uses object pooling efficiently in such situations...

Personally, I use Xyle's approach for very minimalist 2D games. Maybe mixing bounding spheres and bounding rectangles would be better for a Pacman-like game.

For more complex 2D games (large levels with scrolling, etc...), I use spatial subdivisions (regular finite grid) and java.awt.Polygon (it is better for non spherical rotating objects) rather than Rectangle. I will release the source code of my GTA-like blue print if it can help someone.

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #13 - Posted 2009-12-24 12:19:44 »

Allocate them once for all and it solves this problem. Moreover, Java uses object pooling efficiently in such situations...
No, java isn't going to do anything to reuse your Rectangles. It is going to allocate the memory every time.

Offline Xyle
« Reply #14 - Posted 2009-12-25 02:03:16 »

Nice input!
No, java isn't going to do anything to reuse your Rectangles. It is going to allocate the memory every time.
This is what I do. Everytime I create a sprite, it has 1 me rectangle and 1 him rectangle for any ingame collision detection. The rectangles are created only once for each sprite used. So far its worked great.

Thanks for the examples Nate, something to ponder there!

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline glurion

Junior Newbie





« Reply #15 - Posted 2009-12-27 23:41:12 »

Thanks for all the input and i sort of understand what your saying but i have no idea how to implement it.

Notepad? I hope you are using an IDE for development. If not, start. http://eclipse.org

The notepad file was just to show the code, for this i'm using netbeans IDE, but we usually get told to write in notepad at uni
Offline zoto

Senior Member


Medals: 4



« Reply #16 - Posted 2009-12-28 03:57:03 »

I just looked over the posted code. The program approach is a very bad one but I'm guessing it's supposed to be for students that haven't been introduced to OOP yet. The way the munchies are implemented is as a few parallel arrays:
1  
2  
3  
    private  int[] munchieXPos;  // x coordinate of top left corner of each munchie image
   private int[] munchieYPos;   // y coordinate of top left corner of each munchie image
   private int[] munchieAnimationCount;  // current animation counter for each munchie

so if you wanted to access the first munchie(number 0) you could index all the arrays with that number.
munchieXPos[0]
munchieYPos[0]
munchieAnimationCount[0]
So your collision check would need to be changed to do that. Notice how other places in the code where munchies are checked the i counter variable for the loops are used to index the arrays.
1  
munchieXPos[i]
.
As far as removing the munchies The easiest way with that code would be to add a new boolean array to track the active state of the munchie, something like munchieActive[]. Make sure to initialize the array the same as the others are. Change the reset function to loop threw them all and set them to true, make sure they are only checked for collision and drawn if their active state is true, and is set to false when there is a collision.
 
Hope this helps, I'm purposely trying to not just post code as this is for an assignment and if you cant figure this out the later projects will become impossible.
Hope this helps.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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!