I'm developing a game on AndEngine (with Box2d) and on iOS Sprite Kit simultaneously. I want the game's physics to be identical on both platforms. Sprite Kit uses Box2d internally for physics simulation and I'm using Box2d-AndEngine to use it in AndEngine.

In Sprite Kit, I use physicsBody.velocity.
In AndEngine's Box2d, I use body.SetLinearVelocity(x,y).

In Sprite Kit, a physics body's velocity vector is measured in meters per second.

What is the corresponding unit of SetLinearVelocity in Box2d-AndEngine?

  • \$\begingroup\$ I would expect them to use the same units. Have you tried it? \$\endgroup\$ – Anko Oct 11 '14 at 19:53

You have to convert screen pixels (or screen points) to meters and vice versa when you are using Box2D for your game physics. Please read section 1.7 of the Box2D documentation for more information on Box2D units and the scale to use for the best results.

In our current game (iOS, universal app), we use a scale of 1.0 / 64.0 for points-to-meters conversion. Works great.

Example using the above scale:

If your sprite is supposed to move at 200 pixels per second, its Box2D body has to move at 200.0 / 64.0 meters per second (i.e. its velocity vector has the magnitude 3.125).

If your sprite is located at pixel position (64, 64) on the screen, its body is located at (1, 1) in meters.

If possible, you set the Box2D body position and velocity (and other simulated properties) once (or at least rarely) and read back the position every frame in order to update the position of the sprite.

| improve this answer | |
  • \$\begingroup\$ Thanks for the information. But I need bit more clarity. So in your app, 64 pixels is equal to one meter/second i.e Is iOS CGVectorMake(75, 75), setLinearVelocity(75/64, 75/64) in AndEngine ? \$\endgroup\$ – noobgramer Aug 12 '14 at 14:32
  • \$\begingroup\$ I've added an example to my answer \$\endgroup\$ – Raginmari Aug 12 '14 at 14:54
  • \$\begingroup\$ And yes, while Sprite Kit seems to convert values internally, which is good, you have to do it manually when using AndEngine (we use Cocos2d 2.x which does not wrap physics either), so it should be setLinearVelocity(75 / 64.0, 75 / 64.0). The scale you use does not matter much (I think) as long as you follow the recommendations in the Box2D documentation. \$\endgroup\$ – Raginmari Aug 12 '14 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.