Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  Chunks and Collisions  (Read 1030 times)
0 Members and 1 Guest are viewing this topic.
Offline Geemili

Junior Member


Projects: 1


No Games Finished


« Posted 2012-02-20 00:52:52 »

I am making a game, and I am loading it in chunks. AKA: I load only 15 320*240 pixel parts of the world at the time (20*15 blocks). My only problems are collisions. They work properly only in the chunk (0, 0). Chunks in the same quadrant have a weird offset, like being 1 block above where it should be. In the other quadrants, it doesn't work at all, because the collisions keep going to -1, which is out of bounds for my grid.

Here is my code, it is designed to take world coordinates and turn them into a chunk coordinate and then a chunk's block coordinate:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public Block getBlock(float x, float y)
   {
      int chunkX = (int)( (x-(x%320))/320 );
      int chunkY = (int)( (y-(y%240))/240 );
      Chunk chunk = getChunk(chunkX, chunkY);
     
      int blockX = (int)( x-(chunk.getX()*320) );
      int blockY = (int)( y-(chunk.getY()*240) );
      return chunk.getBlock(blockX, blockY);
   }


I want to know why it isn't working, the code seems perfectly sound, and the results a little random. It would be a great help if you knew why, and told me!
Offline Beamery

Senior Newbie


Medals: 1



« Reply #1 - Posted 2012-02-20 05:24:28 »

It could be because you're doing a modulus on a floating point number. Since floats aren't stored exactly, it could lead to slight variations in results compared to what you would expect.  This might explain the weird off by one errors.
Offline DiEvAl

Junior Newbie





« Reply #2 - Posted 2012-02-23 07:51:45 »

When x is negative, x%320 will be negative too. Because of this, when -320 < x < 320, chunkX = 0, so this chunk is 640 blocks wide. Is it intentional?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #3 - Posted 2012-02-23 08:18:50 »

It seems like the whole operation of (x - (x %320) / 320) could be removed and replaced with Math.floor(x/320). At least, I think the effect you're going to there.

x = 330
x % 320 = 10
x - (x % 320) = 320
x - (x % 320) / 320 = 1
Math.floor(x / 320) = 1

The second one feels like you're doing X % 320.

x = 330
chunk = 1
chunk * 320 = 320
x - chunk*320 = 10

I think that taking care of these issues might help figure out the problem. You're using an excess of imprecise mathematics, that will cause problems and probably help confuse where the actual issue is coming from. :3

Offline Geemili

Junior Member


Projects: 1


No Games Finished


« Reply #4 - Posted 2012-02-25 00:46:51 »

Thank you for explaining it, the collision works perfectly now. Cheesy

Also, is there any way to make this work when it reaches the negatives? I just tried it by making it absolute, but I knew that wouldn't work because the number line is reversed, but the chunks have their independent grids, so they aren't affected. So I end up with seemingly random collisions. Any ideas?

When x is negative, x%320 will be negative too. Because of this, when -320 < x < 320, chunkX = 0, so this chunk is 640 blocks wide. Is it intentional?

No, this is not intentional, but I also haven't had any problems with it yet.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (44 views)
2013-05-17 21:29:12

alaslipknot (53 views)
2013-05-16 21:24:48

gouessej (82 views)
2013-05-16 00:53:38

gouessej (81 views)
2013-05-16 00:17:58

theagentd (90 views)
2013-05-15 15:01:13

theagentd (83 views)
2013-05-15 15:00:54

StreetDoggy (127 views)
2013-05-14 15:56:26

kutucuk (149 views)
2013-05-12 17:10:36

kutucuk (149 views)
2013-05-12 15:36:09

UnluckyDevil (159 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.104 seconds with 21 queries.