tags:

views:

1759

answers:

2

Flash Box2D Question (But I guess the "flash" part doesn't matter)

Suppose there is an object A, which is the character and an object B, which is the ground

Suppose the character can only walk when he touches the ground.

There is a box 2d contact listener, which I can detect when an object touches another objects, so I can use it to listen when the character touches the ground. But how do I know if the character has left the ground?

Thanks for reading.

+4  A: 

Suppose there is an object A, which is the character and an object B, which is the ground

Suppose the character can only walk when he touches the ground.

Assuming your object A is b2Body called objectA and objectB is another b2Body called objectB and you world is called m_world. If objectB is the ground I guess it has a density of 0 which makes it static. If you want to check if objectA is above objectB you have two options, either through the bodies themselves which means using box2d unit measurements ( 1 meter = 30 pixels ) , either using the reference to your custom clips set through the userData property.

The advantage of using the second, I guess, is that you will be using the regular pixel units, bare in mind that you have your registration point in the center not at 0,0.

So,in your update loop( where you call m_world.Step ) you would have something like:

if((objectA.m_userData.y + objectA.m_userData.height*.5) < (objectB.m_userData.y-objectB.m_userData.height*.5)) trace('objectA is above ground');
else trace('objectB is on the ground');

Just read you comment and updated again.

I started writing a jsfl command for flash that gets the x,y,width, height from elements on stage and generated the box2d code. I used it to make this simple game: http://www.disturbmedia.com/games/breakfast It seems I don't need to reinvent the wheel as someone else already made something pretty consistent. Just got back from LFPUG where Carlos Ulloa talked a bit about box2d as well and he pointed to this thing:

http://www.sideroller.com/wck/

It seems to generate coordinate arrays for more complex shape definitions which is what you need. In a way this isn't the complete answer to your specific question, but it's a step forwared.

From your description, the game sounds really interesting. Keep us updated :)

George Profenza
the objects in my flash is always moving :p they are some life form which keep doing actions.. but I need to know whether they are on the ground or not. So isSleeping won't help in ground detection. Many thanks for your answer tho.
Unreality
I have edited my answer according to your comment. Hope this helps.
George Profenza
Box2D does not assume any pixel units. 1 unit != 30 pixels!
Zifre
@zifre I meant Box2DAS3. I don't know how it is in C++, plus this came handy when I got stared in Flash:"The first one is Box2D measuring units that may seem weird until you realize Box2D works with meters where 1mt = 30 pixels."quoted source is here:http://www.emanueleferonato.com/2008/11/16/understanding-pixels-and-meters-with-box2d-and-how-to-select-an-object-with-mouse-part-1/I hope I'm not wrong about the as3 version of Box2D because everything worked fine for me using this unit.
George Profenza
Box2D - all versions - lets you define what the ratio is in your code. The prewritten example files (such as helloworld.as) come with a setting of 1m=30px, and has given rise to the whole "1=30" myth. That said, most applications that use Box2D use 1=30. :)
Andy Moore
@Andy I didn't know that. So you mean I could use 1m=1px and not need to do all those multiplications to 30 or 0.3333333333 ? That would be awesome.
George Profenza
what if the ground is not a rectangle? what if the avatar is not a rectangle? ;p I'm looking for a Box2D method ofbodyA.isTouching(bodyB) or bodyA.isColliding(bodyB) ...I believe Box2d has such kind of function internally?It's troublesome to write my own polygon hit test for box2d, because a body can contain many different shapes, and probably my own test hit function won't be as cpu saving as box2d test hit function
Unreality
@Unreality just read the comment and updated the answer.
George Profenza
Many thanks, I have to vote your answer up, but since I still need somebody to post the box2d "isTouching/isColliding" function.. so I don't wanna mark this question 'answered'. Sorry about that.
Unreality
@Unreality No problem. It's annoying that I don't have the time to look into it in depth right now. Glad I could help anyway.
George Profenza
A: 

In the contact listener , there is a method called Remove(b2ContactPoint) . This method is called by the physics engine two bodies are no longer touching. In other words, they have no contact points anymore . You can detect at that point that the body A which is the actor is not floating.

Another cool solution will be to raycast from the Actor to the ground, check the distance between them and you can tell if you are floating or walking. Hope this works

sayjava