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 for squares  (Read 1395 times)
0 Members and 1 Guest are viewing this topic.
Offline Miguel Atienza

Junior Newbie




Miguel lo spagnolo


« Posted 2005-12-19 14:34:58 »

Hi, I need help to implement collision detection:

I've 2 squares with (x1,y1) and (x2,y2) coordinates.

What kind of control I've to do??
Offline bauerr

Junior Member




Java!!!!!!!!!!! !!!


« Reply #1 - Posted 2005-12-19 15:06:39 »

Have a look at the API:
class: Rectangle.
method: intersects(..).

This should do the job.

Regards

Ralf
Offline Miguel Atienza

Junior Newbie




Miguel lo spagnolo


« Reply #2 - Posted 2005-12-19 15:45:41 »

I can't use the class Rectangle.. but where I can look the implementation of intersects?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2005-12-19 15:53:10 »

Download the Java SDK, or JDK or whatever they're calling it now.  It'll include the source code and then you can look at the Rectangle class and see how they've written the intersects() method.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2005-12-19 17:57:24 »

I think that when one is doing collision-detection, one should be able to figure how to do this...

1  
2  
3  
4  
5  
6  
7  
8  
if(
   1 of 4 corners of rect A is inside area of B
   ||
   1 of 4 corners of rect B is inside area of A
  )
{
   // collision
}


With a dozen ways to optimize the above Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline g666

Junior Member





« Reply #5 - Posted 2005-12-19 20:51:59 »

Check for non-intersection on each axis. eg for the x asis;

if(s1.x+s1.w < s2.x || s1.x > s2.x+s2.width)
  return false;

works for me anyway.


desperately seeking sanity
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #6 - Posted 2005-12-25 09:29:38 »

Here is something I hacked up this morning around 4am.
It works really well. Smiley

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  
public boolean hasCollided(Entity entity, Entity entity2) {
      boolean collisionOccured = false;
     
      tmpPoint.setLocation(entity.getX(), entity.getY());
      collisionOccured = isInside(tmpPoint, entity2);
     
      tmpPoint.setLocation(entity.getX() + entity.getWidth(), entity.getY());
      collisionOccured |= isInside(tmpPoint, entity2);
     
      tmpPoint.setLocation(entity.getX() + entity.getWidth(), entity.getY() + entity.getHeight());
      collisionOccured |= isInside(tmpPoint, entity2);
     
      tmpPoint.setLocation(entity.getX(), entity.getY() + entity.getHeight());
      collisionOccured |= isInside(tmpPoint, entity2);
   
      if(collisionOccured)
         vecPoint.setLocation(entity2.getX() - entity.getX(), entity2.getY() - entity.getY());
     
      return collisionOccured;
   }
   
   public boolean isInside(Point point, Entity entity) {
      boolean isInside = false;
     
      isInside = (point.x >= entity.getX()) && (point.x <= (entity.getX() + entity.getWidth()));
      isInside &= (point.y >= entity.getY()) && (point.y <= (entity.getY() + entity.getHeight()));
     
      return isInside;
   }

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline g666

Junior Member





« Reply #7 - Posted 2005-12-25 21:48:17 »

If you only check for wether any vertices interesect the other rect you will miss some situations. Also do you have to use point object instead of primitives!

desperately seeking sanity
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #8 - Posted 2005-12-25 22:51:42 »

I think that when one is doing collision-detection, one should be able to figure how to do this...

1  
2  
3  
4  
5  
6  
7  
8  
if(
   1 of 4 corners of rect A is inside area of B
   ||
   1 of 4 corners of rect B is inside area of A
  )
{
   // collision
}


With a dozen ways to optimize the above Smiley

Uhm... no. Say both rects overlap in the middle like some cross. None of the corners would be inside but they would still collide.

弾幕 ☆ @mahonnaiseblog
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #9 - Posted 2005-12-26 06:19:12 »

If you only check for wether any vertices interesect the other rect you will miss some situations. Also do you have to use point object instead of primitives!

I've tested extensively and haven't found that to be the case.
Objects aren't being created everytime they are passed, so to keep things clean I use points.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
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!