Box2D Multi-Element Bodies

Hello.

I found this gif showing a character composed of a circle which allows movement and a rectangle (above the circle) allowing objects to be pushed.

I can’t understand how the two elements are linked to work like this.
why the rectangle remains permanently above the circle rather than rotating with it.

someone can code the same thing in solar2D (even on flat ground; what interests me is the composition of the character)

thank’s

Without seeing the gif, I can’t say for certain, but that sounds like it was done using joints instead of multi-element bodies.

If you gave that circle the object.isFixedRotation = true, then it wouldn’t rotate even if a connect body was pushed.

Oups.

Yes. Sorry

here is the gif

My first thought when seeing that is:

1 The character is the physics body (a circular body) and would normally be hidden.
2 The visible part (the girl) is an animation that is told when to play and follows the position of the circle.

I can demonstrate this later today when I get time, but I would simply put all my code on the ‘circle’ and have it draw, animate, and move the player image. The movement of the player image can be done in an enterFrame listener.

Warning: I’m a bit rusty, so someone else here may have a more elegant solution.

UPDATE Day job interfered. Will try to provide sample Thursday.

All you said is correct but it was not my question.

My question only concerns the green circle and the green square below.

image

How to compose (or link) those 2 object to have the same effect?
I guess there is a AngularImpulse on the circle to make it turn.
But how the square can remain above the circle without undergoing its rotation ???

I would guess that the setup/linkage goes like this:

  • Create girl display object.
  • Attach square physics body to girl (smaller and offset down). Ensure body has isFixedRotation = true
  • Create tiny dummy display object w/ transparent PNG
  • Attach circle body to dummy object. Use high friction value.
  • Link circle body to square with Pivot joint (or optionally a wheel joint).
  • Use joint motor to turn wheel and thus move conglomerate.

I believe, this should keep the girl vertically aligned, but allow the wheel to drive your motion.

This will definitely take some experimentation, and your impulse idea might be better than motor and/or need to be added to get the initial push.

Again, I’ll do my best to give an example when time permits and if you don’t beat me to the punch.

Meanwhile, at the risk of distracting you, I do have a very old physics sampler I made when we had a weekly show called CoronaGeek.
My entire repository is here: GitHub - roaminggamer/CoronaGeek: Content from the Corona Geek Hangouts
The sample is in the folder: CoronaGeek\Hangouts\ICanMakeThat\PhysicsFun\physicsFun\

Has both pivot and wheel joints. Uses OLD subset of SSK which could be confusing. However, I want to offer the example just in case.

OK. I made a simpler example here:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2022/03/box2d_multi_element_body.zip

All the important parts are in main.lua.

It does not include the player animation, but the movement and collision setup/filtering is provided.

Note: Left-Right arrow keys move player.

Uncomment -- physics.setDrawMode("hybrid") towards top of file to see bodies.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.