Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  [LibGDX] how to create an infinite level with box2d  (Read 6163 times)
0 Members and 1 Guest are viewing this topic.
Offline grevius
« Posted 2013-09-19 15:51:06 »

hi to all! i use libgdx and box2d in my game and i have a question. I have to do a dinamic level, creted randomly in real time that would be infinite to the right (my is a side scroller game).
How can i do this? how to do this with havent thousand of body in memory for the previous track terrain?
what method i have to implement for create this kind of dinamic randomized level?
thx in advice and have a nice day  Wink Grin
Offline namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

« Reply #1 - Posted 2013-09-19 20:04:03 »

so lets say, its gonna be a tiled map

there is a few ways that I know of to do this

each tile or block, is going to have a bodydef associated with it,   keep a Bag Array or something of all of those bodydefs
have it iterate over the array,  checking to see if those bodies/tiles are beyond lets say x < -1000,  then world.remove(thatBodyDef);

Have a giant box/line/whatever  to the lefto f the screen x = -1000,  and then setup a custom collision event,  whenever a tile collides with it, remove that tile from the world


Have a 'chunk'   each chunk contains lets say 50x50 tiles or whatever.    This way you aren't checking against hundreds or thousands of blocks x position.  Youd just check against 3-10  chunks position.  When the chunk is far enough gone to the left, remove the chunk(and all of its tiles that exist in that chunk)

And on the opposite side of things.
On the positive x > 1000,  you could do almost the exact reverse of the above #1 or #3,   either have a chunk system constantly cycling through chunks or   create a single column of block/tiles/boxes  much more frequently.

For 'randomized level'   depends on what style of dynamic you want.  You could use Simplex or Perlin noise.   Or you could use a 'walking/roughness/windiness' algorithm (  Like that, but instead of up/down, implement it to left/right

There are several other ways to handle it, but thats what I would recommend

Recently I implemented the dynamic level, being randomly created in real time, infinite to the right,  using box2d.
play the .jar  and see how it is.
Then you can also look at my sloppy way of implementing it

The chunk and the block is whats handling most of whats related to your question.
p.s. If you decide to dive into the code at all,  be mindful it was written hastily and poorly.  There are some 'unused' things and some 'poor approaches' to several things,  However it does work and is functional, and its performance was just fine for what I needed, so I never cleaned it up  Pointing  Huh Undecided Cry

"Experience is what you get when you did not get what you wanted"
Pages: [1]
  ignore  |  Print  

EgonOlsen (61 views)
2018-06-10 19:43:48

EgonOlsen (42 views)
2018-06-10 19:43:44

EgonOlsen (61 views)
2018-06-10 19:43:20

DesertCoockie (241 views)
2018-05-13 18:23:11

nelsongames (142 views)
2018-04-24 18:15:36

nelsongames (141 views)
2018-04-24 18:14:32

ivj94 (883 views)
2018-03-24 14:47:39

ivj94 (144 views)
2018-03-24 14:46:31

ivj94 (795 views)
2018-03-24 14:43:53

Solater (159 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!