30 btVector3 inertia(0, 0, 0);
33 btRigidBody::btRigidBodyConstructionInfo ci(mass, &_motion, _shape, inertia);
36 if (bullet_additional_damping) {
37 ci.m_additionalDamping =
true;
38 ci.m_additionalDampingFactor = bullet_additional_damping_linear_factor;
39 ci.m_additionalLinearDampingThresholdSqr = bullet_additional_damping_linear_threshold;
40 ci.m_additionalAngularDampingFactor = bullet_additional_damping_angular_factor;
41 ci.m_additionalAngularDampingThresholdSqr = bullet_additional_damping_angular_threshold;
45 _rigid =
new btRigidBody(ci);
46 _rigid->setUserPointer(
this);
59 _motion = copy._motion;
60 _rigid =
new btRigidBody(*copy._rigid);
61 _rigid->setUserPointer(
this);
62 _rigid->setCollisionShape(_shape);
63 _rigid->setMotionState(&_motion);
80 void BulletRigidBodyNode::
81 output(std::ostream &out)
const {
84 BulletBodyNode::do_output(out);
86 out <<
" mass=" << do_get_mass();
92 btCollisionObject *BulletRigidBodyNode::
103 void BulletRigidBodyNode::
106 do_set_mass(do_get_mass());
107 do_transform_changed();
117 void BulletRigidBodyNode::
118 do_set_mass(PN_stdfloat mass) {
120 btScalar bt_mass = mass;
121 btVector3 bt_inertia(0.0, 0.0, 0.0);
123 if (bt_mass > 0.0 && !_shapes.empty()) {
124 _rigid->getCollisionShape()->calculateLocalInertia(bt_mass, bt_inertia);
127 _rigid->setMassProps(bt_mass, bt_inertia);
128 _rigid->updateInertiaTensor();
149 PN_stdfloat BulletRigidBodyNode::
150 do_get_mass()
const {
152 btScalar inv_mass = _rigid->getInvMass();
153 btScalar mass = (inv_mass == btScalar(0.0)) ? btScalar(0.0) : btScalar(1.0) / inv_mass;
166 return do_get_mass();
177 return (PN_stdfloat)_rigid->getInvMass();
194 btVector3 inv_inertia(
195 inertia.get_x() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_x()),
196 inertia.get_y() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_y()),
197 inertia.get_z() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_z())
200 _rigid->setInvInertiaDiagLocal(inv_inertia);
201 _rigid->updateInertiaTensor();
213 btVector3 inv_inertia = _rigid->getInvInertiaDiagLocal();
215 inv_inertia.x() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.x(),
216 inv_inertia.y() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.y(),
217 inv_inertia.z() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.z()
226 LVector3 BulletRigidBodyNode::
227 get_inv_inertia_diag_local()
const {
230 return btVector3_to_LVector3(_rigid->getInvInertiaDiagLocal());
236 LMatrix3 BulletRigidBodyNode::
237 get_inv_inertia_tensor_world()
const {
240 return btMatrix3x3_to_LMatrix3(_rigid->getInvInertiaTensorWorld());
246 void BulletRigidBodyNode::
247 apply_force(
const LVector3 &force,
const LPoint3 &pos) {
250 nassertv_always(!force.is_nan());
251 nassertv_always(!pos.is_nan());
253 _rigid->applyForce(LVecBase3_to_btVector3(force),
254 LVecBase3_to_btVector3(pos));
260 void BulletRigidBodyNode::
261 apply_central_force(
const LVector3 &force) {
264 nassertv_always(!force.is_nan());
266 _rigid->applyCentralForce(LVecBase3_to_btVector3(force));
272 void BulletRigidBodyNode::
273 apply_torque(
const LVector3 &torque) {
276 nassertv_always(!torque.is_nan());
278 _rigid->applyTorque(LVecBase3_to_btVector3(torque));
284 void BulletRigidBodyNode::
285 apply_torque_impulse(
const LVector3 &torque) {
288 nassertv_always(!torque.is_nan());
290 _rigid->applyTorqueImpulse(LVecBase3_to_btVector3(torque));
296 void BulletRigidBodyNode::
297 apply_impulse(
const LVector3 &impulse,
const LPoint3 &pos) {
300 nassertv_always(!impulse.is_nan());
301 nassertv_always(!pos.is_nan());
303 _rigid->applyImpulse(LVecBase3_to_btVector3(impulse),
304 LVecBase3_to_btVector3(pos));
310 void BulletRigidBodyNode::
311 apply_central_impulse(
const LVector3 &impulse) {
314 nassertv_always(!impulse.is_nan());
316 _rigid->applyCentralImpulse(LVecBase3_to_btVector3(impulse));
322 void BulletRigidBodyNode::
323 do_transform_changed() {
325 if (_motion.sync_disabled())
return;
335 _motion.set_net_transform(ts);
338 if (!(get_object()->isKinematicObject())) {
339 btTransform trans = TransformState_to_btTrans(ts);
340 _rigid->setCenterOfMassTransform(trans);
345 if (ts->has_scale()) {
346 btVector3 new_scale = LVecBase3_to_btVector3(ts->get_scale());
347 btVector3 current_scale = _shape->getLocalScaling();
348 btVector3 current_scale_inv(1.0/current_scale.x(), 1.0/current_scale.y(), 1.0/current_scale.z());
350 if (new_scale != current_scale) {
351 _shape->setLocalScaling(current_scale_inv);
352 _shape->setLocalScaling(new_scale);
357 if (!_rigid->isActive()) {
358 _rigid->activate(
true);
365 void BulletRigidBodyNode::
368 if (_motion.sync_disabled())
return;
372 do_transform_changed();
379 void BulletRigidBodyNode::
380 transform_changed() {
382 if (_motion.sync_disabled())
return;
386 do_transform_changed();
395 if (get_object()->isKinematicObject()) {
396 do_transform_changed();
412 LVector3 BulletRigidBodyNode::
413 get_linear_velocity()
const {
416 return btVector3_to_LVector3(_rigid->getLinearVelocity());
422 LVector3 BulletRigidBodyNode::
423 get_angular_velocity()
const {
426 return btVector3_to_LVector3(_rigid->getAngularVelocity());
432 void BulletRigidBodyNode::
433 set_linear_velocity(
const LVector3 &velocity) {
436 nassertv_always(!velocity.is_nan());
438 _rigid->setLinearVelocity(LVecBase3_to_btVector3(velocity));
444 void BulletRigidBodyNode::
445 set_angular_velocity(
const LVector3 &velocity) {
448 nassertv_always(!velocity.is_nan());
450 _rigid->setAngularVelocity(LVecBase3_to_btVector3(velocity));
456 void BulletRigidBodyNode::
457 set_linear_damping(PN_stdfloat value) {
460 _rigid->setDamping(value, _rigid->getAngularDamping());
466 void BulletRigidBodyNode::
467 set_angular_damping(PN_stdfloat value) {
470 _rigid->setDamping(_rigid->getLinearDamping(), value);
476 PN_stdfloat BulletRigidBodyNode::
477 get_linear_damping()
const {
480 return (PN_stdfloat)_rigid->getLinearDamping();
486 PN_stdfloat BulletRigidBodyNode::
487 get_angular_damping()
const {
490 return (PN_stdfloat)_rigid->getAngularDamping();
496 void BulletRigidBodyNode::
500 _rigid->clearForces();
506 PN_stdfloat BulletRigidBodyNode::
507 get_linear_sleep_threshold()
const {
510 return _rigid->getLinearSleepingThreshold();
516 PN_stdfloat BulletRigidBodyNode::
517 get_angular_sleep_threshold()
const {
520 return _rigid->getAngularSleepingThreshold();
526 void BulletRigidBodyNode::
527 set_linear_sleep_threshold(PN_stdfloat threshold) {
530 _rigid->setSleepingThresholds(threshold, _rigid->getAngularSleepingThreshold());
536 void BulletRigidBodyNode::
537 set_angular_sleep_threshold(PN_stdfloat threshold) {
540 _rigid->setSleepingThresholds(_rigid->getLinearSleepingThreshold(), threshold);
546 void BulletRigidBodyNode::
547 set_gravity(
const LVector3 &gravity) {
550 nassertv_always(!gravity.is_nan());
552 _rigid->setGravity(LVecBase3_to_btVector3(gravity));
558 LVector3 BulletRigidBodyNode::
559 get_gravity()
const {
562 return btVector3_to_LVector3(_rigid->getGravity());
568 LVector3 BulletRigidBodyNode::
569 get_linear_factor()
const {
572 return btVector3_to_LVector3(_rigid->getLinearFactor());
578 LVector3 BulletRigidBodyNode::
579 get_angular_factor()
const {
582 return btVector3_to_LVector3(_rigid->getAngularFactor());
588 void BulletRigidBodyNode::
589 set_linear_factor(
const LVector3 &factor) {
592 _rigid->setLinearFactor(LVecBase3_to_btVector3(factor));
598 void BulletRigidBodyNode::
599 set_angular_factor(
const LVector3 &factor) {
602 _rigid->setAngularFactor(LVecBase3_to_btVector3(factor));
608 LVector3 BulletRigidBodyNode::
609 get_total_force()
const {
612 return btVector3_to_LVector3(_rigid->getTotalForce());
618 LVector3 BulletRigidBodyNode::
619 get_total_torque()
const {
622 return btVector3_to_LVector3(_rigid->getTotalTorque());
628 BulletRigidBodyNode::MotionState::
631 _trans.setIdentity();
640 void BulletRigidBodyNode::MotionState::
641 getWorldTransform(btTransform &trans)
const {
649 void BulletRigidBodyNode::MotionState::
650 setWorldTransform(
const btTransform &trans) {
660 void BulletRigidBodyNode::MotionState::
666 LPoint3 p = btVector3_to_LPoint3(_trans.getOrigin());
667 LQuaternion q = btQuat_to_LQuaternion(_trans.getRotation());
683 void BulletRigidBodyNode::MotionState::
688 _trans = TransformState_to_btTrans(ts);
694 bool BulletRigidBodyNode::MotionState::
695 sync_disabled()
const {
703 bool BulletRigidBodyNode::MotionState::
706 bool rc = _was_dirty;
718 return _motion.pick_dirty_flag();
742 get_gravity().write_datagram(dg);
743 get_linear_factor().write_datagram(dg);
744 get_angular_factor().write_datagram(dg);
746 get_linear_velocity().write_datagram(dg);
747 get_angular_velocity().write_datagram(dg);
762 param->fillin(scan, manager);
771 void BulletRigidBodyNode::
773 BulletBodyNode::fillin(scan, manager);
781 LVector3 gravity, linear_factor, angular_factor;
782 gravity.read_datagram(scan);
783 linear_factor.read_datagram(scan);
784 angular_factor.read_datagram(scan);
786 set_gravity(gravity);
787 set_linear_factor(linear_factor);
788 set_angular_factor(angular_factor);
void parse_params(const FactoryParams ¶ms, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
set_mass
Sets the mass of a rigid body.
static void register_with_read_factory()
Tells the BamReader how to create objects of type BulletRigidBodyNode.
get_mass
Returns the total mass of a rigid body.
void do_sync_b2p()
Assumes the lock(bullet global lock) is held by the caller.
bool pick_dirty_flag()
Returns TRUE if the transform of the rigid body has changed at least once since the last call to this...
get_inv_mass
Returns the inverse mass of a rigid body.
set_inertia
Sets the inertia of a rigid body.
get_inertia
Returns the inertia of the rigid body.
void do_sync_p2b()
Assumes the lock(bullet global lock) is held by the caller.
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
An instance of this class is passed to the Factory when requesting it to do its business and construc...
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Similar to MutexHolder, but for a light mutex.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
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.
void set_pos_quat(const LVecBase3 &pos, const LQuaternion &quat)
Sets the translation and rotation component of the transform, leaving scale untouched.
A basic node of the scene graph or data graph.
get_num_parents
Returns the number of parent nodes this node has.
TypeHandle is the identifier used to differentiate C++ class types.
Base class for objects that can be written to and read from Bam files.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.