Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
1  
You're asking people why you're doing this in your code? I'm confused. –  Byte56 Apr 24 '12 at 16:47
    
@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? –  Paul Apr 24 '12 at 16:53
    
@Byte56 : thanks, ok i thought the sprite was the container of the body. can you post an official answer? –  Paul Apr 24 '12 at 17:51
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks Byte56 for the answer –  Paul Apr 24 '12 at 18:20
    
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 –  Paul Apr 24 '12 at 18:38
    
One is being used by the graphics system, the other by the physics system. One being relative to the other would be confusing. –  Byte56 Apr 24 '12 at 18:43
    
alright, i'll keep this in mind, thanks –  Paul Apr 24 '12 at 21:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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