Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  Tile Map & collisions  (Read 3498 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2009-01-22 15:43:46 »

ok now, I'm developing a 2D platformer (e.g. super mario) like castlevania actually
ANYWAY

I'm having problems with performance for the following reason:

A map in my game has like 5 layers, for different purposes, and visual effects.

One layer is like everything the player cannot go through, I call it structures.

So in order to ensure you cannot walk through structures, I naturally check if a tile collides with the player.
I do this by using Rectangle.intersects(Rect...)

ok now for the problem

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public boolean collide(Rectangle in) 
   {
      for (int y=0; y<height; y++)
        {
           for (int x=0; x<width; x++)
           {                          
            if (tiles[x][y] != null)
              {
                 if (in.intersects(new Rectangle((x*TILE_SIZE),(y*TILE_SIZE),(int)tiles[x][y].getPic().getWidth(),(int)tiles[x][y].getPic().getHeight())))
                 {
                    return true;
                 }
              }
           }
        }
     return false;
   }


Now this works fine, but the problem is: when the map gets bigger and bigger

1  
2  
3  
4  
5  
6  
for (int y=0; y<height; y++)
        {
           for (int x=0; x<width; x++)
           {                          
            if (tiles[x][y] != null)
              {


this is done more often and often, and it slows the game down extremely, because I do this several times a frame for every object (player or enemies), naturally

I mean... I cannot check weather or not a tile collides with my player without doing a loop like this with every tile ?!

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2009-01-22 17:32:19 »



white rect=player bounding box
black dots=corners of that bbox
red=the tiles you have to check

Edit:
You know where the player's sprite is, you know the size of it... hence you know the location of those 4 corners, you know the size of the cells... hence you know where each of those two loops has to start and stop (red tiles).

弾幕 ☆ @mahonnaiseblog
Offline Cero
« Reply #2 - Posted 2009-01-23 12:07:49 »

ok works excellent =)

btw: my project needs already more heap size than the default (64M ?)
is it dangerous if the system needs too many heap ?

I mean I cannot even understand why, because all the content that is loaded is maybe 15 mb at max atm =0

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

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2009-01-23 18:06:24 »

Images take width*height*3 bytes or width*height*4 bytes (if there is transparency involved) of RAM. But even opaque images may take the full width*height*4 bytes of RAM in order to be aligned. A 512x512 image will typically take 1mb of RAM.

Audio takes about 10mb per minute (if you don't stream it).

2*2*44,100*60=10,584,000 (in 60 seconds there are 41000 samples taking 16 bit [2 byte] each for both of those 2 channels)

弾幕 ☆ @mahonnaiseblog
Offline lhkbob

JGO Knight


Medals: 32



« Reply #4 - Posted 2009-01-23 22:32:21 »

In your collide() method, you're creating a lot of unnecessary Rectangle objects.  You should be able to have one Rectangle object for the class, and in the loop, just set the dimensions before calling intersects().  I'm pretty sure Rectangle has methods like setWidth(), setHeight(), setX() etc. 

Offline Cero
« Reply #5 - Posted 2009-01-24 14:50:44 »

Images take width*height*3 bytes or width*height*4 bytes (if there is transparency involved) of RAM. But even opaque images may take the full width*height*4 bytes of RAM in order to be aligned. A 512x512 image will typically take 1mb of RAM.

Audio takes about 10mb per minute (if you don't stream it).

2*2*44,100*60=10,584,000 (in 60 seconds there are 41000 samples taking 16 bit [2 byte] each for both of those 2 channels)

ok then it makes sense, since I've got a lot of transparent png's


ya and thanks for the rectangle tip, I never know if it's really that bad to create new variables in loops and stuff.
kinda need a easy way to measure how long some lines take...
of course i can get nanotime and stuff, but its a hassle =P

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2009-01-24 18:05:22 »

Use a profiler. Netbeans got a very nice one for example.

弾幕 ☆ @mahonnaiseblog
Offline Cero
« Reply #7 - Posted 2009-01-24 20:51:36 »

ya maybe later
I'm not using a mainstream java editor right now

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.

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

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

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

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

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

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

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

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

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
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!