Bullet: fix walk velocity of character controller.

This commit is contained in:
enn0x 2011-10-29 21:45:35 +00:00
parent 04096fd5e7
commit 868047e65d
4 changed files with 20 additions and 21 deletions

View File

@ -202,7 +202,7 @@ set_angular_velocity(PN_stdfloat omega) {
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BulletCharacterControllerNode:: void BulletCharacterControllerNode::
sync_p2b(PN_stdfloat dt) { sync_p2b(PN_stdfloat dt, int num_substeps) {
// Synchronise global transform // Synchronise global transform
transform_changed(); transform_changed();
@ -218,19 +218,20 @@ sync_p2b(PN_stdfloat dt) {
_ghost->getWorldTransform().setBasis(m); _ghost->getWorldTransform().setBasis(m);
// Linear movement // Linear movement
LVector3 vp = _linear_velocity / (btScalar)num_substeps;
btVector3 v; btVector3 v;
if (_linear_velocity_is_local) { if (_linear_velocity_is_local) {
btTransform xform = _ghost->getWorldTransform(); btTransform xform = _ghost->getWorldTransform();
xform.setOrigin(btVector3(0.0f, 0.0f, 0.0f)); xform.setOrigin(btVector3(0.0f, 0.0f, 0.0f));
v = xform(LVecBase3_to_btVector3(_linear_velocity)); v = xform(LVecBase3_to_btVector3(vp));
} }
else { else {
v = LVecBase3_to_btVector3(_linear_velocity); v = LVecBase3_to_btVector3(vp);
} }
//_character->setVelocityForTimeInterval(v, dt); //_character->setVelocityForTimeInterval(v, dt);
_character->setWalkDirection(v * dt); _character->setWalkDirection(v * dt);
//_character->setWalkDirection(v);
_angular_velocity = 0.0f; _angular_velocity = 0.0f;
} }

View File

@ -69,7 +69,7 @@ public:
INLINE btPairCachingGhostObject *get_ghost() const; INLINE btPairCachingGhostObject *get_ghost() const;
INLINE btKinematicCharacterController *get_character() const; INLINE btKinematicCharacterController *get_character() const;
void sync_p2b(PN_stdfloat dt); void sync_p2b(PN_stdfloat dt, int num_substeps);
void sync_b2p(); void sync_b2p();
protected: protected:

View File

@ -19,6 +19,8 @@
#include "collideMask.h" #include "collideMask.h"
#define clamp(x, x_min, x_max) max(min(x, x_max), x_min)
TypeHandle BulletWorld::_type_handle; TypeHandle BulletWorld::_type_handle;
PStatCollector BulletWorld::_pstat_physics("App:Bullet:DoPhysics"); PStatCollector BulletWorld::_pstat_physics("App:Bullet:DoPhysics");
@ -136,29 +138,23 @@ get_gravity() const {
// Access: Published // Access: Published
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BulletWorld:: int BulletWorld::
do_physics(PN_stdfloat dt, int substeps, PN_stdfloat stepsize) { do_physics(PN_stdfloat dt, int max_substeps, PN_stdfloat stepsize) {
_pstat_physics.start(); _pstat_physics.start();
int num_substeps = clamp(int(dt / stepsize), 1, max_substeps);
// Synchronize Panda to Bullet // Synchronize Panda to Bullet
_pstat_p2b.start(); _pstat_p2b.start();
sync_p2b(dt); sync_p2b(dt, num_substeps);
_pstat_p2b.stop(); _pstat_p2b.stop();
// Simulation // Simulation
_pstat_simulation.start(); _pstat_simulation.start();
int n = _world->stepSimulation((btScalar)dt, substeps, (btScalar)stepsize); int n = _world->stepSimulation((btScalar)dt, max_substeps, (btScalar)stepsize);
_pstat_simulation.stop(); _pstat_simulation.stop();
if (!n) {
bullet_cat.debug() << "interpolated transforms!" << endl;
}
if (dt > substeps * stepsize) {
bullet_cat.debug() << "lost simulation time!" << endl;
}
// Synchronize Bullet to Panda // Synchronize Bullet to Panda
_pstat_b2p.start(); _pstat_b2p.start();
sync_b2p(); sync_b2p();
@ -173,6 +169,8 @@ do_physics(PN_stdfloat dt, int substeps, PN_stdfloat stepsize) {
} }
_pstat_physics.stop(); _pstat_physics.stop();
return n;
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -181,7 +179,7 @@ do_physics(PN_stdfloat dt, int substeps, PN_stdfloat stepsize) {
// Description: // Description:
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void BulletWorld:: void BulletWorld::
sync_p2b(PN_stdfloat dt) { sync_p2b(PN_stdfloat dt, int num_substeps) {
for (int i=0; i < get_num_rigid_bodies(); i++) { for (int i=0; i < get_num_rigid_bodies(); i++) {
get_rigid_body(i)->sync_p2b(); get_rigid_body(i)->sync_p2b();
@ -196,7 +194,7 @@ sync_p2b(PN_stdfloat dt) {
} }
for (int i=0; i < get_num_characters(); i++) { for (int i=0; i < get_num_characters(); i++) {
get_character(i)->sync_p2b(dt); get_character(i)->sync_p2b(dt, num_substeps);
} }
} }

View File

@ -56,7 +56,7 @@ PUBLISHED:
void set_gravity(PN_stdfloat gx, PN_stdfloat gy, PN_stdfloat gz); void set_gravity(PN_stdfloat gx, PN_stdfloat gy, PN_stdfloat gz);
const LVector3 get_gravity() const; const LVector3 get_gravity() const;
void do_physics(PN_stdfloat dt, int substeps=1, PN_stdfloat stepsize=1.0f/60.0f); int do_physics(PN_stdfloat dt, int max_substeps=1, PN_stdfloat stepsize=1.0f/60.0f);
void set_debug_node(BulletDebugNode *node); void set_debug_node(BulletDebugNode *node);
void clear_debug_node(); void clear_debug_node();
@ -151,7 +151,7 @@ public:
INLINE btDispatcher *get_dispatcher() const; INLINE btDispatcher *get_dispatcher() const;
private: private:
void sync_p2b(PN_stdfloat dt); void sync_p2b(PN_stdfloat dt, int num_substeps);
void sync_b2p(); void sync_b2p();
typedef PTA(PT(BulletRigidBodyNode)) BulletRigidBodies; typedef PTA(PT(BulletRigidBodyNode)) BulletRigidBodies;