cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.02k stars 7.05k forks source link

I found a bug with the physics engine #11238

Open jeason1997 opened 9 years ago

jeason1997 commented 9 years ago

3.x 版本封装的物理引擎,刚体有时候会突然猛烈运动,最后弹出包围盒。

这不是我一个人才有的BUG,我搜索了下论坛里的其他帖子,发现不少人有这个问题。

就算是静止的刚体,有时候也会突然剧烈弹起来。

而且这个BUG的发生有一定偶然性。

jimrange commented 9 years ago

I think this bug is probably a result of parameters that set by the user of the API when tuning the physics engine.

If the physics step is too small or a physics computation takes too much time to complete, then non-converging integration calculations can result in the physics engine due to large time deltas. The result of exploding non-converging values is what you describe as sudden and extreme movement of the physics bodies.

I have been successful at fixing this issue by increasing the substeps, e.g. scene->getPhysicsWorld()->setSubsteps(10); The number 10 here is arbitrary and largely dependent on the physics model you are simulating and how much other CPU is being used by other game code.

Bottom line, getting a physics engine such as Chipmunk to be stable can require some testing and fine tuning. Test on a least-common-denominator device and play around with the substep setting, how many physics objects your game uses, and other CPU hungry game code to find something that works.

A Possible Fix For the Cocos2d-x Code --- A hackish fix to prevent this is to check the time delta that the physics engine is getting stepped at. If it is bigger than, maybe a tenth of a second, then force it to be a tenth of a second. This can lead to undesirable effects in your game (time loss in the physics simulation), but it guarantees physics engine stability in that it eliminates large time delta's from ever occurring.

minggo commented 9 years ago

@WenhaiLin I think it was fixed in v4, we should synchronize it to v3.x.

jimrange commented 9 years ago

I just started noticing a small jittering or shaking of sprites controlled by PhysicsBodies. Is this bug the jittery shaking that occurs with the position of physics bodies? Or is it when the game freezes due to a physics simulation integration diverging?

jeason1997 commented 9 years ago

@jimrange Hi,I use this physics engine by default setting, I sure I don't change any parameter. I just add some spirtes with physicsBodys, and then occur this bug......

你好,我使用这个封装的物理引擎的时候没有进行任何设置,所有参数都是默认的。所以排除了是我把physics step设置得太小或者太大。 我仅仅是添加了几个精灵,并赋予它们物理特征。然后就出现这个BUG。而且不少人会这样。 要是手机平台的话,有时候你把游戏切换到后台再切换回来,刚体有时候也会产生莫名的速度。