Search code examples
iphonecocos2d-iphonepolygonretina-displaybox2d-iphone

Box2d custom polygon and sprites mis-matching


I am using the Physics Editor for creating the Polygon in Box2d. It generates the Polygon and works fine in the non-retina display but doesn't work in the retina display..... I have attached the screen shot for both of the displays.Now when comes to retina display the polygon is not set over the car here's the image for that

It works completely in non-retina display

It doesn't work with the same polygon as of the sprite - Retina Display

Here's my code which I am using in the Project

CCSprite *car = [CCSprite spriteWithFile:@"opp_car.png"];
[car setPosition:ccp(wSize.width/2+50,wSize.height/2-120)];
[self addChild:car];
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;    
spriteBodyDef.userData = car;
spriteBodyDef.position.Set(car.position.x/PTM_RATIO, car.position.y/PTM_RATIO);
b2Body *spriteBody = _world->CreateBody(&spriteBodyDef);

b2PolygonShape spriteShape;
[[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"opp_car-hd.plist"];
[[GB2ShapeCache sharedShapeCache] addFixturesToBody:spriteBody forShapeName:@"opp_car-hd"];
[sprite setAnchorPoint:[[GB2ShapeCache sharedShapeCache]anchorPointForShape:@"opp_car-hd"]];

Any Help would be Appreciated

Thanks.......


Solution

  • It's because cocos2d works in points (1 point is 2 pixels on a retina display), but the way that box2d draws the bodies (debug draw) uses pixels. The creation of the body is perfect (if you were to account for the retina display in that, your world would be twice as big in both dimensions, leading to differences in the physics between retina and non-retina devices), but instead you need to fix your debug draw method. Somewhere there will be a line that ends new GLESDebugDraw(PTM_RATIO); - change that to new GLESDebugDraw(PTM_RATIO * CC_CONTENT_SCALE_FACTOR()); and you should be good.