Learning Box2d

BlitzMax Forums/Brucey's Modules/Learning Box2d

JA2(Posted 2009) [#1]
Hi,

I'm new to programming with box2d and I was wondering if anyone would be kind enough to share some simple examples with me to help me learn?

I've looked through the examples that come with the module but it's quite a lot to take in. Something very simple would be great, maybe with some comments to tell what does what?

A big thank you in advance for any help :)


Armitage 1982(Posted 2009) [#2]
Hi JA2

First of all, read the manual that Brucey include in every modules.
That will give you a good starting point

Then don't forget you need to include something like the render.bmx in order to have a "debug" rendering and actually see something in your BlitzMax Application (box2d is only a physics library and won't render things for you).

Inside this render.bmx there is extended DebugDraw type/class automatically used by Box2d if you setup and provide correctly a b2DebugDraw object settings (see test.bmx).
The render.bmx also contain important scale values (normally xScale/yScale of 8.0).
Box2D use a system of meter/second and since you don't want that 1px = to 1 meter you better have to choose the best scale value for your world (or then the simulation will be very slow).

The test.bmx is the main core of every testbed examples available in Box2d.
Contain : The main loop, mouse manager, box2d Settings, box2d contact point manager and Box2d body destructor.

Examples are generally made of simple body setuping.

The helloworld.bmx is your best ally to fully understand the logics behind Box2d. (It's probably the simpliest example you ask for :-)

I suggest you follow the pyramid.bmx for a visual example and read the appropriate chapter from the BlitzMax Box2d Documentation.

Create a "simple example" for you to learn would probably mean rewriting what's already present in these 3 files.

Don't hesitate to go to the official box2d website : http://www.box2d.org/
A very few things is changed in the way BlitzMax run Box2d so you could learn a lot of things from the Forum or the Wiki.

I could only help you with specific questions on Box2d.

Hope that help a bit :-)


JA2(Posted 2009) [#3]
Thank you for replying, Armitage :)

I have spent almost my entire day looking through the examples and reading the C++ manual from the box2d wiki. I put test.bmx and render.bmx in the same file and copy pasted the bridge example, then took the whole thing apart piece by piece to see what's happening. Things are starting to come together slowly.

Could you tell me, how do I draw an image instead of the debugdraw shapes? I had a go at converting an example from the wiki but I couldn't get it working.

http://www.box2d.org/wiki/index.php?title=Linking_graphics_to_bodies_in_Box2D_AS3


JA2(Posted 2009) [#4]
Got it working :)

I had a quick look at your website and found a physics demo that you made with cubes, balls, chains etc. I'm trying to recreate it as best I can. I'm going to have a look at making edges next, I guess this is how you made the ground pieces on your physic_stuff demo?

Any tips or advice for creating a scrolling tilemap with physics bodies and all?

:)


Armitage 1982(Posted 2009) [#5]
Hi JA2

Glad you did it !
Sorry but I was on "vacation" ;)

Thanks for downloading stuff ^^
Was done with Chipmunk but the last examples on my blog are fully done with Box2d.

In fact, My first level editor was using rectangle forms since at that time chainEdges did not exist.
Later on I totally rewrite it to provide a polygonal editor for platform and it's much much better :)

Edges won't always work specially if you use them as dynamic object.
It's a question of mass calculation.
For example these dynamic edgeschain won't work out of the box with Buoyancy effect.

Also, I experiences problem with buoyancy in my game so I write my own. Faster but is less accurate.
There is many ways to realise things in Box2d :)

I also cut the corners of my body players so there is no "sticky" problem when passing from one platform to another and use the normal angle of any slopes to add a little more power to the velocity impulsion in order to walk smoothly over.

Some in the Box2d forum go with the idea of the "unicycle body" player which work great too.

Box2d is faster enough to process a whole platform level without the need of particular optimization or "areas".
From 500 ~ 1000 Objects at time depending of your rendering batch method.

Most of the crash comes from creating objects out of the AABB world, so don't hesitate to add border space around your level.


JA2(Posted 2009) [#6]
Thanks for the advice. I still have a little way to go before thinking about adding a player to my project. I want to have a go at making a platform game once I've got the hang of what I'm doing.

Is setting a body's linear velocity the correct way to go about making my tiles scroll? I'm having some difficulties getting and setting the velocity, I think maybe I'm using vectors in a way they they aren't meant to be used...


Brucey(Posted 2009) [#7]
Is setting a body's linear velocity the correct way to go about making my tiles scroll

Noooo! :-p

I don't think you want to be moving anything that is meant to be static.
Rather, move your "view" of the platform, much like you would with normal tilemap tiles. Work off some kind of offset instead, and draw everything relative to that offset.

Unless of course you are talking about moving platforms?


Armitage 1982(Posted 2009) [#8]
Brucey is right.

The scrolling mechanics is specific to the rendering part of your game.

Think about separating your graphic from your data processing.
Only objects like player, vehicles, monsters, bullets or anything moving on his own shall be considered as Dynamic Object.

Unmovable objects or equal to 0.0 in density are Static object.

Also I don't think setting the linear velocity is the good way to go since Box2d did it for you.
When you want to move something in your world is better to use ApplyImpulse or ApplyForce.
Personally I only use SetLinearVelocity to reduce, for example, the Linear Velocity over time like this :
body.SetLinearVelocity(Vec2(body.GetLinearVelocity().GetX() * 0.90, body.GetLinearVelocity().GetY()))


Unless you know exactly what you are doing I suggest you to first right the beginning of your platform game without Box2d and then adding it by replacing any interacting element. I think it's a great way to go.


JA2(Posted 2009) [#9]
Thank you both for the replies and the explanation. I feel like a twit :)

I think I was just over thinking what I wanted to do (if that's possible :s) I've got it to work now by just adding a global worldx and worldy, changing only those values, and then adding it to the drawing xy of each shape :)

Unless you know exactly what you are doing I suggest you to first right the beginning of your platform game without Box2d and then adding it by replacing any interacting element. I think it's a great way to go.

I know more or less what to I need to do, I've made a few platform games before. Problem is that was 7 or 8 years ago and I've been stuck into 3d game making since. I plan to use sensors for the players hands and feet to check for slopes and edges that can be held on to, something like Flashback or the first Prince of Persia is what I had in mind.


Armitage 1982(Posted 2009) [#10]
Great !
Flashback (Another World, Heart of the Alien, Fade to Black) and Prince of Persia are legends for me.

I really don't know if sensor for body part are best to handle slopes and edges with Box2d since Kinematic isn't implemented yet.
I think Eric Chahi and Jordan Mechner both use somekind of finite state machinery for this.
But if you manage complex animation system with Box2d you'll probably unlock a great gameplay.


JA2(Posted 2009) [#11]
Well I was thinking of setting up grab points where the player can cling onto. Then when the sensor for the players hands overlaps the grab point he should hold on and change animation. For the feet, I thought a sensor in front of the player and one behind, both at the height of his feet should be able to tell if he's on level ground, a slope, or standing by an edge. Then I can change the animation to a sloped walk or balancing etc.

I'm just getting to the part where I can load tiles from an animimage and make a shape in code for each one. Do you know if there is any limit to the number of shapes I can add to a single body? I thought one body with lots of shapes would be better than lots of seperate bodies. Or is there another way I should be doing this?


Armitage 1982(Posted 2009) [#12]
Well I first try what you are currently trying to achieve (each tiles gives different shapes) but this is not optimized at all.
It's working but you lose the complete flexibly of Box2d by using old 2D Tiles techniques.


In my game for example I use different kind of tools.
One to draw tiles over layers and another to freely design ground shapes thanks to a simple polygon editor as you can see.
Tiles like any others objects in my game are fully customizable that's why I choose this option.

Freely shaping every level platforms that way dramatically reduce the amount of static shapes too.
What's great with this idea is that I can now use my engine to create top down games (as soon as Box2d features top down physics of course).

It's maybe not the best solution but I came with this one after a lot of trial and error.

Also "b2Settings.h" inside the "box2d.mod\Source\Common" has a bunch of settings like maxPolygonVertices or maxProxies.
I needed to change this file a bit to improve the stability of my game or simply increase the number of objects.

Exceeding some of these values in your software will lead to a runtime crash !
Always good to know (Even if I think Brucey add a lot of exceptions since :)


JA2(Posted 2009) [#13]
Wow that really looks nice!

I'm using quite large tiles for my game (128 x 128) so there's not too many on screen or active at once so I don't know if optimizing will make too much of a difference. I'm going for a more minimalist look for my game graphics if I get that far.

I've planned out 44 collision tiles so far so I can make platforms with different slopes, walls, and ceilings. I had planned to make an object editor for things other than tiles so that I can add grab points etc on to them and save/load the info.

I'll go and check out the b2settings.h :)


Brucey(Posted 2009) [#14]
Exceeding some of these values in your software will lead to a runtime crash !

Box2D tends to throw assertions when things go out of range - which makes trapping difficult.

And in some cases, like if maxPolygonVertices is not high enough for the number you are using, it won't even realise, and you'll get all kinds of nullpointer access issues.
Depending on how many individual things you have flying around, the default may be enough.


JA2(Posted 2009) [#15]
I've finished my collision shapes and parallax scrolling map with 4 layers. I had to do it all twice because I managed to delete everything and had to start over :/

Anyway, now I'm working on the player control. After I've made the player's shape and body, I create some extra orientated boxes for the hands and feet and set them to be sensors. It's all working rather well but how can I detect if two specific shapes are overlapping each other? What I want to do is detect if the player's 'hand' is overlapping a 'grab point', both set to be b2shapes.


Armitage 1982(Posted 2009) [#16]
I'm not at home for the moment so I could only answer you this :
In my case I'm using the userData of each body/shape definition in order to store the object
myBodyDef.userData = Self
then using a ContactPoint resolver (I think Brucey has translated a good starting point showing how to correctly extending b2ContactListener with ContactPoint Type to manage collisions) I recovers the two colliding bodies
local obj1:Object = cp.shape1.GetUserData()
local obj2:Object = cp.shape2.GetUserData()
identify them via casting
if myType(ob1) then ...
and then execute the appropriate object methods to handle this collision.

Hope this help.


Rico(Posted 2009) [#17]
Wow. Armitage your game is looking great! :D Its been a while since I've seen it. I played your old version. Nice graphics!


Armitage 1982(Posted 2009) [#18]
Thanks Rico
Fortunately it's just an Alpha :-D
There's many improvements present in the next (Last ?) Alpha 0.2.0 set in less than 2 weeks (hopefully).
Some game modes, more objects and several levels are planned for the beta.
Just need to find time between job hunt and life to get this done.

Maybe yours will be finished at the same time :)


JA2(Posted 2009) [#19]
Thanks for the suggestions, Armitage.

I've got it all working quite nicely now, the player can grab edges and pull himself up or let go and drop down. Next I'm going to have a go at ladder/rope climbing :)