I have a CCSprite (in which i have a b2body); The scope is to create a sprite that moves with a body to a location and back;
I create the b2body in this way:
if (_body)
{
_world->DestroyBody(_body);
}
b2BodyDef bd;
bd.type = b2_kinematicBody;
_body = _world->CreateBody(&bd);
b2PolygonShape shape;
b2Vec2 _p1, _p2 , _p1sub,_p2sub;
_p1 = b2Vec2(point1.x/PTM_RATIO,point1.y/PTM_RATIO);
_p2 = b2Vec2(point2.x/PTM_RATIO,point2.y/PTM_RATIO);
shape.SetAsEdge(_p1, _p2);
_body->CreateFixture(&shape, 0);
After i created the body, i initialize the action:
id a1 = [CCMoveBy actionWithDuration:1 position:ccp(0,-self.position.y+destinationPoint.y)];
id action2 = [CCRepeatForever actionWithAction:
[CCSequence actions: [[a1 copy] autorelease], [a1 reverse], nil]
];
[self runAction:action2];
on each frame, i update the body location
_body->SetTransform(b2Vec2(self.position.x/PTM_RATIO,self.position.y/PTM_RATIO), 0);
The thing is that they are both drawn but the sprite is in the correct position while the body is not; the moves are coherent
FYI i use PTM_RATIO through out the game so this is surely not the problem
Keep in mind that the sprite’s position will be an offset to its parent’s position. If the parent is not a CCNode, CCScene or CCLayer (or their position is not 0,0) the sprite will be offset by the parent’s position.
For example, a sprite (let’s call it S) with dimensions (contentSize) of 100,100 is located at position 0,0. Now you add a child node to S. The child node’s position (unless modified) will be at -50,-50 due to the fact that child nodes orient themselves on their parent’s lower left corner instead of the parent’s actual position. This frequently throws even the most experienced cocos2d programmers off guard.