15 #include "bulletCharacterControllerNode.h"
17 TypeHandle BulletCharacterControllerNode::_type_handle;
24 BulletCharacterControllerNode::
28 _sync = TransformState::make_identity();
29 _sync_disable =
false;
32 btTransform trans = btTransform::getIdentity();
35 if (!shape->is_convex()) {
36 bullet_cat.error() <<
"a convex shape is required!" << endl;
40 btConvexShape *convex = (btConvexShape *)(shape->ptr());
43 _ghost =
new btPairCachingGhostObject();
44 _ghost->setUserPointer(
this);
46 _ghost->setWorldTransform(trans);
47 _ghost->setInterpolationWorldTransform(trans);
48 _ghost->setCollisionShape(convex);
49 _ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
52 _up = get_default_up_axis();
55 _linear_movement_is_local =
false;
56 _linear_movement.set(0.0f, 0.0f, 0.0f);
57 _angular_movement = 0.0f;
60 _character =
new btKinematicCharacterController(_ghost, convex, step_height, _up);
61 _character->setGravity((btScalar)9.81f);
75 void BulletCharacterControllerNode::
76 set_linear_movement(
const LVector3 &movement,
bool is_local) {
78 nassertv(!movement.
is_nan());
80 _linear_movement = movement;
81 _linear_movement_is_local = is_local;
89 void BulletCharacterControllerNode::
90 set_angular_movement(PN_stdfloat omega) {
92 _angular_movement = omega;
100 void BulletCharacterControllerNode::
101 sync_p2b(PN_stdfloat dt,
int num_substeps) {
107 btScalar angle = dt * deg_2_rad(_angular_movement);
109 btMatrix3x3 m = _ghost->getWorldTransform().getBasis();
110 btVector3 up = m[_up];
112 m *= btMatrix3x3(btQuaternion(up, angle));
114 _ghost->getWorldTransform().setBasis(m);
117 LVector3 vp = _linear_movement / (btScalar)num_substeps;
120 if (_linear_movement_is_local) {
121 btTransform
xform = _ghost->getWorldTransform();
122 xform.setOrigin(btVector3(0.0f, 0.0f, 0.0f));
123 v =
xform(LVecBase3_to_btVector3(vp));
126 v = LVecBase3_to_btVector3(vp);
130 _character->setWalkDirection(v * dt);
131 _angular_movement = 0.0f;
139 void BulletCharacterControllerNode::
145 btTransform trans = _ghost->getWorldTransform();
146 CPT(TransformState) ts = btTrans_to_TransformState(trans, scale);
151 if (!m_sync.almost_equal(m_ts)) {
153 _sync_disable =
true;
155 _sync_disable =
false;
164 void BulletCharacterControllerNode::
165 transform_changed() {
167 if (_sync_disable)
return;
170 CPT(TransformState) ts = np.get_net_transform();
175 if (!m_sync.almost_equal(m_ts)) {
180 PN_stdfloat heading = ts->get_hpr().get_x();
184 _character->warp(LVecBase3_to_btVector3(pos));
187 btMatrix3x3 m = _ghost->getWorldTransform().getBasis();
188 btVector3 up = m[_up];
190 m = btMatrix3x3(btQuaternion(up, deg_2_rad(heading)));
192 _ghost->getWorldTransform().setBasis(m);
195 _shape->set_local_scale(scale);
215 bool BulletCharacterControllerNode::
216 is_on_ground()
const {
218 return _character->onGround();
226 bool BulletCharacterControllerNode::
229 return _character->canJump();
237 void BulletCharacterControllerNode::
248 void BulletCharacterControllerNode::
249 set_fall_speed(PN_stdfloat fall_speed) {
251 _character->setFallSpeed((btScalar)fall_speed);
259 void BulletCharacterControllerNode::
260 set_jump_speed(PN_stdfloat jump_speed) {
262 _character->setJumpSpeed((btScalar)jump_speed);
270 void BulletCharacterControllerNode::
271 set_max_jump_height(PN_stdfloat max_jump_height) {
273 _character->setMaxJumpHeight((btScalar)max_jump_height);
281 void BulletCharacterControllerNode::
282 set_max_slope(PN_stdfloat max_slope) {
284 _character->setMaxSlope((btScalar)max_slope);
292 PN_stdfloat BulletCharacterControllerNode::
293 get_max_slope()
const {
295 return (PN_stdfloat)_character->getMaxSlope();
302 PN_stdfloat BulletCharacterControllerNode::
303 get_gravity()
const {
305 return (PN_stdfloat)_character->getGravity();
312 void BulletCharacterControllerNode::
313 set_gravity(PN_stdfloat gravity) {
315 _character->setGravity((btScalar)gravity);
323 void BulletCharacterControllerNode::
324 set_use_ghost_sweep_test(
bool value) {
326 return _character->setUseGhostSweepTest(value);
A basic node of the scene graph or data graph.
This is the base class for all three-component vectors and points.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
void set_transform(const TransformState *transform, Thread *current_thread=Thread::get_current_thread())
Changes the complete transform object on this node.
static NodePath any_path(PandaNode *node, Thread *current_thread=Thread::get_current_thread())
Returns a new NodePath that represents any arbitrary path from the root to the indicated node...
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so...
This is a 4-by-4 transform matrix.
LVecBase3 get_scale() const
Retrieves the scale component of the transform.
TypeHandle is the identifier used to differentiate C++ class types.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...