Jump to content

A better way to "wake" physics objects?


benbeshara

Recommended Posts

Hi,


At the moment in order to 'wake' physics objects so that the player character can interact with them I've added

if(%col.getClassName() $= "PhysicsShape"){
     %col.applyImpulse("0, 0, 0", 2);
}

to the bottom of PlayerData::onCollision. I've tried exposing the setSleeping variable to script as an alternative means but had no luck with it.


It's entirely possible that I'm completely misunderstanding how physicsShapes are supposed to work, but as far as I can tell during the simulation they sleep when their level of motion is reduced to a small enough level and can only be woken again by interactions from another physics object or by having applyImpulse() called.


Is there a better way to wake physicsShapes? Or a way to set them not to sleep if, say, the player is in close proximity?


Thanks

Link to comment
Share on other sites

The way I handled this is adding the following function to px3Body (it should probably get more generalized and moved up to physicsBody, so as to more easily implement the same thing in bullet, but I'm physx3 focused and it was faster to put it here:

 

In px3Body.h:

  virtual void setDynamic( bool isDynamic );

In px3Body.cpp:

void Px3Body::setDynamic( bool isDynam )
{ 
bool isKinematic = mBodyFlags & BF_KINEMATIC;
if (isDynam==isKinematic)
{ //ie we're switching states, we were kinematic and now we want to be dynamic, or vice versa.
	physx::PxRigidDynamic *kBody = static_cast<physx::PxRigidDynamic*>(mActor);
	if (isDynam)
	{
		kBody->setRigidDynamicFlag(physx::PxRigidDynamicFlag::eKINEMATIC, false);
		mBodyFlags &= ~PhysicsBody::BF_KINEMATIC;
	} else {
		kBody->setRigidDynamicFlag(physx::PxRigidDynamicFlag::eKINEMATIC, true);
		mBodyFlags |= PhysicsBody::BF_KINEMATIC;
	}
}  
}

 

You can then call that like mPhysicsRep->setDynamic(true) from a PhysicsShape when you want a live object, and send it false to put it to sleep.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...