15 #include "physxSoftBody.h" 16 #include "physxSoftBodyDesc.h" 17 #include "physxSoftBodyNode.h" 18 #include "physxScene.h" 19 #include "physxGroupsMask.h" 21 #include "boundingBox.h" 31 link(NxSoftBody *softbodyPtr) {
36 _ptr->userData =
this;
41 scene->_softbodies.add(
this);
53 _ptr->userData = NULL;
54 _error_type = ET_released;
57 scene->_softbodies.remove(
this);
70 nassertv(_error_type == ET_ok);
73 _ptr->getScene().releaseSoftBody(*_ptr);
92 _ptr->getWorldBounds(bounds);
96 _node->set_bounds(&bb);
108 nassertr(_error_type == ET_ok, NULL);
109 return (
PhysxScene *)_ptr->getScene().userData;
118 get_soft_body_node()
const {
120 nassertr(_error_type == ET_ok, NULL);
130 create_soft_body_node(
const char *name) {
132 nassertr(_error_type == ET_ok, NULL);
135 _node->allocate(
this);
151 nassertv(_error_type == ET_ok);
153 _name = name ? name :
"";
154 _ptr->setName(_name.c_str());
165 nassertr(_error_type == ET_ok,
"");
166 return _ptr->getName();
178 nassertv(_error_type == ET_ok);
179 nassertv(group >= 0 && group < 32);
180 _ptr->setGroup(group);
192 nassertr(_error_type == ET_ok, 0);
193 return _ptr->getGroup();
204 nassertv(_error_type == ET_ok);
206 NxGroupsMask _mask = mask.get_mask();
207 _ptr->setGroupsMask(_mask);
221 nassertr(_error_type == ET_ok, mask);
223 NxGroupsMask _mask = _ptr->getGroupsMask();
224 mask.set_mask(_mask);
237 nassertr(_error_type == ET_ok, 0);
238 return _ptr->getNumberOfParticles();
250 nassertv(_error_type == ET_ok);
251 _ptr->setParticleRadius(radius);
262 nassertr(_error_type == ET_ok, 0.0f);
263 return _ptr->getParticleRadius();
274 nassertv(_error_type == ET_ok);
276 NxU32 flags = _ptr->getFlags();
285 _ptr->setFlags(flags);
296 nassertr(_error_type == ET_ok,
false);
298 return (_ptr->getFlags() & flag) ?
true :
false;
309 nassertr(_error_type == ET_ok, 0.0f);
310 return _ptr->getDensity();
326 nassertr(_error_type == ET_ok, 0.0f);
327 return _ptr->getRelativeGridSpacing();
339 nassertv(_error_type == ET_ok);
340 ptr()->setVolumeStiffness(stiffness);
351 nassertr(_error_type == ET_ok, 0.0f);
352 return ptr()->getVolumeStiffness();
364 nassertv(_error_type == ET_ok);
365 ptr()->setStretchingStiffness(stiffness);
376 nassertr(_error_type == ET_ok, 0.0f);
377 return ptr()->getStretchingStiffness();
389 nassertv(_error_type == ET_ok);
390 ptr()->setDampingCoefficient(coef);
401 nassertr(_error_type == ET_ok, 0.0f);
402 return ptr()->getDampingCoefficient();
414 nassertv(_error_type == ET_ok);
415 ptr()->setFriction(friction);
426 nassertr(_error_type == ET_ok, 0.0f);
427 return ptr()->getFriction();
439 nassertv(_error_type == ET_ok);
440 nassertv(factor > 1.0f);
441 ptr()->setTearFactor(factor);
452 nassertr(_error_type == ET_ok, 0.0f);
453 return ptr()->getTearFactor();
465 nassertv(_error_type == ET_ok);
466 nassertv(factor > 1.0f);
467 ptr()->setAttachmentTearFactor(factor);
478 nassertr(_error_type == ET_ok, 0.0f);
479 return ptr()->getAttachmentTearFactor();
490 nassertv(_error_type == ET_ok);
491 ptr()->setSolverIterations(iterations);
502 nassertr(_error_type == ET_ok, 0);
503 return ptr()->getSolverIterations();
522 nassertr(_error_type == ET_ok,
false);
523 return _ptr->isSleeping();
539 nassertv(_error_type == ET_ok);
540 _ptr->wakeUp(wakeCounterValue);
557 nassertv(_error_type == ET_ok);
575 nassertv(_error_type == ET_ok);
576 _ptr->setSleepLinearVelocity(threshold);
590 nassertr(_error_type == ET_ok, 0.0f);
591 return _ptr->getSleepLinearVelocity();
594 #if NX_SDK_VERSION_NUMBER > 281 602 set_self_collision_thickness(
float thickness) {
604 nassertv(_error_type == ET_ok);
605 _ptr->setSelfCollisionThickness(thickness);
613 float PhysxSoftBody::
614 get_self_collision_thickness()
const {
616 nassertr(_error_type == ET_ok, 0.0f);
617 return _ptr->getSelfCollisionThickness();
626 set_hard_stretch_limitation_factor(
float factor) {
628 nassertv(_error_type == ET_ok);
629 ptr()->setHardStretchLimitationFactor(factor);
638 float PhysxSoftBody::
639 get_hard_stretch_limitation_factor()
const {
641 nassertr(_error_type == ET_ok, 0.0f);
642 return ptr()->getHardStretchLimitationFactor();
644 #endif // NX_SDK_VERSION_NUMBER > 281 655 // Function: PhysxSoftBody::attach_vertex_to_global_pos
657 // Description: Attaches a cloth vertex to a position in world
661 attach_vertex_to_global_pos(unsigned int vertexId, LPoint3f const &pos) {
663 nassertv(_error_type == ET_ok);
664 nassertv(!pos.is_nan());
666 _ptr->attachVertexToGlobalPosition(vertexId, PhysxManager::point3_to_nxVec3(pos));
670 // Function: PhysxSoftBody::attach_to_shape
672 // Description: Attaches the cloth to a shape. All cloth points
673 // currently inside the shape are attached.
675 // This method only works with primitive and convex
676 // shapes. Since the inside of a general triangle mesh
677 // is not clearly defined.
680 attach_to_shape(PhysxShape *shape) {
682 nassertv(_error_type == ET_ok);
685 NxU32 attachmentFlags = 0; // --TODO--
686 _ptr->attachToShape(shape->ptr(), attachmentFlags);
690 // Function: PhysxSoftBody::attach_to_colliding_shapes
692 // Description: Attaches the cloth to all shapes, currently
695 // This method only works with primitive and convex
696 // shapes. Since the inside of a general triangle mesh
697 // is not clearly defined.
700 attach_to_colliding_shapes() {
702 nassertv(_error_type == ET_ok);
704 NxU32 attachmentFlags = 0; // --TODO--
705 _ptr->attachToCollidingShapes(attachmentFlags);
709 // Function: PhysxSoftBody::detach_from_shape
711 // Description: Detaches the cloth from a shape it has been
712 // attached to before.
714 // If the cloth has not been attached to the shape
715 // before, the call has no effect.
718 detach_from_shape(PhysxShape *shape) {
720 nassertv(_error_type == ET_ok);
723 _ptr->detachFromShape(shape->ptr());
727 // Function: PhysxSoftBody::free_vertex
729 // Description: Frees a previously attached cloth point.
732 free_vertex(unsigned int vertexId) {
734 nassertv(_error_type == ET_ok);
735 _ptr->freeVertex(vertexId);
739 // Function: PhysxSoftBody::attach_vertex_to_shape
741 // Description: Attaches a cloth vertex to a local position within
745 attach_vertex_to_shape(unsigned int vertexId, PhysxShape *shape, LPoint3f const &localPos) {
747 nassertv(_error_type == ET_ok);
748 nassertv(!localPos.is_nan());
751 NxU32 attachmentFlags = 0; // --TODO--
752 _ptr->attachVertexToShape(vertexId, shape->ptr(),
753 PhysxManager::point3_to_nxVec3(localPos),
758 // Function: PhysxSoftBody::get_vertex_attachment_status
760 // Description: Return the attachment status of the given vertex.
762 PhysxEnums::PhysxVertexAttachmentStatus PhysxSoftBody::
763 get_vertex_attachment_status(unsigned int vertexId) const {
765 nassertr(_error_type == ET_ok, VAS_none);
766 // --TODO-- nassertr(vertexId < _ptr->getNumberOfParticles(), VAS_none);
768 return (PhysxVertexAttachmentStatus) _ptr->getVertexAttachmentStatus(vertexId);
772 // Function: PhysxSoftBody::get_vertex_attachment_shape
774 // Description: Returns the pointer to an attached shape pointer
775 // of the given vertex. If the vertex is not attached
776 // or attached to a global position, NULL is returned.
778 PhysxShape *PhysxSoftBody::
779 get_vertex_attachment_shape(unsigned int vertexId) const {
781 nassertr(_error_type == ET_ok, NULL);
782 // --TODO-- nassertr(vertexId < _ptr->getNumberOfParticles(), NULL);
784 NxShape *shapePtr = _ptr->getVertexAttachmentShape(vertexId);
785 PhysxShape *shape = shapePtr ? (PhysxShape *)(shapePtr->userData) : NULL;
791 // Function: PhysxSoftBody::get_vertex_attachment_pos
793 // Description: Returns the attachment position of the given
794 // vertex. If the vertex is attached to shape, the
795 // position local to the shape's pose is returned. If
796 // the vertex is not attached, the return value is
799 LPoint3f PhysxSoftBody::
800 get_vertex_attachment_pos(unsigned int vertexId) const {
802 nassertr(_error_type == ET_ok, LPoint3f::zero());
803 // --TODO-- nassertr(vertexId < _ptr->getNumberOfParticles(), LPoint3f::zero());
805 return PhysxManager::nxVec3_to_point3(_ptr->getVertexAttachmentPosition(vertexId));
809 // Function: PhysxSoftBody::set_external_acceleration
811 // Description: Sets an external acceleration which affects all non
812 // attached particles of the cloth.
815 set_external_acceleration(LVector3f const &acceleration) {
817 nassertv(_error_type == ET_ok);
818 nassertv_always(!acceleration.is_nan());
820 _ptr->setExternalAcceleration(PhysxManager::vec3_to_nxVec3(acceleration));
824 // Function: PhysxSoftBody::set_wind_acceleration
826 // Description: Sets an acceleration acting normal to the cloth
827 // surface at each vertex.
830 set_wind_acceleration(LVector3f const &acceleration) {
832 nassertv(_error_type == ET_ok);
833 nassertv_always(!acceleration.is_nan());
835 _ptr->setWindAcceleration(PhysxManager::vec3_to_nxVec3(acceleration));
839 // Function: PhysxSoftBody::get_external_acceleration
841 // Description: Retrieves the external acceleration which affects
842 // all non attached particles of the cloth.
844 LVector3f PhysxSoftBody::
845 get_external_acceleration() const {
847 nassertr(_error_type == ET_ok, LVector3f::zero());
848 return PhysxManager::nxVec3_to_vec3(_ptr->getExternalAcceleration());
852 // Function: PhysxSoftBody::get_wind_acceleration
854 // Description: Retrieves the acceleration acting normal to the
855 // cloth surface at each vertex
857 LVector3f PhysxSoftBody::
858 get_wind_acceleration() const {
860 nassertr(_error_type == ET_ok, LVector3f::zero());
861 return PhysxManager::nxVec3_to_vec3(_ptr->getWindAcceleration());
865 // Function: PhysxSoftBody::add_force_at_vertex
867 // Description: Applies a force (or impulse) defined in the
868 // global coordinate frame, to a particular vertex
872 add_force_at_vertex(LVector3f const &force, int vertexId, PhysxForceMode mode) {
874 nassertv(_error_type == ET_ok);
875 _ptr->addForceAtVertex(PhysxManager::vec3_to_nxVec3(force),
881 // Function: PhysxSoftBody::add_force_at_pos
883 // Description: Applies a radial force (or impulse) at a
884 // particular position. All vertices within radius
885 // will be affected with a quadratic drop-off.
888 add_force_at_pos(LPoint3f const &pos, float magnitude, float radius, PhysxForceMode mode) {
890 nassertv(_error_type == ET_ok);
891 _ptr->addForceAtPos(PhysxManager::point3_to_nxVec3(pos),
898 // Function: PhysxSoftBody::add_directed_force_at_pos
900 // Description: Applies a directed force (or impulse) at a
901 // particular position. All vertices within radius
902 // will be affected with a quadratic drop-off.
905 add_directed_force_at_pos(LPoint3f const &pos, LVector3f const &force, float radius, PhysxForceMode mode) {
907 nassertv(_error_type == ET_ok);
908 _ptr->addDirectedForceAtPos(PhysxManager::point3_to_nxVec3(pos),
909 PhysxManager::vec3_to_nxVec3(force),
PhysxScene * get_scene() const
Returns the scene which this soft body belongs to.
void set_attachment_tear_factor(float factor)
Sets the soft body attachment tear factor (must be larger than one).
bool is_sleeping() const
Returns true if this soft body is sleeping.
An axis-aligned bounding box; that is, a minimum and maximum coordinate triple.
float get_damping_coefficient() const
Retrieves the damping coefficient.
float get_tear_factor() const
Retrieves the soft body tear factor.
unsigned int get_solver_iterations() const
Retrieves the soft body solver iterations.
float get_sleep_linear_velocity() const
Returns the linear velocity below which an soft body may go to sleep.
void wake_up(float wakeCounterValue=NX_SLEEP_INTERVAL)
Wakes up the soft body if it is sleeping.
float get_density() const
Gets the soft body density.
A scene is a collection of bodies, constraints, and effectors which can interact. ...
void set_volume_stiffness(float stiffness)
Sets the soft body volume stiffness in the range from 0 to 1.
void set_damping_coefficient(float coef)
Sets the damping coefficient in the range from 0 to 1.
void set_particle_radius(float radius)
Sets the soft body particle radius (must be positive).
unsigned int get_group() const
Retrieves the collision group this soft body is part of.
bool get_flag(PhysxSoftBodyFlag flag) const
Retrieves the value of a single flag.
void set_group(unsigned int group)
Sets which collision group this soft body is part of.
void set_friction(float friction)
Sets the soft body friction coefficient in the range from 0 to 1.
void set_stretching_stiffness(float stiffness)
Sets the soft body stretching stiffness in the range from 0 to 1.
PhysxGroupsMask get_groups_mask() const
Gets the 128-bit groups mask used for collision filtering.
void set_name(const char *name)
Sets a name string for the object that can be retrieved with get_name().
float get_relative_grid_spacing() const
Gets the relative grid spacing for the broad phase.
void set_flag(PhysxSoftBodyFlag flag, bool value)
Sets the value of a single flag.
Renderable geometry which represents a soft body mesh.
void set_tear_factor(float factor)
Sets the soft body tear factor (must be larger than one).
void set_groups_mask(const PhysxGroupsMask &mask)
Sets 128-bit mask used for collision filtering.
float get_particle_radius() const
Gets the soft body particle radius.
float get_friction() const
Retrieves the soft body friction coefficient.
void set_solver_iterations(unsigned int iterations)
Sets the soft body solver iterations.
const char * get_name() const
Retrieves the name string.
void set_sleep_linear_velocity(float threshold)
Sets the linear velocity below which an soft body may go to sleep.
static LPoint3f nxVec3_to_point3(const NxVec3 &p)
Converts from NxVec3 to LPoint3f.
TypeHandle is the identifier used to differentiate C++ class types.
float get_attachment_tear_factor() const
Retrieves the attachment soft body tear factor.
float get_volume_stiffness() const
Retrieves the soft body volume stiffness.
void put_to_sleep()
Forces the soft body to sleep.
float get_stretching_stiffness() const
Retrieves the soft body stretching stiffness.
unsigned int get_num_particles()
Gets the number of cloth particles.