14

I'm using box2d for physics simulation. I'm moving a circle using arrow keys by applying impulse on the body when ever a key is pressed. Unfortunately, the circle moves excruciatingly slow and doesn't seem to accelerate like a true physical body is supposed to. My world's dimensions are 400x800 pixels. The radius of the circle body is 20f.

According to this, the problem can be solved by scaling the circle radius down when creating it and scaling up after getting the body position during painting. It doesn't seem to make any difference at all. Is there some step or setting I'm missing here ?

Code on pastebin

Community
  • 1
  • 1
ivymike
  • 1,391
  • 2
  • 17
  • 27
  • Could you share the relevant code? E.g. construction of the body etc. – Tom Feb 08 '13 at 14:04
  • @Tom Added link for code on pastebin. I'm using box2d with libgdx game engine - so the code is in Java. – ivymike Feb 08 '13 at 14:14
  • 1
    Are you actually applying the impulse on the circle somewhere? `ApplyLinearImpulse` – Tom Feb 08 '13 at 14:23
  • @Tom Yes. I'm doing that right after determining the impulse (updated the code to reflect that) : fixture.getBody().applyLinearImpulse(impulse, fixture.getBody().getWorldCenter()); – ivymike Feb 08 '13 at 15:18
  • 1
    Aside from the density being fairly high, I can't see anything out of the ordinary with what you've pasted. What kind of debugging have you done? Have you tried hard-coding an impulse to apply, rather than relying on key-press etc? – Tom Feb 08 '13 at 15:37
  • @Tom Yup. Hard coded impulse just to see if it works. Didn't make any difference. Also tried to set linear velocity directly, apply force etc. I know that these calls have some effect because they do move the body till it reaches some velocity. But after that, it simply doesn't seem to increase velocity at all. – ivymike Feb 08 '13 at 15:42

2 Answers2

27

It took me pretty long to understand that now, but there actually really IS a hard limit on velocity in Box2D. See here:

There is a maximum movement limit of 2.0 units per time step, given in the file b2Settings.h in the source code.

If you have one step per frame at 60FPS, then your bodies can move at a maximum speed of 120m/s. Maybe this will save somebody a bit of time one day.

noone
  • 18,807
  • 5
  • 58
  • 75
4

Yes, there is a difference between which size do you use. Box2d operates over floating point numbers and thus it's precision is limited and simulation quality differs. Box2d is tuned to work best with dynamic bodies of size 0.1 to 10.

So generally you should prefer to use one units for physics and other for graphics

Now to your question. Body's velocity is affected by collisions, friction (if one body is sliding over the other) and by linear damping. So check what value for linear damping do you use (it's non-zero by default as I remember). There are no velocity limitations, as far as I know, but higher velocity will provide less quality collisions if using the same time step.

Andrew
  • 22,844
  • 11
  • 57
  • 88