移动 CCLayer 后,Box2d 主体不与动画对齐
我正在开发一款自上而下风格的游戏,用户可以同时控制多个角色。这些角色可以向任何方向移动。角色可以使用 Box2d 进行碰撞检测来碰撞静态物体(墙壁、建筑物)。用户可以通过在屏幕上拖动手指来移动相机。这将移动 CCLayer 以及我添加到图层中的任何包含 CCSprite 对象的内容。
这工作得很好,但后来我意识到移动图层和精灵并没有移动它们所附着的 Box2d 主体。尸体继续按照原来的投影移动。我一直在寻找答案,但似乎没有人遇到过这个问题,这让我相信我的方法可能不对。
我考虑过移动相机而不是图层,但经过大量谷歌搜索后,似乎大多数人都不赞成这个想法。那么有人有什么建议吗?
I'm developing a top down style game where the user can control multiple characters at one time. These characters can move in any direction. There are static bodies (walls, buildings) that the characters can bump into using Box2d for collision detection. The user can move the camera around by dragging his finger across the screen. This will move the CCLayer and any containing CCSprite objects that I added to the layer.
This worked just fine, but then I realized that moving the layer and the sprites did not move the Box2d bodies that they were attached to. The bodies continue moving in their original projections. I've been searching for an answer, but it doesn't seem like anyone has had this problem which leads me to believe that my approach may be off.
I considered moving the camera around instead of the layer, but after a lot of google searching it seems that most people frown upon that idea. So does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的方法有点不对劲。除了碰撞检测之外,我没有使用 Box2D 进行任何其他操作,因此我不应该使用它在屏幕上移动精灵。我更新了代码,让 cocos2d 处理精灵的定位,然后根据精灵的位置更新了 box2d 主体位置。
这是我发现有用的教程:
http://www .raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
该教程解释了如何根据你的 cocos2d 精灵的位置。此外,由于我要移动图层,因此我必须对教程代码进行轻微修改。这是:
CCSprite *sprite = (CCSprite*)b->GetUserData();
CGPoint pos = [gameLayer ConvertToWorldSpace:[精灵位置]];
b2Vec2 b2Position = b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(b2Position, b2Angle);
方法
convertToWorldSpace:(CGPoint)point
获取精灵的位置并将其位置转换为基于世界空间的位置关于如何在屏幕上移动 CCLayer 位置。我希望其他人觉得这有帮助!
My approach was off a bit. I'm not using Box2D for anything other than collision detection, so I should not have been using it to move my sprites around the screen. I updated my code to have cocos2d handle the positioning of my sprites and then I updated the box2d body locations based on the sprite's locations.
Here's a tutorial I found helpful:
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
That tutorial explains how to update the body locations based on your cocos2d sprites' locations. In addition since I was moving the layer around I had to make a slight modification to the tutorials code. Here it is:
CCSprite *sprite = (CCSprite*)b->GetUserData();
CGPoint pos = [gameLayer convertToWorldSpace:[sprite position]];
b2Vec2 b2Position = b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(b2Position, b2Angle);
The method
convertToWorldSpace:(CGPoint)point
takes the location of the sprite and converts it's location to world space based on how you have the CCLayer position moved around the screen.I hope someone else finds this helpful!