Reputation: 1322
I'm making my first game with cocos2d and box2d and have discovered something quite strange. I have multiple box2D bodies flying around, when I turn on the debug draw I see that on my retina display device, the box2d bodies are positioned as if on a 480x320 device, i.e. in the bottom left quadrant of the screen, yet the sprites are in the correct position.
For example, a body supposed to be at (720,320) would have its sprite located at this position and its box2D body drawn at (360,160).
The interactions are taking place, just offset, so everything works ok...
When I test the game on a device running a 480x320 display everything is correctly lined up.Obviously there is something I'm missing in the translation from non-retina display device to retina display device but I can't seem to work it out.
An example method that produces a body is:
-(void)addArrow{
CGSize winSize = [CCDirector sharedDirector].winSize;
arrow = [CCSprite spriteWithFile:[gameSettings objectAtIndex:0]];
arrow.position = ccp(bowBar.position.x,bowBar.position.y);
arrow.tag = 1;
// Create ball body
b2BodyDef arrowBodyDef;
arrowBodyDef.type = b2_dynamicBody;
arrowBodyDef.position.Set(arrowPoint.x/PTM_RATIO, arrowPoint.y/PTM_RATIO);
arrowBodyDef.userData = arrow;
b2Body * arrowBody = world->CreateBody(&arrowBodyDef);
arrowBody->SetTransform(arrowBody->GetPosition(),- CC_DEGREES_TO_RADIANS(bowBar.rotation));
b2PolygonShape rectangle;
rectangle.SetAsBox(arrow.contentSize.width/PTM_RATIO/2, arrow.contentSize.height/PTM_RATIO/2);
b2FixtureDef rectangleShapeDef;
rectangleShapeDef.shape = &rectangle;
rectangleShapeDef.density = 150.0f;
rectangleShapeDef.friction = 1.0f;
rectangleShapeDef.restitution = 0.0f;
arrowFixture = arrowBody->CreateFixture(&rectangleShapeDef);
[self addChild:arrow z:15]
}
Upvotes: 0
Views: 872
Reputation: 11
Also be aware that you may have to change wherever you use the PTM_RATIO macro to include CC_CONTENT_SCALE_FACTOR(). Something like this:
b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO)
may need to move to this:
b2Vec2(pixel.x / PTM_RATIO * CC_CONTENT_SCALE_FACTOR(), pixel.y / PTM_RATIO * CC_CONTENT_SCALE_FACTOR())
Upvotes: 1
Reputation: 32066
It's just debug draw that's wrong. You can line it up like this:
glPushMatrix();
glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1);
world->DrawDebugData();
glPopMatrix();
Upvotes: 0