|
I'm trying to implement parallax scrolling with multi-layer support, where each layer uses Box2D for collision detection and physics. I know how to separate collision detection between layers (using masks), but I’m unsure about handling movement and scaling. Objects on layers further from the camera should move more slowly and appear smaller. How can I make Box2D account for this? |
Replies: 2 comments 1 reply
|
I would use a larger visual viewport for the layers further back. Make it a rendering problem. Also you might consider using a separate Box2D world for each layer instead of using masks. |
|
For the game I'm making, we wanted something similar with physics happening on multiple layers/planes. I figured I'd write up something here about how I solved it, since I thought that might be of interest. For us, having separate worlds for each layer wasn't ideal because we wanted to be able to have objects that span multiple layers (so e.g. an object spanning layers 1 and 2 would collide with both layer-1 objects and layer-2 objects, but layer-1 and layer-2 objects wouldn't collide with each other) as well as being able to joint objects on different layers together. Using collision masks wouldn't work either because we wanted to have a large number of layers, each of which can have objects on it with all the same physics interactions as on any other layer. As far as I can tell this would require essentially duplicating all the collision mask bits we use per each layer, but I ended up making a small modification to Box2D - basically giving Alternatively, you could use a bitmask for layers instead of a range. Then you'd be able to specify a non-contiguous set of layers for each shape, at the cost of having fewer total layers for the same memory footprint. A single As for rendering perspective/parallax, we handle that entirely separately from Box2D. One thing that's a bit lacking about our approach is that the debug rendering via |
I would use a larger visual viewport for the layers further back. Make it a rendering problem. Also you might consider using a separate Box2D world for each layer instead of using masks.