Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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
 3D voxel collision  (Read 2341 times) 0 Members and 1 Guest are viewing this topic.
RylandAlmanza

Junior Devvie

Medals: 3

 « Posted 2012-03-06 20:33:49 »

So I'm making a game where everything is made up of voxels, including the map. I've got simple 3D collision detection and handling working exactly how I want it to, except for one thing. I have to loop through every voxel in the map! This is what I used to do for 2D tile-based games, and it worked fine, but I assume this will take a toll on my game's performance since there's a whole new dimension to loop through. Is there a way I can only check collision with voxels near the player?
sproingie

JGO Kernel

Medals: 202

 « Reply #1 - Posted 2012-03-06 20:50:21 »

You only need to check for collisions in the area of things that are moving, which is the bounding cube of the minimum and maximum x,y,z of the start and end locations of everything that moved subsequent to the last check.  This can be pruned down further, but it's a good place to start.
Geemili

Senior Devvie

Medals: 9
Projects: 1
Exp: 2 years

No Games Finished

 « Reply #2 - Posted 2012-03-06 23:40:26 »

If your using voxels, I assume that you are using an array. I am doing 2D collisions in my game using this:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public Block getBlock(float xx, float yy){   int x = Math.round(xx);   int y = Math.round(yy);   int chunkX = (int) Math.floor( x/320 );   int chunkY = (int) Math.floor( y/240 );   Chunk chunk = getChunk(chunkX, chunkY);         int blockX = ( (x%320)/16);   int blockY = ( (y%240)/16);   return chunk.getBlock(blockX, blockY);}public Block getBlock(int x, int y){   return level[x][y];}`

Note that the first half of the first function is for getting a world location, and the next gets the block. The second function is what the chunk uses to find that block. As you can see, it doesn't have to loop through every block. Unfortunately, this does not work in negative. Or, I haven't found out how to, others can probably find out how to do that.
 Games published by our own members! Check 'em out!
UprightPath
 « Reply #3 - Posted 2012-03-06 23:42:53 »

Math.floor() isn't needed! Integer division results in the floor!

tom
 « Reply #4 - Posted 2012-03-07 08:22:44 »

Not for negative numbers.

theagentd

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2012-03-07 08:42:26 »

Not for negative numbers.
The fact that you are doing the division between two integers means that the result too will be an integer, and obviously is already "rounded", as in rounded towards zero. The floors! They do nothing!

Myomyomyo.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (28 views) 2015-04-23 03:42:11 theagentd (32 views) 2015-04-22 16:23:07 Riven (45 views) 2015-04-16 10:48:47 Duke0200 (55 views) 2015-04-16 01:59:01 Fairy Tailz (39 views) 2015-04-14 20:13:12 Riven (42 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (63 views) 2015-04-10 00:51:04 BurntPizza (62 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x alwex 11x 65K 11x kingroka123 11x Rayvolution 8x kevglass 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x Ecumene 7x KevinWorkman 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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