简体   繁体   English

Box2d物理实体与Cocos2d Sprite不同步

[英]Box2d physics body becoming out of sync with Cocos2d sprite

I am fairly new to cocos2d and box2d. 我对cocos2d和box2d相当陌生。 I am attempting to create a game horizontal endless runner using the accelerometer. 我正在尝试使用加速度计创建游戏水平无尽奔跑者。 I have the character falling from the top and is constantly moving downwards. 我的角色从高处掉下来,并不断向下移动。 The problem that i am running into is that overtime the sprite and the physics body become out of sync and the physics body moves away from the sprite thus causing wierd behavior with the collision. 我遇到的问题是加班的时间,子图形和物理实体不同步,并且物理结构从子图形移开,从而导致碰撞时产生奇怪的行为。 This only happens when ran on the device, because the player is constantly moving down, the collision is becoming out of sync vertically and the horizontal remains correct. 仅当在设备上运行时才会发生这种情况,因为玩家不断下移,碰撞在垂直方向变得不同步,水平方向仍然正确。 My step function is : 我的步进函数是:

const float32 timeStep = 1.0f / 30.0f;
const int32 velocityIterations = 5;
const int32 positionIterations = 1;

// step the world
world->Step(timeStep, velocityIterations, positionIterations);

How great a distance are we talking about? 我们在说多远? If you are moving a Box2D body over a very large distance (thousands of Box2D meters) you may run into inaccuracies in either the physics simulation or cocos2d positions (or both) that could lead to the effect you described. 如果将Box2D实体移动很长的距离(数千个Box2D米),则可能在物理模拟或cocos2d位置(或两者)中都出现误差,这可能会导致您描述的效果。

2D Physics engines aren't designed to work in a world without constraints (ie endless scrolling). 2D物理引擎并非旨在在没有任何限制(即无限滚动)的世界中工作。 If that's what you're doing, you'll have to come up with a solution where the world is constraint but the player doesn't realize this. 如果这是您正在做的事情,那么您将不得不提出一种解决方案,其中世界受到约束,而玩家却没有意识到这一点。 A common way is to reset the position of objects and associated views back to the top of the (constrained) world once they pass a certain threshold. 一种常见的方法是,一旦对象和关联的视图超过某个阈值,便将它们的位置重置回(受限)世界的顶部。 Most games just lock the player in place and instead move the world underneath the player to give the impression of endless movement, whereas only the background is moving. 大多数游戏只是将玩家锁定在适当位置,而是将世界移动到玩家下方,给人以无尽移动的印象,而只有背景在移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM