Box2D Framework

Monkey Forums/Monkey Code/Box2D Framework

CopperCircle(Posted 2012) [#1]
Hi, here is my simple framework for Box2D based on Volkers Box2D Screen for Diddy.

http://www.coppercircle.co.uk/Box2DFramework.zip

- Allows you to easily create Box2D bodies with or without images attached, including spheres, boxes, bridges and polys.

- You can also set debug drawing on a per body basis and have image and debug drawing.

- Get collision info.

- Load Box2D Generic XML files created by PhysicsEditor - http://www.codeandweb.com/physicseditor

Note: Currently only one shape per PhysicsEditor XML file is supported and make sure the image anchor point is at 0,0. Also you will need to vertically flip your image after creating the poly XML file as the exporter is in BETA and flips the verts.

Thanks to muddy_shoes for the great work on creating the Box2D Module.


Neuro(Posted 2012) [#2]
This would definitely be useful later on :). But the objects seems to kind of float as they fall. How would we set the world gravity or weight(mass?) of the object?


therevills(Posted 2012) [#3]
This is cool :)

How would we set the world gravity

Just checking Copper's code and when you create the world the constructor has a gravity parameter:

Demo.monkey
[monkeycode]Class Box2dscreen Extends Screen
..
Method CreateDemo:Void()
..
Self.world = New CWorld(Self.ratio, 0, 10.0, box2dDebug)
..[/monkeycode]

Which goes to this constructor:

box2dframework.monkey
[monkeycode]Class CWorld
..
Method New(ratio:Float = 1.0, gravityX:Float = 0.0, gravityY:Float = 10.0, debug:Int = 0)
[/monkeycode]


CopperCircle(Posted 2012) [#4]
Hi, yes you can set the gravity as therevills shows, you can also access other parameters like this:

groundEntity.fixtureDef.density = 1.0
groundEntity.fixtureDef.friction = 0.2
groundEntity.fixtureDef.restitution = 1.0
Local massdata:b2MassData = New b2MassData()
massdata.center = New b2Vec2(0,0)
massdata.mass = 100.0
massdata.I = 0.0
groundEntity.body.SetMassData(massdata)


Also to fix the flipped verts with PhysicsEditor XML Export just change the this line in the export.xml to this <yAxisDirection>down</yAxisDirection>

*UPDATE, added a simple rope entity.


matt(Posted 2012) [#5]
I'm not seeing things fall any faster either way. Changing the gravity in the CWorld declaration, or adding the code you list above.

Also, trying to add an extra parameter to the "orange" sphere creation does not result in spheres of a different radius size.

Any ideas?

Great work, though!


CopperCircle(Posted 2012) [#6]
Hi matt, the objects fall at the simulation speed, you could use ApplyForce to speed them up.

Also the CreateSphere function uses the image size if you pass an image, if you put Null as the image you can use the radius option and attach your own image.


Volker(Posted 2012) [#7]
Matt, probably you have to use some scaling with box2d.
I haven't implemented this in my box2dscreen, to keep things simple.
Why use scaling?
"..you should not use pixels as your units, an object of length 200 pixels would be seen by Box2D as the size of a 45 story building."
Which will result in some weird behaviour.
So you need something like a "global phys_scale=30" parameter, and all
creation and positioning of bodies must be divided through this.
Then the physics will behave similar to games like Angry Birds, and
gravity can be speeded up.
Take a look at the demos included in the box2d mod, they all use
some scaling.
Don't forget to set the scaling for debug_draw too.


matt(Posted 2012) [#8]
Thanks guys


CopperCircle(Posted 2012) [#9]
I made this as a quick way to prototype ideas, but would be great if someone wants to add scaling?


matt(Posted 2012) [#10]
It would be great (but a lot more work) if it had a mouse interface for placing and defining scenes of shapes with physics properties.

Sort of like a Box2D physics/scene generator? Or does such a thing already exist?


c.k.(Posted 2012) [#11]
matt, see Physics Editor link in first post of this thread.


matt(Posted 2012) [#12]
I want to do more than create shapes with properties. I want to put bunch of them on screen, for example set up one of the famous Box2D demo scenes, and then run the system and see how it interacts.

Kind of like Algodoo, but specific to Box2d and with the ability to export a scene.

http://www.algodoo.com


c.k.(Posted 2012) [#13]
Maybe this would work for you: http://www.jacobschatz.com/bisonkick/


c.k.(Posted 2012) [#14]
http://www.levelhelper.org/ ?


c.k.(Posted 2012) [#15]
There's got to be something out there, right?! :-)

Let me know what you find, because I'm going to need something like this as well.


matt(Posted 2012) [#16]
The closest I have found so far is Mekanimo, but it seems to be abandoned.

http://www.mekanimo.net (and see YouTube for videos)

LevelHelper looks promising.


CopperCircle(Posted 2012) [#17]
You could write a loader for LevelHelper xml files or make your own editor. I am making an editor for my needs that combines box2d and diddy's tiled maps.


Volker(Posted 2012) [#18]
There is another editor which looks pretty good.
But someone would have to write a loader.
http://box2d.org/forum/viewtopic.php?f=6&t=7457


matt(Posted 2012) [#19]
@CopperCircle your editor sounds cool

I'll also check out the one Volker mentioned


CopperCircle(Posted 2012) [#20]
I have just updated this to include physics scaling and better updating, objects now react to gravity and the physics response is more realistic.


Neuro(Posted 2012) [#21]
Definitely working much better CopperCircle, thanks for the update :)!


Paul - Taiphoz(Posted 2012) [#22]
am I missing some files ?

D:/Code/MonkeyPro/modules/box2d/collision/b2aabb.monkey<215> : Error : Identifier 'Min' not found.
Abnormal program termination. Exit code: -1


Paul - Taiphoz(Posted 2012) [#23]
ignore me, just me being stupid.


FelipeA(Posted 2012) [#24]
This is really nice, for a long time I wanted to try out box2d but found it very difficult, with this in just a couple of minutes I had a small demo running. http://shin.cl/b2d

I do have a question, I think is more related to box2D. How do I compare two userdata.


Volker(Posted 2012) [#25]
You cast them.
Untested:
Class Ship

Field body:B2Body

Method New()
  '... do things like create a body etc.
  
  ' store the object the body belongs to in the userdata
  self.body.SetUserdata(self)
End Method

End Class

Then you can do things like this:
local userdata:Object=body.GetUserdata() ' retrieve userdata from a body
if Ship(userdata)<>NULL ' is it the userdata of a ship?
  Print " body's userdata is a ship!" 'yes
endif


To just compare them a
if body1.GetUserdata()=body2.GetUserdata()

should be enough.


Tri|Ga|De(Posted 2012) [#26]
I get this error:

Duplicate identifier 'DiddyApp' found in module 'framework' and module 'framework'.



CopperCircle(Posted 2012) [#27]
Are you compiling the demo or your own code?


Tri|Ga|De(Posted 2012) [#28]
The demo


CopperCircle(Posted 2012) [#29]
Not sure, make sure you have the latest Diddy module installed.


Tri|Ga|De(Posted 2012) [#30]
I downloaded the latest Diddy before trying your demo.


CopperCircle(Posted 2012) [#31]
You may have diddy in the modules directory twice?


Tri|Ga|De(Posted 2012) [#32]
No I have just one folder called Diddy


Xaron(Posted 2012) [#33]
This works incredible well. Thanks for that! :)


Xaron(Posted 2012) [#34]
Oh well, just one note: Please consider to change

Function RadToDeg:Int(rad:Float)


to

Function RadToDeg:Float(rad:Float)



Tri|Ga|De(Posted 2012) [#35]
@CopperCircle

Do you have other solutions to my problem.
I would really much like to add Box2D to my current game.


Volker(Posted 2012) [#36]
You should set up a new monkey (keep your old one), just with newest diddy and box2d and test if compiling the example works.


Tri|Ga|De(Posted 2012) [#37]
Okay I did that and now I can run the demo.
Thanks!


OvineByDesign(Posted 2012) [#38]
anyone else get shockingly bad results when running on Android Devices ?


Just tried a simple 16 boxes on screen on an S3 and nexus 7, both struggled.

runs fine in Html5 and glfw

/StuC


Volker(Posted 2012) [#39]
Have you disabled DebugDraw of box2d? Costs a lot.
Is your App running in release mode?


OvineByDesign(Posted 2012) [#40]
Yes debug is disabled and running in release mode.

Just tried the demos that came with the Box2d port and they run like a dog too.

/StuC


CopperCircle(Posted 2012) [#41]
I have not had any major issue with android, it is a little slower than other platforms but still runs ok?


OvineByDesign(Posted 2012) [#42]
hmmm I wonder if I have something wrong with my setup? latest technology monkey, box2d. android sdk upto date, latest java.

as soon as I get over 16 objects the engine really runs slow


CopperCircle(Posted 2012) [#43]
Do all the objects need to be on screen at the same time? I dynamically add/remove objects as the camera moves to save on box2d updating.


OvineByDesign(Posted 2012) [#44]
Its not the drawing of the objects thats the slow down, its the physics. Im plotting a single pixel in place of the object and the slowdown still occurs.

I don't class 16 objects as alot on screen. Take for instance angry birds that runs super smooth, definitely has more than 16 object on most levels and that uses box2D.


/StuC


MikeHart(Posted 2012) [#45]
Stu, for which version of Android are you compiling?


OvineByDesign(Posted 2012) [#46]
Ive compiled for 13,14 and 15.

I'ive also now compiled on IOS and found that around 25 objects and that starts to crawl too.


/StuC


CopperCircle(Posted 2012) [#47]
It will never be a fast as a native implementation of box2d, but if you dynamically enable/disable box2d bodies when you need them you can do allot, I have a game with hundreds of objects working at 30 FPS.
It depends no what type of game your trying to make.

It is slow when lots of objects are moving and collisions are being updated, try to use static objects or edges if poss, also enable sleep on objects and if an object is not on the screen remove it's body.
You can also play with the box2d update/accuracy settings.


OvineByDesign(Posted 2012) [#48]
it doesn't. seem much on screen for the slow down to happen...... I'm just stacking 20 rectangles on top of each other then launching a missile at it. seeing they are all on screen is can't disable any of them

might have to see if there is a library I can link into. instead of using the monkey code version

stuc


muddy_shoes(Posted 2012) [#49]
It's not really possible to respond practically to complaints about "slowness" because the terminology is too vague. I can't tell if you're seeing normal or abnormal performance based on what you've written.

The demo set for the port includes a test where a ball runs down a slope and hits three stacks of 10 boxes. The physics update on that takes 20-25ms on my ZTE Blade running Android 2.3.7. However, the visual frame rate will look much worse if the standard monkey rendering mechanism is in place due to the way it drops frames.

Android using Java isn't a speedy platform and Box2D's worst-case scenarios involve stacks of objects because of the number of collisions occurring. Creating a game that involves piles of objects being knocked over using the monkey port is perhaps not a sensible plan.

You mention using a "linked library". You might be interested in reading the results of the comparison I did with LibGDX, which uses Box2D via JNI calls: http://pointlessdiversions.blogspot.co.nz/2012/03/monkey-vs-libgdx-box2d-performance-on.html. Also, if you're comparing with Angry Birds then I believe I'm correct that it was written using the NDK and so has another level of performance advantage.


Armitage1982(Posted 2012) [#50]
I wonder what kind of performances we could achieve by using Farseer written in Monkey ?

The language is already advanced enough since I remember Alex Okafor (http://www.paradeofrain.com/code-archive/) did it in BlitzMax with a very old version of Farseer.

Is there any C# to Monkey tool available yet ?


cageInfamous(Posted 2013) [#51]
I like this framework a lot. But how do I manually add forceto an entity or move an entity?


rIKmAN(Posted 2013) [#52]
The last time I used Box2D it was ApplyImpulse() and ApplyForce().


Volker(Posted 2013) [#53]
You can although directly set the velocity of the with body.setvelocity() to move.


Sammy(Posted 2013) [#54]
If Monkey supported an Android native C++ NDK target, this would allow the use of the Box2D C source, which would probably solve the speed problems on Android IMHO.


Nobuyuki(Posted 2013) [#55]
It's possible to use the C source now, but that would complicate the portability of box2d on some targets. Since monkey's Box2d is done in pure monkey without native externs, that means it's supported on all platforms monkey supports. The problems that entails wouldn't be resolved magically with monkey supporting an NDK target -- In fact, it would probably create new headaches, as NDK basically takes away your guarantee of basic portability, due to differences in small things like GPU texture compression support across devices which would now have to be handled by trans instead of the OS drivers.


Sammy(Posted 2013) [#56]
GPU texture compression is handled by the graphics API not directly by Monkey, in this case GLES 2.0. Using or not using the Android NDK does not affect this.