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\$
    – House
    Commented Apr 24, 2012 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
    Commented Apr 24, 2012 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
    Commented Apr 24, 2012 at 17:51

1 Answer 1

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
    Commented Apr 24, 2012 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\$
    – House
    Commented Apr 24, 2012 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .