Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Collision Question  (Read 589 times)
0 Members and 2 Guests are viewing this topic.
Offline duce

JGO n00b
*

Posts: 47



« on: 2011-12-06 21:48:32 »

I have a player sprite that must collide with blocks in the world. However, I don't want to use a for loop to check every single block in the world every tick, so what can I do? My idea was to get the player's current position, and only check blocks around the player. Is this a good idea?
Offline gbeebe

Full Member
**

Posts: 145
Medals: 5



« Reply #1 on: 2011-12-06 21:54:39 »

That's basically what I'd do.  Get the player's position, subtract and then do small loops to check the tiles around the player...
1  
2  
3  
4  
5  
6  
7  
for (int searchX = playerX - 1; searchX == playerX + 1; searchX ++)
{
    for (int searchY = playerY - 1; searchY == playerY + 1; searchY ++)
    {
        //check for collision
   }
}
Offline Scarzzurs

Full Member
**

Posts: 124
Medals: 4



« Reply #2 on: 2011-12-07 02:18:36 »

I have a player sprite that must collide with blocks in the world. However, I don't want to use a for loop to check every single block in the world every tick, so what can I do? My idea was to get the player's current position, and only check blocks around the player. Is this a good idea?

This is definitely a good idea if you have many non-colliding objects and have profiled your application and found it to be slow in this area(!).
However for it to work, you must have some kind of data structure that allows efficient lookup of nearby tiles, for example a equality spaced grid.
In my game Hydro Hydra - 2nd Dive I have used such a grid to sacrifice some amount of memory in exchange of performance.

However I don't think I can stress this enough:
Unless you have no deadlines and do it for the fun of it, avoid premature optimization! :-)

- Scarzzurs

My games and Projects:
BlastingPixels.com,
Old website
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.095 seconds with 19 queries.