Box2d custom polygon and sprites mis-matching

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:22:59

问题


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

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.......


回答1:


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.




回答2:


Just add [car setPosition:ccp(0,0)];

It will work :)



来源:https://stackoverflow.com/questions/8710268/box2d-custom-polygon-and-sprites-mis-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!