0
\$\begingroup\$

In a game, we add a body for each sprite, the method is:

createBodyAtLocation:(CGPoint) forSprite:(Box2DSprite*)sprite

In which we create the body and we attach it to the sprite in parameter: sprite.body = body;

Then we set the position of the body, but later on in the code, in the update method, we also set the position of the sprite to the same position as the body. I was wondering: Why are we doing it twice? As we already set the position of the body in the world, and we made a link between the two with sprite.body?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You're asking people why you're doing this in your code? I'm confused. \$\endgroup\$ – MichaelHouse Apr 24 '12 at 16:47
  • \$\begingroup\$ @Byte56 : yes because it's not my own code, i'm reading a book and i'm not sure to understand why we do this? i think it's a general rule in box 2d, isn't it? \$\endgroup\$ – Paul Apr 24 '12 at 16:53
  • \$\begingroup\$ @Byte56 : thanks, ok i thought the sprite was the container of the body. can you post an official answer? \$\endgroup\$ – Paul Apr 24 '12 at 17:51
1
\$\begingroup\$

The sprite is the graphical representation and the body is the physical representation. They don't need to have the same position, even though they often will. This is likely the reason you can or need to set the positions separately.

\$\endgroup\$
2
  • \$\begingroup\$ can you please tell me something : the body is actually contained in the box2DSprite, and we "send" the box2dSprite to the batch node. The body and the sprite frame are in the same box right? why the position of the body is not relative to the local coordinates of the box ? (here, we set the two, body and sprite, to have the same location, as if they were both relative to the screen) ? thanks \$\endgroup\$ – Paul Apr 24 '12 at 18:38
  • \$\begingroup\$ One is being used by the graphics system, the other by the physics system. One being relative to the other would be confusing. \$\endgroup\$ – MichaelHouse Apr 24 '12 at 18:43

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.