Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  2D collision detection - best approach?  (Read 2565 times)
0 Members and 1 Guest are viewing this topic.
Offline cheatsguy

Junior Devvie


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Posted 2012-12-09 17:36:59 »

Hello, everyone! I've come to a point that I'm stuck on, and I'm not sure how to continue from here. Currently I'm making a 2D side-scroller with large-ish sprites (64x64 up to 128x128) and I need a few forms of collision detection. I need at least one for collision with projectiles, and one for collision with environments. I know that pixel-perfect is time-consuming, so what would be the next-best thing?

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 143
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-12-09 17:40:51 »

Java2D? Or some other library? And why would you need two systems of detection?
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #2 - Posted 2012-12-09 17:47:10 »

I used regular Rectangle's for collisions with projectiles. Then some points around the character for environment-collisions. I wrote up an article about it here.

In my game, each sprite/entity then has an array of inner Rectangle's. So if, a projectile's Rectangle collides with the outer Rectangle of an entity, I check which of the inner Rectangles (if any) it is colliding with, and deal damage and so forth accordingly. Then I check for projectile-collisions with the terrain.

- Jonas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alesky

Junior Devvie


Medals: 3
Exp: 15 years


mmm....


« Reply #3 - Posted 2012-12-09 21:15:25 »


i have implemented a side scroller in 2D with Java2D look this is my piece of code

each entity extends an abstract class that has this methods implemented
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public Rectangle[] getEntityRectangle(){
      Rectangle rect = new Rectangle((int)(position.getPosX() - size.getHalfWidth()), (int)(position.getPosY() - size.getHalfHeigh()), size.getWidth(), size.getHeigh());
      return new Rectangle[]{rect};
   }

   public boolean collidedWith(Entity other) {
      Rectangle[] ownRect = getEntityRectangle();
      Rectangle[] otherRect = other.getEntityRectangle();      
     
      for (int i = 0; i < ownRect.length; i++) {
         for (int j = 0; j < otherRect.length; j++) {
            if(ownRect[i].intersects(otherRect[j])){
               return true;
            }
         }
      }
      return false;    
   }


if u look the implementation of the intersects method of the rect class you will see that i just a check between the basic variables of the two rect to chek,
for my there are not difference between proiectiles and environments, both are Entity so use this method.

Just for optimize and control the collision i have one list that contain all the shot of the enemy versus the player
and i verify the collision hire between player and enemy shot
and i have another list that contain all the environment and ther i verify the collision between player and environment elements
finally i have one for the enemy and i use it to verify player proiectiles versus enemy and enemy to player collisions


Offline cheatsguy

Junior Devvie


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #4 - Posted 2012-12-09 22:06:27 »

Java2D? Or some other library? And why would you need two systems of detection?

I'm using Slick2D, and I need one system for collision with the world (which uses tiles) and one for getting hit by other player's attacks. I was thinking of using rectangles overlapping certain parts of the body for attacks, but for collision with walls that doesn't make much sense... also, just to give you an idea of what the character looks like:

and when he's running:

(arms are rendered in-game)

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline cheatsguy

Junior Devvie


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #5 - Posted 2012-12-13 22:49:27 »

I used regular Rectangle's for collisions with projectiles. Then some points around the character for environment-collisions. I wrote up an article about it here.

In my game, each sprite/entity then has an array of inner Rectangle's. So if, a projectile's Rectangle collides with the outer Rectangle of an entity, I check which of the inner Rectangles (if any) it is colliding with, and deal damage and so forth accordingly. Then I check for projectile-collisions with the terrain.

The only problem with this is the actual size of the current character(s). Their legs will clip through walls if I make the hitbox too small, and will be able to stand on air if it's too big. How should I approach it in this case?

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2012-12-13 23:15:42 »

As I've also described in the article, I used just 1 point for the bottom, to get around the issue of standing in the air. I also wrote that I'd recommend that you use 2, like the Sonic-drawing. The spread between these two points is quite up to you, and leaves you with complete control of how much overlap you want to allow. So you won't have the problem you're describing.

You shouldn't factor in the legs when choosing a width for the collision rectangle. It won't matter much either way, though. People don't care whether 3 frames of an animation cut through the tiles of the map. And if you choose to factor in the entire width of the animation, it might leave the player being stopped unnaturally far from walls. Choose some median, or forget about the legs. People won't care either way. Even though you might Smiley

- Jonas
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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (101 views)
2014-11-29 21:32:03

toopeicgaming1999 (160 views)
2014-11-26 15:22:04

toopeicgaming1999 (163 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!