15 #include "bulletRigidBodyNode.h"
16 #include "bulletShape.h"
29 _motion =
new MotionState();
33 btVector3 inertia(0, 0, 0);
36 btRigidBody::btRigidBodyConstructionInfo ci(mass, _motion, _shape, inertia);
39 if (bullet_additional_damping) {
40 ci.m_additionalDamping =
true;
41 ci.m_additionalDampingFactor = bullet_additional_damping_linear_factor;
42 ci.m_additionalLinearDampingThresholdSqr = bullet_additional_damping_linear_threshold;
43 ci.m_additionalAngularDampingFactor = bullet_additional_damping_angular_factor;
44 ci.m_additionalAngularDampingThresholdSqr = bullet_additional_damping_angular_threshold;
48 _rigid =
new btRigidBody(ci);
49 _rigid->setUserPointer(
this);
57 void BulletRigidBodyNode::
58 output(ostream &out)
const {
60 BulletBodyNode::output(out);
70 btCollisionObject *BulletRigidBodyNode::
84 void BulletRigidBodyNode::
103 btScalar bt_mass = mass;
104 btVector3 bt_inertia(0.0, 0.0, 0.0);
107 _rigid->getCollisionShape()->calculateLocalInertia(bt_mass, bt_inertia);
110 _rigid->setMassProps(bt_mass, bt_inertia);
111 _rigid->updateInertiaTensor();
124 btScalar inv_mass = _rigid->getInvMass();
125 btScalar mass = (inv_mass == btScalar(0.0)) ? btScalar(0.0) : btScalar(1.0) / inv_mass;
138 return (PN_stdfloat)_rigid->getInvMass();
160 btVector3 inv_inertia(
161 inertia.get_x() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_x()),
162 inertia.get_y() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_y()),
163 inertia.get_z() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_z())
166 _rigid->setInvInertiaDiagLocal(inv_inertia);
167 _rigid->updateInertiaTensor();
181 btVector3 inv_inertia = _rigid->getInvInertiaDiagLocal();
183 inv_inertia.x() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.x(),
184 inv_inertia.y() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.y(),
185 inv_inertia.z() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.z()
197 get_inv_inertia_diag_local()
const {
199 return btVector3_to_LVector3(_rigid->getInvInertiaDiagLocal());
208 get_inv_inertia_tensor_world()
const {
210 return btMatrix3x3_to_LMatrix3(_rigid->getInvInertiaTensorWorld());
218 void BulletRigidBodyNode::
221 nassertv_always(!force.
is_nan());
222 nassertv_always(!pos.
is_nan());
224 _rigid->applyForce(LVecBase3_to_btVector3(force),
225 LVecBase3_to_btVector3(pos));
233 void BulletRigidBodyNode::
234 apply_central_force(
const LVector3 &force) {
236 nassertv_always(!force.
is_nan());
238 _rigid->applyCentralForce(LVecBase3_to_btVector3(force));
246 void BulletRigidBodyNode::
247 apply_torque(
const LVector3 &torque) {
249 nassertv_always(!torque.
is_nan());
251 _rigid->applyTorque(LVecBase3_to_btVector3(torque));
259 void BulletRigidBodyNode::
260 apply_torque_impulse(
const LVector3 &torque) {
262 nassertv_always(!torque.
is_nan());
264 _rigid->applyTorqueImpulse(LVecBase3_to_btVector3(torque));
272 void BulletRigidBodyNode::
275 nassertv_always(!impulse.
is_nan());
276 nassertv_always(!pos.
is_nan());
278 _rigid->applyImpulse(LVecBase3_to_btVector3(impulse),
279 LVecBase3_to_btVector3(pos));
287 void BulletRigidBodyNode::
288 apply_central_impulse(
const LVector3 &impulse) {
290 nassertv_always(!impulse.
is_nan());
292 _rigid->applyCentralImpulse(LVecBase3_to_btVector3(impulse));
300 void BulletRigidBodyNode::
301 transform_changed() {
303 if (_motion->sync_disabled())
return;
306 CPT(TransformState) ts = np.get_net_transform();
314 _motion->set_net_transform(ts);
318 if (!is_kinematic()) {
319 btTransform trans = TransformState_to_btTrans(ts);
320 _rigid->setCenterOfMassTransform(trans);
325 if (ts->has_scale()) {
326 btVector3 new_scale = LVecBase3_to_btVector3(ts->get_scale());
327 btVector3 current_scale = _shape->getLocalScaling();
328 btVector3 current_scale_inv(1.0/current_scale.x(), 1.0/current_scale.y(), 1.0/current_scale.z());
330 if (new_scale != current_scale) {
331 _shape->setLocalScaling(current_scale_inv);
332 _shape->setLocalScaling(new_scale);
337 if (!_rigid->isActive()) {
338 _rigid->activate(
true);
347 void BulletRigidBodyNode::
350 if (is_kinematic()) {
360 void BulletRigidBodyNode::
372 get_linear_velocity()
const {
374 return btVector3_to_LVector3(_rigid->getLinearVelocity());
383 get_angular_velocity()
const {
385 return btVector3_to_LVector3(_rigid->getAngularVelocity());
393 void BulletRigidBodyNode::
394 set_linear_velocity(
const LVector3 &velocity) {
396 nassertv_always(!velocity.
is_nan());
398 _rigid->setLinearVelocity(LVecBase3_to_btVector3(velocity));
406 void BulletRigidBodyNode::
407 set_angular_velocity(
const LVector3 &velocity) {
409 nassertv_always(!velocity.
is_nan());
411 _rigid->setAngularVelocity(LVecBase3_to_btVector3(velocity));
419 void BulletRigidBodyNode::
422 _rigid->clearForces();
430 PN_stdfloat BulletRigidBodyNode::
431 get_linear_sleep_threshold()
const {
433 return _rigid->getLinearSleepingThreshold();
441 PN_stdfloat BulletRigidBodyNode::
442 get_angular_sleep_threshold()
const {
444 return _rigid->getAngularSleepingThreshold();
452 void BulletRigidBodyNode::
453 set_linear_sleep_threshold(PN_stdfloat threshold) {
455 _rigid->setSleepingThresholds(threshold, _rigid->getAngularSleepingThreshold());
463 void BulletRigidBodyNode::
464 set_angular_sleep_threshold(PN_stdfloat threshold) {
466 _rigid->setSleepingThresholds(_rigid->getLinearSleepingThreshold(), threshold);
474 void BulletRigidBodyNode::
475 set_gravity(
const LVector3 &gravity) {
477 nassertv_always(!gravity.
is_nan());
479 _rigid->setGravity(LVecBase3_to_btVector3(gravity));
488 get_gravity()
const {
490 return btVector3_to_LVector3(_rigid->getGravity());
498 void BulletRigidBodyNode::
499 set_linear_factor(
const LVector3 &factor) {
501 _rigid->setLinearFactor(LVecBase3_to_btVector3(factor));
509 void BulletRigidBodyNode::
510 set_angular_factor(
const LVector3 &factor) {
512 _rigid->setAngularFactor(LVecBase3_to_btVector3(factor));
521 get_total_force()
const {
523 return btVector3_to_LVector3(_rigid->getTotalForce());
532 get_total_torque()
const {
534 return btVector3_to_LVector3(_rigid->getTotalTorque());
542 BulletRigidBodyNode::MotionState::
545 _trans.setIdentity();
556 void BulletRigidBodyNode::MotionState::
557 getWorldTransform(btTransform &trans)
const {
567 void BulletRigidBodyNode::MotionState::
568 setWorldTransform(
const btTransform &trans) {
580 void BulletRigidBodyNode::MotionState::
586 LPoint3 p = btVector3_to_LPoint3(_trans.getOrigin());
587 LQuaternion q = btQuat_to_LQuaternion(_trans.getRotation());
608 void BulletRigidBodyNode::MotionState::
609 set_net_transform(
const TransformState *ts) {
613 _trans = TransformState_to_btTrans(ts);
621 bool BulletRigidBodyNode::MotionState::
622 sync_disabled()
const {
632 bool BulletRigidBodyNode::MotionState::
635 bool rc = _was_dirty;
650 return _motion->pick_dirty_flag();
void set_inertia(const LVecBase3 &inertia)
Sets the inertia of a rigid body.
A basic node of the scene graph or data graph.
This is the base class for all three-component vectors and points.
void set_mass(PN_stdfloat mass)
Sets the mass of a rigid body.
PN_stdfloat get_mass() const
Returns the total mass of a rigid body.
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
void set_pos_quat(const LVecBase3 &pos, const LQuaternion &quat)
Sets the translation and rotation component of the transform, leaving scale untouched.
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.
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...
PN_stdfloat get_inv_mass() const
Returns the inverse mass of a rigid body.
This is the base quaternion class.
TypeHandle is the identifier used to differentiate C++ class types.
This is a 3-by-3 transform matrix.
LVector3 get_inertia() const
Returns the inertia of the rigid body.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
bool pick_dirty_flag()
Returns TRUE if the transform of the rigid body has changed at least once since the last call to this...