视网膜显示设备上精灵与 box2D 主体的偏移
我正在使用 cocos2d 和 box2d 制作我的第一个游戏,并发现了一些非常奇怪的东西。我有多个 box2D 主体在周围飞行,当我打开调试绘制时,我看到在我的视网膜显示设备上,box2d 主体的位置就像在 480x320 设备上一样,即位于屏幕的左下象限,但精灵是在正确的位置。
例如,一个应该位于 (720,320) 的主体将使其精灵位于此位置,并将其 box2D 主体绘制在 (360,160)。
交互正在发生,只是偏移,所以一切正常...
当我在运行 480x320 显示屏的设备上测试游戏时,一切都正确排列。显然,在非视网膜显示屏的翻译中我遗漏了一些东西设备到视网膜显示设备,但我似乎无法解决。
生成主体的示例方法是:
-(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]
}
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]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另请注意,您可能必须在使用 PTM_RATIO 宏的任何位置进行更改以包含 CC_CONTENT_SCALE_FACTOR()。
像这样的事情:
可能需要移动到这个:
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:
may need to move to this:
这只是调试绘制错误。你可以这样排列:
It's just debug draw that's wrong. You can line it up like this: