Box2d自定义多边形和sprites不匹配 [英] Box2d custom polygon and sprites mis-matching

查看:195
本文介绍了Box2d自定义多边形和sprites不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Physics Editor在Box2d中创建多边形。它生成多边形,在非视网膜显示器工作正常,但不工作在视网膜显示.....我已经附加了两个显示屏的屏幕截图。现在当视网膜显示多边形没有设置

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"]];

任何帮助将被感激。

.......

推荐答案

这是因为cocos2d工作在点(1点是视网膜显示器上的2像素)但box2d绘制身体的方式(调试绘制)使用像素。身体的创造是完美的(如果你考虑到视网膜显示,你的世界将是两个维度的两倍,导致视网膜和非视网膜设备之间的物理差异),但相反,你需要以修复您的调试绘制方法。某些地方将有一行结束新的GLESDebugDraw(PTM_RATIO); - 更改为新的GLESDebugDraw(PTM_RATIO * CC_CONTENT_SCALE_FACTOR()); 你应该是好的。

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.

这篇关于Box2d自定义多边形和sprites不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆