mirror of https://github.com/axmolengine/axmol.git
Merge pull request #7962 from jagd/v3
introduce substeps for an accurate simulation of physics
This commit is contained in:
commit
8c595e9d24
|
@ -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()
|
||||
|
|
|
@ -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);
|
||||
const float dt = _updateTime * _speed / _substeps;
|
||||
for (int i = 0; i < _substeps; ++i)
|
||||
{
|
||||
_info->step(dt);
|
||||
for (auto& body : _bodies)
|
||||
{
|
||||
body->update(_updateTime * _speed);
|
||||
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)
|
||||
|
|
|
@ -141,6 +141,14 @@ 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);
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue