视网膜显示设备上精灵与 box2D 主体的偏移

发布于 2024-12-04 06:06:21 字数 1432 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2024-12-11 06:06:21

另请注意,您可能必须在使用 PTM_RATIO 宏的任何位置进行更改以包含 CC_CONTENT_SCALE_FACTOR()。
像这样的事情:

 b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO)

可能需要移动到这个:

b2Vec2(pixel.x / PTM_RATIO * CC_CONTENT_SCALE_FACTOR(), pixel.y / 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:

 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())
微凉 2024-12-11 06:06:21

这只是调试绘制错误。你可以这样排列:

 glPushMatrix();
 glScalef(CC_CONTENT_SCALE_FACTOR(), CC_CONTENT_SCALE_FACTOR(), 1);
 world->DrawDebugData();
 glPopMatrix();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文