Labyrinth with physics

Hello everyone

I’m creating a classic labyrinth game with physics.

as usual in the labyrinth moves a ball.

Everything was going well but I got a diagram from the chart and now I have some doubts about some things.

In the picture (attached) the red part are the area where the ball should stand, while the white circles are the points where I have problems:

  1. Do that the end portions of each “row” of the labyrinth are not squared but semicirconferences.

  2. Create paths not only horizontal and vertical but also diagonal

  3. Also create curved paths

Is there a “simple” way to do this?

Of course the problem is not creating the path graphics

The problem is that physics best reflects the graphics

I hope I explained myself

Have you used physics.setDrawMode(“hybrid”) to verify that the physics world matches your graphics?

[lua]

physics.setDrawMode( “hybrid” )

[/lua]

Of course!
But as I said my problem is the curved bodies I can not recreate them physically
Those that look like a half donut …

How did you code the physics bodies?  Can you show us a screen shot of the physics bodies?  I’ve used Physics Editor to create half donuts before but I’m not sure if I’m understanding your exact questions.  A few more pictures would be helpful.

Well then I made a small example.

code:

local physics = require("physics") physics.start() physics.setGravity( 0.0, 0.0 ) physics.setDrawMode( "hybrid" ) --debug hybrid normal local p1 = display.newRoundedRect( 160, 70, 250, 40, 20 ) p1:setFillColor( 1, 0, 0 ) local p2 = display.newRoundedRect( 160, 220, 40, 300, 20 ) p2:setFillColor( 1, 0, 0 ) local p3 = display.newRoundedRect( 160, 370, 290, 80, 40 ) p3:setFillColor( 1, 0, 0 ) local r1 = display.newRect( 160, 40, 220, 20 ) r1:setFillColor( 0, 0, 1 ) physics.addBody( r1, "static" ) local r2 = display.newRect( 160, 420, 220, 20 ) r2:setFillColor( 0, 0, 1 ) physics.addBody( r2, "static" ) local r3 = display.newRect( 95, 210, 90, 240 ) r3:setFillColor( 0, 0, 1 ) physics.addBody( r3, "static" ) local r4 = display.newRect( 225, 210, 90, 240 ) r4:setFillColor( 0, 0, 1 ) physics.addBody( r4, "static" )

result:

As you can see the physical bodies allow the ball to be in the red area, however as you can see in the following photo I do not know how to dynamically cover the points represented by white circles:

Using Box2D physics (which is at Corona’s core) does not typically allow you to build “concave” shapes, which is what your curved regions require… fortunately, you can do it using “edge/chain shape” physics bodies, which are basically like a line that can bend/curve however you need, including concave.

https://docs.coronalabs.com/api/library/physics/addBody.html#edge-shape-chain-body

Now, it’s up to you how exactly you determine the vertices which will form the chain that bends around the curves… but a little geometry should be able to solve that matter.

Take care,

Brent

thanks a lot this is what I needed!

I did not know they could use it like that.

Now with a function i calculate the points of the curve and the game is done.

The only thing I ask is if there is a way to define the thickness of the chain

Great! With chain shapes, there’s not really such thing as a “thickness” but you could (with a bit of extra math) continue to add vertices around the back side of the curve, then connect the end points, so that it forms like a thicker “C” shape that’s closed (no open ends).

Brent

Thanks to the speed.

However in this case add some vertices to simulate the thickness.

Is not there a limit number for the vertex of the true chains?

There’s no strict “limit” that I’m aware of, but as with all things physics-related, best to keep it reasonable… I mean, don’t go making a chain shape with 2000 vertices or something excessive. :slight_smile:

Brent

Well perfect !!

Thanks again for the availability :slight_smile:

I have a project that has an environment with chain bodies (1350 vertices), hundreds of regular physics bodies, dynamic shading and AI.  It runs at about 27 FPS on iPhone 7 and a Samsung tablet.  Of all the things mentioned above, the chain bodies appear to carry the smallest computational overhead of the bunch once the program in running but they cause a delay at launch as the chain-shapes are generated.

Thanks, good to know.

So if you use a lot of vertices, it’s best to use a load screen

I would say @sporkfin’s case is very much pushing the “typical” limits… and it’s honestly great to hear it runs as fast as it does! For general cases where you don’t push physics into the “hundreds” or even “thousands” or objects/vertices, a loading screen probably isn’t necessary.

Brent

Since we are on the theme, is it best performing to create a rectangle body or a chain with 4 vertices?

What to know if to create a labyrinth with more than one or the other.

sorry if I use the same tred but open a new thought would be useless

Basic rule of thumb is, the fewer overall physics bodies you can have, the better… but within reason. If it would take 5 rectangular objects to “bound” a region OR 1 chain shape, then I’d use the chain shape… 1 body vs. 5. However, I wouldn’t suggest trying to trace an entire level using 1 chain shape of 4000 vertices or something extreme like that. I’d aim for balance… use chain shapes as much as you can within reason, but fill in certain large areas with rectangular objects.

Brent

One level requires about 100/200 vertices no more …

Anyway I understand what you mean and if possible avoid chains.

Thank you so much for the advice

I’d say if you max out at 100-200 vertices, then go ahead and use one chain (if you can form one chain to trace the entire level). I was more cautionary about something like 2000+ vertices. :slight_smile:

Oh, makes me very happy then! :smiley:

With the vertices I use less time than creating and positioning rectangles.

obviously i can put a rectangle instead of 4 vertices.

Have you used physics.setDrawMode(“hybrid”) to verify that the physics world matches your graphics?

[lua]

physics.setDrawMode( “hybrid” )

[/lua]