So, the physics is superb, however I'm having problems getting nice feeling controlls of the player and enemies. The problem, which you're probably already familiar with, is that to switch direction you have to first counter all the movement for the current direction. I'd love to get some pointers on how to solve this, I'm currently using ApplyForce() which seems to not be the optimal way.
Player Controller
General DiscussionI think you should be able to use something like:
This will set that body's velocity to stopped, effectively.
Code:
b2Vec2 zeroVel (0, 0);
body->SetLinearVelocity(zeroVel);
body->SetLinearVelocity(zeroVel);
This will set that body's velocity to stopped, effectively.
Doh, the boardsoftware ate my post 🙁
Anyway, thanks for the help, it will help a lot in fine tuning the controller.
I have some more problems though which I'd love to get some help with.
First of all I let 1 box2d unit represent 1 pixel in my 2d renderer, which is pretty convinient in one way. However, I'm starting to wonder if perhaps it's causing some anomalities which I'm seeing due to precision issues. That's just a wild guess though, now to the problems at hand.
I've uploaded a small project which hopefuly will run on most machines: http://www.megafileupload.com/en/file/11150/proj-zip.html. The box, which is to become the player, can be controlled with the arrows. As you can see the box likes to get stuck in the ground. Also, it's somewhat bouncy even with resitution set to 0. If you run in one direction when the box rests on the static boxes it takes of, which could probable by solved by applying a somewhat downwards force when running when the getting stuck on ground problem is solved, would still appreciate some teories of why it's happening though.
This is how I move the player currently
pDelta is the delta time.
I've tried to use ApplyForce(), which while working requires really really high speed values to cause any effect, the pDelta is most likely the culprit but I love me some frame rate independent movement.
For my simulation I use 10 iterations and the delta time as timestep.
Any help appreciated!
Anyway, thanks for the help, it will help a lot in fine tuning the controller.
I have some more problems though which I'd love to get some help with.
First of all I let 1 box2d unit represent 1 pixel in my 2d renderer, which is pretty convinient in one way. However, I'm starting to wonder if perhaps it's causing some anomalities which I'm seeing due to precision issues. That's just a wild guess though, now to the problems at hand.
I've uploaded a small project which hopefuly will run on most machines: http://www.megafileupload.com/en/file/11150/proj-zip.html. The box, which is to become the player, can be controlled with the arrows. As you can see the box likes to get stuck in the ground. Also, it's somewhat bouncy even with resitution set to 0. If you run in one direction when the box rests on the static boxes it takes of, which could probable by solved by applying a somewhat downwards force when running when the getting stuck on ground problem is solved, would still appreciate some teories of why it's happening though.
This is how I move the player currently
Code:
mBody->ApplyImpulse( b2Vec2( mSpeed * pDelta , 0 ), b2Vec2( 0.0f, 0.0f ) );
pDelta is the delta time.
I've tried to use ApplyForce(), which while working requires really really high speed values to cause any effect, the pDelta is most likely the culprit but I love me some frame rate independent movement.
For my simulation I use 10 iterations and the delta time as timestep.
Any help appreciated!
Box2D is tuned to work with units of meters-kilograms-seconds (MKS). This is tunable in b2Settings.h, but it might be easier for you to just use meters. First try using a character that is about a half meter wide.
There is a chance that your character box is hitting the corners of the ground boxes. Try one large ground box first. You should use a force to move your character and some small ground friction.
You really should provide a constant physics step. If your frame rate varies then take multiple time steps. Box2D would need some modification to work correctly with a varying time step.
There is a chance that your character box is hitting the corners of the ground boxes. Try one large ground box first. You should use a force to move your character and some small ground friction.
You really should provide a constant physics step. If your frame rate varies then take multiple time steps. Box2D would need some modification to work correctly with a varying time step.
Ah, I did some scaling and let 100 pixels represent 1 meter, which is much more representetive. Used a the ground box from the HelloWorld and sure enough, it moves a lot better over the surface and doesn't get stuck as easily. However, if I let the box come to an halt it will freeze in place, no matter how much force I apply to it. Another thing I've noticed is that the ground box friction doesn't seem to affect the box at all. Also, I don't know if it means anything at all, but the positioning of the box relative to the colliding ground always seems to be 2 pixels off, as seen in:

Really appreciate all the help.

Really appreciate all the help.
Did some more testing, seems like the body falls asleep even if I've set doSleep to false.
Spotted another problem, don't know if it's an error in my 2drenderer or if it's in box2d yet, but about once every second positions flicker. Going to do some more testing on that front.
Spotted another problem, don't know if it's an error in my 2drenderer or if it's in box2d yet, but about once every second positions flicker. Going to do some more testing on that front.
Heh, I bet the box is falling asleep. I don't wake things when you apply a force/impulse. If you have the latest from SVN, there should be an option in b2BodyDef to disable sleep on your character.
Box2D always allows a little bit of overlap. This helps to reduce jitter. The amount of overlap is b2_linearSlop. Don't set this to zero! 🙂 Just make your collision box a little bit bigger than your character graphics.
Okay, I just saw your new post. Try calling body->WakeUp() before you apply the force. I'll test the doSleep flag.
Box2D always allows a little bit of overlap. This helps to reduce jitter. The amount of overlap is b2_linearSlop. Don't set this to zero! 🙂 Just make your collision box a little bit bigger than your character graphics.
Okay, I just saw your new post. Try calling body->WakeUp() before you apply the force. I'll test the doSleep flag.
Ah, worked like a charm. Switched back to my old boxes, seems like they're very slightly rotated and that's causing the player to get stuck in between them, working to solve that one now though, thanks for all the help!
Fixed the boxes, however I think it will be hard to use ground tiles without getting stuck at the seems so I'm switching to a ground collision box for now.
You can probably avoid snags by using circle collision at the bottom. Circle shapes will be supported soon. For now you can approximate them with octogons.
Original thread: https://box2d.org/forum/viewtopic.php?t=19