Looking for some pointers.

Hey!

I just discovered Box2D yesterday, and it looks great! I've been wanting to create a more physics-driven game for a long time now, but I haven't found a good library to do the hard part for me. But I guess my look is over now. 😉

I've been playing around with Box2D a bit now and I have few, most likely easy to answer questions. I guess I could find the answer on my own by throughly testing stuff, but I figured why not just ask the pros.

My plan is to make a sidescroller, a platformer kind of game. My biggest issue right now is to figure how to work with Box2D and player physics:

1) How do I prevent from the player "rolling", i.e. I want the player only to be able to move sideways, and "lock" the angular velocity.
EDIT. I guess the preventRotation is for this. :p

2) I know that I can move the player by applying forces and/or impulses, but I'm not really sure which one I should use. What is exactly the difference between two? Is an impulse more short-term of an effect or what? Should I use force to move the player sideways, but an impulse to trigger a jump?

3) Right now I'm moving the player with ApplyForce(), but the problem I'm having that the speed of the player becomes way too fast. How do I limit how fast the player can move (also right now the acceleration time to the max speed of the player is too slow, I guess this has to do friction of the player and the ground, I can't really figure how to find out the correct frictions for them). I was trying to play around with GetLinearVelocity and SetLinearVelocity, only changing the x component of it. Am I on the right track?

4) Assume that player is moving to left, and then suddenly changes the direction to right. I want this movement to be pretty much instant, but right now it kind of "lags", because I'm applying a force of same magnitude on the opposite side that the player is already moving to and first the player de-accelerates and then starts accelerating in different direction. This is obviously realistic, but I'm not really looking for realism in this particular case, as it'd compromise how fun the gameplay is.

5) Anyone who has played a platformer knows that you can change a direction mid-air, and that if you stop pressing directional keys (i.e. left or right), the character doesn't "slide in the air". However with Box2D there's no friction in the air, so the player just slides mid-air after jump, as long as he hits ground. What's the best way to implement such air friction, so that the player doesn't slide in the air, but stops (almost) instantly. I guess this has something to do with SetLinearVelocity again?

Also lastly, an irrelevant question, but I'm somewhat of a physics newbie and furthermore english is not my native language, so can someone explain me the difference between forces, impulses and torques (I somewhat understand the first two, just not their actual difference or which one should be used and when).

Thanks for any help to guide my way!
Well, I've been testing stuff out for last few hours, and I got everything working as I wanted. 🙂
The only thing I'm curious about now, is what's the torque. I'm assuming it has something to do with angular velocity.

Another unrelated question, is there some way to apply "constant" forces on bodies. For example gravity (which is of course already implemented in b2World). For example say I want to have wind in my game, what would be the best way to achieve that?
You *could* just modify the x-gravity, should give you the effect you're looking for.
Let me know if you figure out how to check if the player is standing on ground to prevent mid-air jumping.... I'm also making a side scrolling shooter (with a couple fancy features of course).
Wikipedia is a great place to learn physics concepts:
http://en.wikipedia.org/wiki/Torque

You can use linear damping to slow down your character.

You can iterate over a body's contact list to see if it's touching the ground.

I haven't tried to program a character in Box2D. It would be great if someone would contribute a test-bed example.
Thanks for the reply! I'm not really sure what you mean with linear damping. I know what the term means, but I'm not sure if you mean that I should manually "damp" the velocity by using SetLinearVelocity(), or is there some build-in mechanism for linear damping (I can see there's a m_linearDamping member variable in b2Body, but I'm not sure if this is just an internal variable or something I can use, since there's no setter/getter for it).

Anyways, I've got couple of another questions if you don't mind. 🙂

1) Is there a way to get an area of a body (I assume you calculate this somewhere, since you can get the mass)?

2) Can I somehow make two bodies in-collapsible with each other using collision masking, but still have a way to check whether those two given bodies intersect (or check if there's contact points). I want to do this because I'd like to use Box2D to simulate water ripples for me, but I want to apply all forces, impulses, etc. on it manually, and not have any objects falling in water to automatically trigger any forces on the water body. But I still need a way to check if an object (body) collides with water (body as well), to see if I should apply a custom force on it (I can't just use a constant water level because depending on how strong the ripples are, the water level can rise or lower).

3) Any pointers on how I should setup forces to deal with water so that light objects would float, given that I know the gravity and the "average" water level?

4) What exactly is inertia and is the return value of GetInertia() clamped into some range? I looked it up on Wikipedia, but can't figure out any practical use for it.

Thanks again!
You can use the linear damping setting in b2BodyDef.

You'll have to compute the area yourself. Buoyancy is a planned feature. Fortunately it is easy to compute the area of circles and convex polygons.

Sensors are another planned feature. Search these forums for a recently posted solution.

Did you read this? http://en.wikipedia.org/wiki/Moment_of_inertia
reko wrote:
Well, I've been testing stuff out for last few hours, and I got everything working as I wanted. 🙂


Questions 2-5 are exactly what I need answers to. Can you please share what your conclusions were?
Sorry, I'm sure I replied before but I apparently didn't. 😛 Here you go:

2) There's not really the one and "right" way to do it how I see it. It really depends on what kind of simulation you want to do. I ended up using impulses to move my character, because the acceleration time is much shorter with it. It really depends on what you're after, and you really get the best results by experimenting.

3) Well this was a two part question. I already answered the "acceleration time to the max speed of the player is too slow" in my previous answer (the answer was to use an impulse instead of force). To limit the max speed, I simply check before applying an impulse whether the x component of GetLinearVelocity is too big; If it is I won't apply an impulse. This way other forces can still make the player go faster than the "max speed" of movement I've defined.

4) Using an impulse instead of a force and having a small friction on the player solved this one. It's important to have small friction so your player moves smoothly on a ground and i.e. after jump won't "freeze" for a while before being apple to move again. I use a friction value of 0.1f for my player at the moment.

5) I solved this by damping the horizontal velocity by using GetLinearVelocity and SetLinearVelocity, but as stated in this topic, there's a setting to set linear damping for a body, so you probably want to try that first.



Now back to my own questions.

1) What's the best way to calculate an area of a convex poly? I'm thinking of adding together all the vectors and finding out the center point by taking the average, and then treating each subsequent 2 vertices as a triangle with the center point and sum all the areas of the triangles. Is there a better way to do this?

2) I've been trying to get realistic water (with waves and ripples being produced by falling objects etc.) working in my engine, but it's proving to be very hard to get working properly. I tried to do the water surface with bodies + joints first (like a bridge), but I couldn't get this to produce wanted results at all (my idea was to have a set of upwards facing forces positioned underwater to "negate" the gravity). Does anyone have any pointers for simulating 1D water, not only the buoyancy but also dynamic waves (doesn't have to be absolutely realistic, but it should look somewhat good and work efficiently enough. To be honest I'd prefer to keep it as simple as possible)? I've been trying to read various tutorials and papers but no luck! Since buoyancy is a planned feature, does that also mean that simulating water will be easier in future versions? I'm not in hurry in developing my engine, but is there any kind of estimate when these features will be realised (excluding censors, already read how to implement them now! Thanks for that 😉 )

Thanks for your excellent work with the box2D and for the help! I really appreciate your efforts.
Your method for computing the area should work. Just keep in mind that the vertex average is not the center. You can find out more about this in Computation Geometry in C by O'Rourke.

See this paper for some ideas for simulating waves and buoyancy: http://www.matthiasmueller.info/talks/g ... ds2007.pdf
Re: area, to break a convex polygon into triangles you don't even need to use the center (barring precision issues). You can just make a fan with a tip at any point on the polygon.

Even that is probably more complex than needed, though, as once you simplify the result, you get a pretty formula:

Assuming that x[N] and y[N] are equal to x[0] and y[0] resp, (the polygon is closed)

Area = .5 * Sum( x[i]*y[i+1] - x[i+1]*y[i] )

where the sum runs from i=0 to i = N-1. If I'm correct, this works even for nonconvex polygons. See http://local.wasp.uwa.edu.au/~pbourke/g ... /polyarea/ for more (Paul Bourke's pages have a lot of useful geometry info, I often turn there first when I need something like that, though I don't tend to use his code because it doesn't appear to be offered under an open license).

Then again, it seems to me that there's a simpler solution: use the PolyMass(b2MassData* massData, const b2Vec2* vs, int32 count, float32 rho) method that Box2d uses to calculate the mass, giving it a rho of 1f, and then reading off the mass from the massData object. Is there some reason this wouldn't work?

Original thread: https://box2d.org/forum/viewtopic.php?t=160