How to move a charcter manually in Box2d?

BlitzMax Forums/Brucey's Modules/How to move a charcter manually in Box2d?

Rico(Posted 2008) [#1]
Hello! I'm hoping using Box2d to write a game. Although I'm just messing around with the examples at the moment, so I'm a bit green. Can someone tell me the technique of how to do this? (if it is possible).

I have already written a simple 2d platform game in Bmax, and I have the main character moving around how I like. I would like to convert this to box2D so his movement is the same (ie not using true physics but setting his movement manually each frame). All the other objects in this game would operate under physics and therefore would need to be moved by him. I would like the main character to just push everything out of the way. So his movement is never interrupted or slowed in any way, by an object. He would however of course be stopped by a wall or when he can't push an object because it is against a wall. He can also jump on to and off objects.

I was thinking this could maybe be done by using the SetPosition command every frame and by giving my main character a large density I don't know if this would work though, because wouldn't the DoStep command then alter his position?

Please help. Thank you!


Armitage 1982(Posted 2008) [#2]
I don't know well since I choose to use a Physics engine to process every interactions in my world.

Why not trying to set a large amount of mass for your player and a very tiny mass for others objects.
Then setting global gravity with 0,0 and processing each objects with a custom ApplyForce.
Tweaking player and objects friction.
Use contact points to control collision against wall and all others objects.

There is no SetPosition method for b2Body only with b2BodyDef.
RE-Creating each frame the body may not be the best solution... But who knows !

Box2d is based on Impulsion forces so it should be possible to reach such a result by tweaking objects values.

All depend on the way your player is moving and the forces applied to others objects (gravity, force, torque, etc.)

Another solution would be using sensors.
A sensor is a shape that detects collision but does not produce a response.
Add 2 shapes by object.
Set the first one as a sensor and add a collision mask to the second one in order to prevent collision with main character.
Then all you have to do is applying impulse / force to the object when is sensor overlap the main character.

Don't forget there is a Box2D forum ( http://www.box2d.org/forum/ ) to ask question relative to Box2d itself ;)


Rico(Posted 2008) [#3]
Thanks Armitage - you are very helpful! Am I right in that you are saying you can only set the body's position when you initially define it?

Can you get a platformer up and running that has similar control to the classics - e.g Mario, Sonic etc by using forces + impulses? I am worried I will not be able to the control right using this system. I will definitely try though!

I have now also posted a thread on the Box 2D forum- which is very good.

Thanks - Rico.


Space_guy(Posted 2008) [#4]
well there is one commands to move a boyd after its creation
SetXForm

it seems to work fine for me


Armitage 1982(Posted 2008) [#5]
Well spotted Space_guy ! Thanks :)

SetXForm 5.4.3. Position and Velocity in the manual.


if it can reassure you Rico I've come to realize this : http://arm42.free.fr/files/Metagolf.rar
It's basic and WIP (this is why the background and the main Mario sprite isn't mine, the rest is though) but it's more or less the same control that you see in Mario.
Only with Impulsion, hard gravity force (-90 * _body.GetMass()) and timer to control various jumping level with pressure button.
It's just a question of tweaking really.


Brucey(Posted 2008) [#6]
btw, if you see any gaping holes in the manual, please let me know. (it's available via Third Party Modules->bah.box2d).

thanks.


Rico(Posted 2008) [#7]
That's very good Armitage! I certainly think with time I will be able to come up with something good too. I am a beginner at the moment, but I am looking forward to learning! So far I like Box2D better than Chipmunk - its seems more flexible. Keep working on your game, what I've seen so far is excellent :)
Yes - you'll definitely have to change the main character - but not to a blue hedgehog. That probably wouldn't be a good idea (unless you have a lot of money)
BTW do you think its possible to do loops and curved surfaces in Box2d? I suppose you could do approximate curves with lots of angled lines.


Brucey - the manual is good but its annoying not being able to use the Find function on it (not your fault though). Thanks for converting Box2D! I was originally trying to write my own physics engine but it was very very very bad. My maths is pretty awful, so you can see why.


Armitage 1982(Posted 2008) [#8]
@Rico
Thanks for the comment ;)
As far as your shapes stay Convex everything is possible !
Note there is a maximum number of polygon vertices with Box2d (the default b2_maxPolygonVertices in b2Settings is set to 8).
There is also some others good settings to look at inside b2Settings (and don't forget to rebuild the Brucey module after changing them ;).

I read at the Box2d forum that someone is developing a Convex shape decomposition (http://www.box2d.org/forum/viewtopic.php?f=4&t=83) Sources available.
With this you should be able to extend box2d shapes a little more :)
I didn't use this for the moment since my current shape are simple enough to stay convex.

Take a look at Blide http://www.blide.org/
This IDE have search documentation function as well as contextual help shorcut (F1) and many others useful tools.
I can't believe working without it ^^ Ziggy made an awesome work on this ! Constantly updated too which is great :)


Rico(Posted 2008) [#9]
Yes that Convex Shape decomposition looks like it will be useful! - as does the new IDE (I will try it out soon)

I've just been messing around with my character control, and I have found that you can use body.SetLinearVelocity when moving a character. Then just reset it to zero at the beginning of the next loop. By doing this I can get my man to move very small steps.
I'm going to try it with Impulses next - to see whats best.

Rico