Merge pull request #7962 from jagd/v3

introduce substeps for an accurate simulation of physics
This commit is contained in:
minggo 2014-09-09 10:27:59 +08:00
commit 8c595e9d24
3 changed files with 32 additions and 5 deletions

View File

@ -1,4 +1,5 @@
cocos2d-x-3.3?? ??
[NEW] PhysicsWorld: add setSubsteps() and getSubsteps()
[NEW] ActionManager: added removeAllActionsByTag()
[NEW] GLViewProtocol: added getAllTouches()
[NEW] Node: added stopAllActionsByTag()

View File

@ -879,6 +879,18 @@ void PhysicsWorld::setGravity(const Vect& gravity)
_info->setGravity(gravity);
}
void PhysicsWorld::setSubsteps(int steps)
{
if(steps > 0)
{
_substeps = steps;
if (steps > 1)
{
_updateRate = 1;
}
}
}
void PhysicsWorld::step(float delta)
{
if (_autoStep)
@ -914,10 +926,14 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
_updateTime += delta;
if (++_updateRateCount >= _updateRate)
{
_info->step(_updateTime * _speed);
for (auto& body : _bodies)
const float dt = _updateTime * _speed / _substeps;
for (int i = 0; i < _substeps; ++i)
{
body->update(_updateTime * _speed);
_info->step(dt);
for (auto& body : _bodies)
{
body->update(dt);
}
}
_updateRateCount = 0;
_updateTime = 0.0f;
@ -936,6 +952,7 @@ PhysicsWorld::PhysicsWorld()
, _updateRate(1)
, _updateRateCount(0)
, _updateTime(0.0f)
, _substeps(1)
, _info(nullptr)
, _scene(nullptr)
, _delayDirty(false)

View File

@ -141,7 +141,15 @@ public:
inline void setUpdateRate(int rate) { if(rate > 0) { _updateRate = rate; } }
/** get the update rate */
inline int getUpdateRate() { return _updateRate; }
/**
* set the number of substeps in an update of the physics world.
* One physics update will be divided into several substeps to increase its accuracy.
* default value is 1
*/
void setSubsteps(int steps);
/** get the number of substeps */
inline int getSubsteps() const { return _substeps; }
/** set the debug draw mask */
void setDebugDrawMask(int mask);
/** get the bebug draw mask */
@ -150,7 +158,7 @@ public:
/**
* To control the step of physics, if you want control it by yourself( fixed-timestep for example ), you can set this to false and call step by yourself.
* Defaut value is true.
* Note: if you set auto step to false, setSpeed and setUpdateRate won't work, you need to control the time step by yourself.
* Note: if you set auto step to false, setSpeed setSubsteps and setUpdateRate won't work, you need to control the time step by yourself.
*/
void setAutoStep(bool autoStep){ _autoStep = autoStep; }
/** Get the auto step */
@ -194,6 +202,7 @@ protected:
int _updateRate;
int _updateRateCount;
float _updateTime;
int _substeps;
PhysicsWorldInfo* _info;
Vector<PhysicsBody*> _bodies;