Box2D - Get your motor running

Monkey Archive Forums/Monkey Tutorials/Box2D - Get your motor running

skid(Posted 2015) [#1]
Box2D is a powerful physics simulation system that can be used for a developing a variety of monkey games.

The idea of this series is to help those wanting to take the first few steps using muddy shoe's most excellent port of Erin Cato's box2d engine.

Download the monkey Box2D module here, this tutorial code has been tested with release 1.0.12

And if you want some background reading try the box2d manual which is here -

Step 1 Download and install the box2d module.

Step 2 Run the above code and play with F1 and F2 keys

Step 3 Check out in the main class and observe how

* box2d uses bodies and fixtures to do it's thing
* without the random impulse the simulation stacks the balls perfectly
* box2d DrawDebugData function replaces any need for placeholder graphics when prototyping
* box2d spreads out balls if you add 3 at a time on top of each other

Duke87(Posted 2015) [#2]
Nice work!

I just downloaded box2d 1.0.12
I guess u forgot to inlcude:
Function Main:Int()
	New MainDemo
End Function 

to the maindemo.monkey.
Performing very well on all targets, but Flash.

Bye Duke.

skid(Posted 2015) [#3]
Today's code is my attempt at simulating a pinball flipper. And a bit of a refactor...

The shape of the flipper is a bunch of points which I create in a plain old float array then convert to vector of b2Vec2 for use by Box2D.

Simply treating the flipper as a static body and using Rotate kind of works in a really wrong kind of way.

However proper solution is to anchor the flipper to the ground with a Revolute joint which gives us everything we need for setting up the mechanics.

There are two basic simulations you can do with box2d.

A top down simulation such as pool or mini-golf where gravity is turned off and the camera faces the ground.

A side on simulation such as angry birds or worms where the camera is aimed at the horizon and gravity points straight down.

And remember a successful game doesn't need to simulate a large population of bodies, typically the good ones have particularly low numbers and the attention has gone into tuning the experience in regards to most fun.

skid(Posted 2015) [#4]
Today's simulation is the start of a quarry wheel loader machine with an arm and bucket that can catch falling balls.

The code also features a new fixture generator that takes a path of x#,y#,r# points and adds radial and connecting quads fixtures.

I left the AddStaticEdge function in the code for a future ground contour which we will hopefully be driving across shortly.

skid(Posted 2015) [#5]
This version adds a chassis, suspension and wheels to the arm bucket attachment.

When playing with the new vehicle note:

* applying too much rotational force / torque makes the wheel's spin, in future we could reduce torque at low speeds
* even with only 3 joints the machine feels like it has a life with mass / density of arm giving it a real character
* the box2d prismatic joints allow us to emulate suspension, see the box2d manual for more info

skid(Posted 2015) [#6]
Today I take a break from vehicles and build a human.

After making a few marks in a door frame I used a tape measure (mm) to calculate a version of my own skeletal form for simulation purposes.

There is a bunch of keyboard mappings to control every motor and even without any basic constraints it looks like it will be possible for mini-me to stand balanced and perhaps even take a little walk...

DruggedBunny(Posted 2015) [#7]
Gah, I missed all this! Will give it a whirl later, sounds cool!

skid(Posted 2015) [#8]
Todays version adds constraints to the joints and doubles up on the limbs so min-me now has the correct number.

Motors are hooked to keyboard speed settings and amount of torque is set to fun / slightly drunk / party mode:

qw er ty ui
as df gh jk
zx cv bn m,

temporarily online here -

The hips and pelvis need separating they are both spinning at same position which is bad I think.

This week I am thinking about tackling some of the following:

- basic motor feedback so mini-me can hold a pose
- attachments so mini-me can hold a phone while driving a quarry loader
- a four legged friend and a two winged friend
- more vehicles with a downhill run with jumps...

therevills(Posted 2015) [#9]
Motors are hooked to keyboard speed settings and amount of torque is set to fun / slightly drunk / party mode:

LOL! I think I broke his back!

These are great tutorials skid! Would love to see you attaching some graphics to the objects too.

skid(Posted 2015) [#10]
Thanks! I just did my first test broadcast. Current graphics blur the hell out so it looks truly hideous on youtube.

The downhill step will have the viewport attached to a body, will add graphics once that bit is working.

skid(Posted 2015) [#11]
I am using an app called OBS to broadcast live from chrome to youtube.

I think I have the bandwidth and just need a bit more computing power for the compression stage.

This particular performance is called I have no shame.

Live broadcast is here.

skid(Posted 2015) [#12]
After struggling with a back bone that could break very easily the penny finally dropped.

By removing the fixture I attach to the hips in above version the back and upper legs suddenly start behaving extremely well.

I had tried a weld joint but that gave me very bizarre results.

Removing the hip fixture however is not without it's own problems. box2d seems to say thats impossible to solve and allows mini-me to hang in what looks like a very unbalanced state.

Rebuilding mini-me starting with the head and not the hips seems to be the correct fix and the fragile 90 year old breakable pelvis issue seems to be solved.

Emulating carrying a tray of drinks is one I thought I will add once we have learned to walk.

TheRedFox(Posted 2016) [#13]
Thanks for making these available!

RedGTurtlepa(Posted 2016) [#14]
thanks for this!

SLotman(Posted 2016) [#15]
I know this is an old topic, but since it was resurrected... ^_^

A top down simulation such as pool or mini-golf where gravity is turned off and the camera faces the ground

I'd love to see how to implement that!