Skip to content

Commit

Permalink
server: initial implementation of trigger actors (used for func_water)
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed Dec 26, 2023
1 parent b622ca6 commit f6080e8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions server/cbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ extern void DispatchUpdatePlayerBaseVelocity( edict_t *pEdict );
#define ACTOR_CHARACTER 3 // player or monster physics shadow
#define ACTOR_STATIC 4 // static actor (env_static)
#define ACTOR_VEHICLE 5 // complex body (vehicle)
#define ACTOR_TRIGGER 6 // used for func_water

#define PARENT_FROZEN_POS_X BIT( 1 ) // compatible with PhysX flags NX_BF_FROZEN_
#define PARENT_FROZEN_POS_Y BIT( 2 )
Expand Down
7 changes: 6 additions & 1 deletion server/doors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ void CBaseDoor::Spawn( void )
UTIL_SetOrigin( this, m_vecPosition1 );
}

m_pUserData = WorldPhysic->CreateKinematicBodyFromEntity(this);
if (FStrEq(STRING(pev->classname), "func_water")) {
m_pUserData = WorldPhysic->CreateTriggerFromEntity(this);
}
else {
m_pUserData = WorldPhysic->CreateKinematicBodyFromEntity(this);
}

m_iState = STATE_OFF;

Expand Down
1 change: 1 addition & 0 deletions server/physic.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class IPhysicLayer
virtual void *CreateKinematicBodyFromEntity( CBaseEntity *pEntity ) = 0;
virtual void *CreateStaticBodyFromEntity( CBaseEntity *pObject ) = 0;
virtual void *CreateVehicle( CBaseEntity *pObject, int scriptName = 0 ) = 0;
virtual void *CreateTriggerFromEntity( CBaseEntity *pEntity ) = 0;
virtual void *RestoreBody( CBaseEntity *pEntity ) = 0;
virtual void SaveBody( CBaseEntity *pObject ) = 0;
virtual void SetOrigin( CBaseEntity *pEntity, const Vector &origin ) = 0;
Expand Down
11 changes: 11 additions & 0 deletions server/physx/event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ using namespace physx;
* be created or destroyed. If state modification is needed then the changes should be stored to a buffer
* and performed after the simulation step.
*/

void EventHandler::onTrigger(PxTriggerPair* pairs, PxU32 count)
{
for (PxU32 i = 0; i < count; i++)
{
if (pairs[i].flags & (PxTriggerPairFlag::eREMOVED_SHAPE_TRIGGER | PxTriggerPairFlag::eREMOVED_SHAPE_OTHER)) {
continue; // ignore pairs when shapes have been deleted
}
}
}

void EventHandler::onContact(const PxContactPairHeader &pairHeader, const PxContactPair *pairs, PxU32 nbPairs)
{
for (PxU32 i = 0; i < nbPairs; i++)
Expand Down
2 changes: 1 addition & 1 deletion server/physx/event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EventHandler : public physx::PxSimulationEventCallback
virtual void onConstraintBreak(physx::PxConstraintInfo* constraints, physx::PxU32 count) {};
virtual void onWake(physx::PxActor** actors, physx::PxU32 count) {};
virtual void onSleep(physx::PxActor** actors, physx::PxU32 count) {};
virtual void onTrigger(physx::PxTriggerPair* pairs, physx::PxU32 count) {};
virtual void onTrigger(physx::PxTriggerPair* pairs, physx::PxU32 count);
virtual void onAdvance(const physx::PxRigidBody* const* bodyBuffer, const physx::PxTransform* poseBuffer, const physx::PxU32 count) {};
virtual void onContact(const physx::PxContactPairHeader& pairHeader, const physx::PxContactPair* pairs, physx::PxU32 nbPairs);
TouchEventsQueue &getTouchEventsQueue();
Expand Down
36 changes: 34 additions & 2 deletions server/physx/physx_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ void CPhysicPhysX :: InitPhysic( void )
PxFilterObjectAttributes attributes0, PxFilterData filterData0,
PxFilterObjectAttributes attributes1, PxFilterData filterData1,
PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize) -> PxFilterFlags {
if (PxFilterObjectIsTrigger(attributes0) || PxFilterObjectIsTrigger(attributes1))
{
pairFlags = PxPairFlag::eTRIGGER_DEFAULT;
return PxFilterFlag::eDEFAULT;
}

pairFlags = PxPairFlag::eCONTACT_DEFAULT
| PxPairFlag::eDETECT_CCD_CONTACT
| PxPairFlag::eNOTIFY_TOUCH_CCD
Expand All @@ -165,7 +171,7 @@ void CPhysicPhysX :: InitPhysic( void )
}

return PxFilterFlag::eDEFAULT;
};
};

m_worldBounds.minimum = PxVec3(-32768, -32768, -32768);
m_worldBounds.maximum = PxVec3(32768, 32768, 32768);
Expand Down Expand Up @@ -1231,6 +1237,32 @@ void *CPhysicPhysX :: CreateStaticBodyFromEntity( CBaseEntity *pObject )
return pActor;
}

void *CPhysicPhysX::CreateTriggerFromEntity(CBaseEntity *pEntity)
{
PxBoxGeometry boxGeometry;
boxGeometry.halfExtents = pEntity->pev->size / 2.0f;
Vector centerOrigin = pEntity->pev->mins + pEntity->pev->size / 2.0f;
PxRigidStatic *pActor = m_pPhysics->createRigidStatic(PxTransform(centerOrigin));
PxShape *pShape = PxRigidActorExt::createExclusiveShape(*pActor, boxGeometry, *m_pDefaultMaterial);

if (!pActor)
{
ALERT( at_error, "failed to create trigger actor from entity %s\n", pEntity->GetClassname());
return NULL;
}

pShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
pShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
pActor->setName(pEntity->GetClassname());
pActor->userData = pEntity->edict();
m_pScene->addActor(*pActor);

pEntity->m_iActorType = ACTOR_TRIGGER;
pEntity->m_pUserData = pActor;

return pActor;
}

void *CPhysicPhysX :: CreateVehicle( CBaseEntity *pObject, string_t scriptName )
{
#if defined (HAS_PHYSIC_VEHICLE)
Expand Down Expand Up @@ -1538,7 +1570,7 @@ void CPhysicPhysX :: UpdateEntityAABB( CBaseEntity *pEntity )
ClearBounds( pEntity->pev->absmin, pEntity->pev->absmax );
AddPointToBounds( actorBounds.minimum, pEntity->pev->absmin, pEntity->pev->absmax );
AddPointToBounds( actorBounds.maximum, pEntity->pev->absmin, pEntity->pev->absmax );

pEntity->pev->mins = pEntity->pev->absmin - pEntity->pev->origin;
pEntity->pev->maxs = pEntity->pev->absmax - pEntity->pev->origin;
pEntity->pev->size = pEntity->pev->maxs - pEntity->pev->mins;
Expand Down
1 change: 1 addition & 0 deletions server/physx/physx_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CPhysicPhysX : public IPhysicLayer
void *CreateKinematicBodyFromEntity( CBaseEntity *pEntity );
void *CreateStaticBodyFromEntity( CBaseEntity *pObject );
void *CreateVehicle( CBaseEntity *pObject, string_t scriptName = 0 );
void *CreateTriggerFromEntity( CBaseEntity *pEntity );
void *RestoreBody( CBaseEntity *pEntity );
void SaveBody( CBaseEntity *pObject );
bool Initialized( void ) { return (m_pPhysics != NULL); }
Expand Down
1 change: 1 addition & 0 deletions server/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CPhysicNull : public IPhysicLayer
virtual void *CreateBoxFromEntity( CBaseEntity *pObject ) { return NULL; }
virtual void *CreateKinematicBodyFromEntity( CBaseEntity *pEntity ) { return NULL; }
virtual void *CreateStaticBodyFromEntity( CBaseEntity *pObject ) { return NULL; }
virtual void *CreateTriggerFromEntity( CBaseEntity *pEntity ) { return NULL; }
virtual void *CreateVehicle( CBaseEntity *pObject, string_t scriptName = 0 ) { return NULL; }
virtual void *RestoreBody( CBaseEntity *pEntity ) { return NULL; }
virtual void SaveBody( CBaseEntity *pObject ) {}
Expand Down

0 comments on commit f6080e8

Please sign in to comment.