20 #include "phyobject.h"
21 #include "private/phyobjectprivate.h"
22 #include "private/worldprivate.h"
27 :
WObject( w, name, tm, false ), materialv(
"default") {
29 priv =
new PhyObjectPrivate();
31 if ( createPrivate ) {
32 w->pushObject(
this );
33 createPrivateObject();
40 isKinematicCollidable =
false;
42 objInertiaVec =
wVector(0.0, 1.0, 1.0, 1.0);
43 objInvInertiaVec =
wVector(0.0, 1.0, 1.0, 1.0);
47 #ifdef WORLDSIM_USE_NEWTON
48 if (
priv->body != NULL ) {
49 NewtonDestroyBody( worldpriv->world,
priv->body );
50 NewtonReleaseCollision( worldpriv->world,
priv->collision );
60 #ifdef WORLDSIM_USE_NEWTON
63 NewtonBodySetTransformCallback(
priv->body, 0 );
64 NewtonBodySetForceAndTorqueCallback(
priv->body, 0 );
78 isKinematicCollidable = c;
79 if (isKinematicCollidable) {
82 NewtonBodySetMassMatrix(
priv->body, 0, 1, 1, 1 );
86 NewtonCollision* nullC = NewtonCreateNull( worldpriv->world );
87 NewtonBodySetCollision(
priv->body, nullC );
88 NewtonReleaseCollision( worldpriv->world, nullC );
92 NewtonBodySetTransformCallback(
priv->body, (PhyObjectPrivate::setTransformHandler) );
93 NewtonBodySetForceAndTorqueCallback(
priv->body, (PhyObjectPrivate::applyForceAndTorqueHandler) );
94 NewtonBodySetFreezeState(
priv->body, 0 );
95 NewtonBodySetCollision(
priv->body,
priv->collision );
96 NewtonBodySetMatrix(
priv->body, &
tm[0][0] );
99 NewtonBodySetMassMatrix(
priv->body, 0.0, 1.0, 1.0, 1.0 );
101 NewtonBodySetMassMatrix(
priv->body, objInertiaVec[0], objInertiaVec[1], objInertiaVec[2], objInertiaVec[3] );
109 if (isStatic == b)
return;
111 #ifdef WORLDSIM_USE_NEWTON
113 NewtonBodySetMassMatrix(
priv->body, 0.0, 1.0, 1.0, 1.0 );
115 NewtonBodySetMassMatrix(
priv->body, objInertiaVec[0], objInertiaVec[1], objInertiaVec[2], objInertiaVec[3] );
121 #ifdef WORLDSIM_USE_NEWTON
125 NewtonBodySetForce(
priv->body, &forceAcc[0] );
126 NewtonBodySetTorque(
priv->body, &torqueAcc[0] );
127 NewtonBodySetOmega(
priv->body, &zero[0] );
128 NewtonBodySetVelocity(
priv->body, &zero[0] );
130 NewtonWorld* nworld = NewtonBodyGetWorld(
priv->body );
131 NewtonWorldCriticalSectionLock( nworld );
132 for( NewtonJoint* contactList = NewtonBodyGetFirstContactJoint(
priv->body ); contactList;
133 contactList = NewtonBodyGetNextContactJoint(
priv->body, contactList ) ) {
135 for(
void* contact = NewtonContactJointGetFirstContact(contactList); contact; contact = nextContact ) {
137 nextContact = NewtonContactJointGetNextContact( contactList, contact );
138 NewtonContactJointRemoveContact( contactList, contact );
141 NewtonWorldCriticalSectionUnlock( nworld );
146 #ifdef WORLDSIM_USE_NEWTON
148 NewtonBodySetMatrix(
priv->body, &
tm[0][0] );
152 void PhyObject::addForce(
const wVector& force ) {
156 void PhyObject::setForce(
const wVector& force) {
160 wVector PhyObject::force() {
162 #ifdef WORLDSIM_USE_NEWTON
163 NewtonBodyGetForce(
priv->body, &f[0] );
168 void PhyObject::addTorque(
const wVector& torque ) {
172 void PhyObject::setTorque(
const wVector& torque ) {
176 wVector PhyObject::torque() {
178 #ifdef WORLDSIM_USE_NEWTON
179 NewtonBodyGetTorque(
priv->body, &t[0] );
186 #ifdef WORLDSIM_USE_NEWTON
188 NewtonBodySetMassMatrix(
priv->body, mi[0], mi[1], mi[2], mi[3] );
189 NewtonBodyGetInvMass(
priv->body, &objInvInertiaVec[0], &objInvInertiaVec[1], &objInvInertiaVec[2], &objInvInertiaVec[3]);
194 return objInertiaVec;
197 wVector PhyObject::inertiaVec()
const {
199 mi[0] = objInertiaVec[1];
200 mi[1] = objInertiaVec[2];
201 mi[2] = objInertiaVec[3];
206 wVector PhyObject::invMassInertiaVec()
const {
207 return objInvInertiaVec;
212 mi[0] = objInvInertiaVec[1];
213 mi[1] = objInvInertiaVec[2];
214 mi[2] = objInvInertiaVec[3];
220 #ifdef WORLDSIM_USE_NEWTON
221 if ( newmass < 0.0001 ) {
222 objInertiaVec =
wVector(0.0, 1.0, 1.0, 1.0);
223 objInvInertiaVec =
wVector(0.0, 1.0, 1.0, 1.0);
225 NewtonBodySetMassMatrix(
priv->body, 0, 1, 1, 1 );
228 real centre[3] = { 0, 0, 0 };
229 NewtonConvexCollisionCalculateInertialMatrix(
priv->collision, inertia, centre );
231 objInertiaVec =
wVector(newmass, newmass*inertia[0], newmass*inertia[1], newmass*inertia[2]);
232 NewtonBodySetMassMatrix(
priv->body, objInertiaVec[0], objInertiaVec[1], objInertiaVec[2], objInertiaVec[3] );
234 NewtonBodyGetInvMass(
priv->body, &objInvInertiaVec[0], &objInvInertiaVec[1], &objInvInertiaVec[2], &objInvInertiaVec[3]);
237 NewtonBodySetMassMatrix(
priv->body, 0, 1, 1, 1 );
244 return objInertiaVec[0];
247 void PhyObject::setOmega(
const wVector& omega ) {
248 #ifdef WORLDSIM_USE_NEWTON
249 NewtonBodySetOmega(
priv->body, &omega[0] );
253 wVector PhyObject::omega() {
255 #ifdef WORLDSIM_USE_NEWTON
256 NewtonBodyGetOmega(
priv->body, &t[0] );
261 void PhyObject::setVelocity(
const wVector& velocity ) {
262 #ifdef WORLDSIM_USE_NEWTON
263 NewtonBodySetVelocity(
priv->body, &velocity[0] );
267 wVector PhyObject::velocity() {
269 #ifdef WORLDSIM_USE_NEWTON
270 NewtonBodyGetVelocity(
priv->body, &t[0] );
275 void PhyObject::addImpulse(
const wVector& pointDeltaVeloc,
const wVector& pointPosit ) {
276 #ifdef WORLDSIM_USE_NEWTON
277 NewtonBodyAddImpulse(
priv->body, &pointDeltaVeloc[0], &pointPosit[0] );
281 void PhyObject::setMaterial( QString material ) {
282 this->materialv = material;
283 #ifdef WORLDSIM_USE_NEWTON
284 if (
worldv->priv->matIDs.contains( material ) ) {
285 NewtonBodySetMaterialGroupID(
priv->body,
worldv->priv->matIDs[material] );
290 QString PhyObject::material()
const {
294 void PhyObject::createPrivateObject() {
295 #ifdef WORLDSIM_USE_NEWTON
296 NewtonCollision* c = NewtonCreateNull( worldpriv->world );
298 priv->body = NewtonCreateBody( worldpriv->world, c, &initialTransformationMatrix[0][0] );
299 priv->collision = NewtonBodyGetCollision(
priv->body );
301 NewtonBodySetAutoSleep(
priv->body, 0 );
302 NewtonBodySetMassMatrix(
priv->body, 1.0, 1.0, 1.0, 1.0 );
303 NewtonBodySetUserData(
priv->body,
this );
305 NewtonBodySetTransformCallback(
priv->body, (PhyObjectPrivate::setTransformHandler) );
306 NewtonBodySetForceAndTorqueCallback(
priv->body, (PhyObjectPrivate::applyForceAndTorqueHandler) );
static wMatrix identity()
create an identity matrix
void setKinematic(bool b, bool c=false)
Changes between kinematic/dynamic behaviour for the object.
wMatrix tm
Trasformation matrix.
void setMass(real)
Set the mass without touching the Inertia data.
virtual ~PhyObject()
Destroy this object.
void reset()
reset the object: set the velocity to zeroset the angural velocity to zeroset any residual forces and...
real mass()
Return the mass.
PhyObject(World *world, QString name="unamed", const wMatrix &tm=wMatrix::identity(), bool cp=true)
Create a physics object with no-collision-shape and insert it in the world passed.
PhyObjectPrivate * priv
Engine encapsulation.
void setMassInertiaVec(const wVector &)
Set the Mass and momentum of Inertia: ( mass, Ixx, Iyy, Izz )
void setStatic(bool b)
Makes the object static or not.
wVector invInertiaVec() const
Return the inverse of Inertia.
virtual void changedMatrix()
syncronize this object with underlying physic object
wVector massInertiaVec() const
Return the Mass and momentum of Inertia.