Browse Source

implement basic collision loading

physx
nillerusr 1 year ago
parent
commit
5324d8f328
  1. 1
      .gitignore
  2. 14
      vphysics-physx/main.cpp
  3. 95
      vphysics-physx/physics_collide.cpp
  4. 10
      vphysics-physx/physics_environment.cpp
  5. 1
      vphysics-physx/physics_environment.h
  6. 3
      vphysics-physx/physics_globals.h
  7. 26
      vphysics-physx/physics_object.cpp
  8. 6
      vphysics-physx/trace.cpp

1
.gitignore vendored

@ -37,3 +37,4 @@ waf3*/
.vscode/ .vscode/
.depproj/ .depproj/
source-engine.sln source-engine.sln
*.save

14
vphysics-physx/main.cpp

@ -12,6 +12,7 @@
#include "tier1/tier1.h" #include "tier1/tier1.h"
#include "ivu_vhash.hxx" #include "ivu_vhash.hxx"
#include "PxPhysicsAPI.h" #include "PxPhysicsAPI.h"
#include "convert.h"
using namespace physx; using namespace physx;
@ -80,7 +81,6 @@ private:
unsigned int m_bits[32]; unsigned int m_bits[32];
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Main physics interface // Main physics interface
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -127,6 +127,9 @@ InitReturnVal_t CPhysicsInterface::Init()
MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false ); MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false );
PxTolerancesScale scale;
scale.length = g_PhysicsUnits.unitScaleMetersInv; // typical length of an object
gPxFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gPxAllocatorCallback, gPxErrorCallback); gPxFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, gPxAllocatorCallback, gPxErrorCallback);
if( !gPxFoundation ) if( !gPxFoundation )
@ -142,7 +145,7 @@ InitReturnVal_t CPhysicsInterface::Init()
gPxPvd->connect(*transport,PxPvdInstrumentationFlag::eALL); gPxPvd->connect(*transport,PxPvdInstrumentationFlag::eALL);
gPxPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gPxFoundation, PxTolerancesScale(), recordMemoryAllocations, gPxPvd); gPxPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gPxFoundation, scale, recordMemoryAllocations, gPxPvd);
if( !gPxPhysics ) if( !gPxPhysics )
{ {
@ -150,15 +153,16 @@ InitReturnVal_t CPhysicsInterface::Init()
return INIT_FAILED; return INIT_FAILED;
} }
gPxCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gPxFoundation , PxCookingParams(PxTolerancesScale())); gPxCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gPxFoundation , PxCookingParams(scale));
return INIT_OK; return INIT_OK;
} }
void CPhysicsInterface::Shutdown() void CPhysicsInterface::Shutdown()
{ {
if( gPxCooking )
gPxCooking->release();
if( gPxPhysics ) if( gPxPhysics )
gPxPhysics->release(); gPxPhysics->release();

95
vphysics-physx/physics_collide.cpp

@ -30,6 +30,7 @@
#include "mathlib/polyhedron.h" #include "mathlib/polyhedron.h"
#include "tier1/byteswap.h" #include "tier1/byteswap.h"
#include "physics_globals.h" #include "physics_globals.h"
#include "tier1/smartptr.h"
// memdbgon must be the last include file in a .cpp file!!! // memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h" #include "tier0/memdbgon.h"
@ -319,32 +320,6 @@ struct moppheader_t : public physcollideheader_t
}; };
#endif #endif
#if ENABLE_IVP_MOPP
class CPhysCollideMopp : public CPhysCollide
{
public:
CPhysCollideMopp( const moppheader_t *pHeader );
CPhysCollideMopp( IVP_Compact_Mopp *pMopp );
CPhysCollideMopp( const char *pBuffer, unsigned int size );
~CPhysCollideMopp();
void Init( const char *pBuffer, unsigned int size );
// IPhysCollide
virtual int GetVCollideIndex() const { return 0; }
virtual IVP_SurfaceManager *CreateSurfaceManager( short & ) const;
virtual void GetAllLedges( IVP_U_BigVector<IVP_Compact_Ledge> &ledges ) const;
virtual unsigned int GetSerializationSize() const;
virtual Vector GetMassCenter() const;
virtual void SetMassCenter( const Vector &massCenter );
virtual unsigned int SerializeToBuffer( char *pDest, bool bSwap = false ) const;
virtual void OutputDebugInfo() const;
private:
IVP_Compact_Mopp *m_pMopp;
};
#endif
class CPhysCollideCompactSurface : public CPhysCollide class CPhysCollideCompactSurface : public CPhysCollide
{ {
public: public:
@ -1525,6 +1500,8 @@ CPhysConvex *CPhysicsCollision::BBoxToConvex( const Vector &mins, const Vector &
CPhysCollide *CPhysicsCollision::BBoxToCollide( const Vector &mins, const Vector &maxs ) CPhysCollide *CPhysicsCollision::BBoxToCollide( const Vector &mins, const Vector &maxs )
{ {
printf("BBoxToCollide\n");
// can't create a collision model for an empty box ! // can't create a collision model for an empty box !
if ( mins == maxs ) if ( mins == maxs )
{ {
@ -1604,7 +1581,8 @@ int CPhysicsCollision::CollideWrite( char *pDest, CPhysCollide *pCollide, bool b
CPhysCollide *CPhysicsCollision::UnserializeCollide( char *pBuffer, int size, int index ) CPhysCollide *CPhysicsCollision::UnserializeCollide( char *pBuffer, int size, int index )
{ {
return CPhysCollide::UnserializeFromBuffer( pBuffer, size, index ); return NULL;
// return CPhysCollide::UnserializeFromBuffer( pBuffer, size, index );
} }
class CPhysPolysoup class CPhysPolysoup
@ -1851,8 +1829,60 @@ float CPhysicsCollision::CollideSurfaceArea( CPhysCollide *pCollide )
return area; return area;
} }
PxConvexMesh *IVPLedgeToConvexShape( const ivp_compat::compactledge_t *pLedge )
{
if ( !pLedge->n_triangles )
return nullptr;
const char *pVertices = reinterpret_cast< const char * >( pLedge ) + pLedge->c_point_offset;
const ivp_compat::compacttriangle_t *pTriangles = reinterpret_cast< const ivp_compat::compacttriangle_t * >( pLedge + 1 );
const int nVertCount = pLedge->n_triangles * 3;
CArrayAutoPtr<PxVec3> convexVerts(new PxVec3[nVertCount]);
// Each triangle
for ( int i = 0; i < pLedge->n_triangles; i++ )
{
// For each point of the current triangle
for ( int j = 0; j < 3; j++ )
{
static constexpr size_t IVPAlignedVectorSize = 16;
const int nIndex = pTriangles[ i ].c_three_edges[ j ].start_point_index;
const float *pVertex = reinterpret_cast< const float * >( pVertices + ( nIndex * IVPAlignedVectorSize ) );
convexVerts[ ( i * 3 ) + j ] = PxVec3( IVP2HL(pVertex[0]), IVP2HL(pVertex[1]), IVP2HL(pVertex[2]) );
}
}
PxConvexMeshDesc convexDesc;
convexDesc.points.count = nVertCount;
convexDesc.points.stride = sizeof(PxVec3);
convexDesc.points.data = convexVerts.Get();
convexDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX;
PxDefaultMemoryOutputStream buf;
PxConvexMeshCookingResult::Enum result;
if(!gPxCooking->cookConvexMesh(convexDesc, buf, &result))
return NULL;
PxDefaultMemoryInputData input(buf.getData(), buf.getSize());
PxConvexMesh* convexMesh = gPxPhysics->createConvexMesh(input);
Msg("Cooking success %u!\n", convexMesh->getNbVertices());
//pConvexShape->SetUserData( pLedge->client_data );
return convexMesh;
}
static void GetAllIVPEdges(const ivp_compat::compactledgenode_t *pNode, CUtlVector<const ivp_compat::compactledge_t *> *vecOut) static void GetAllIVPEdges(const ivp_compat::compactledgenode_t *pNode, CUtlVector<const ivp_compat::compactledge_t *> *vecOut)
{ {
if (!pNode || !vecOut) return; if (!pNode || !vecOut) return;
if ( !pNode->IsTerminal() ) if ( !pNode->IsTerminal() )
@ -1874,6 +1904,17 @@ CPhysCollide *DeserializeIVP_Poly( const ivp_compat::compactsurface_t* pSurface
Msg("ledge count - %d\n", ledges.Count()); Msg("ledge count - %d\n", ledges.Count());
if( ledges.Count() == 1 )
{
PxConvexMesh *mesh = IVPLedgeToConvexShape( ledges[0] );
return (CPhysCollide*)mesh;
}
for( int i = 0; ledges.Count(); i++ )
{
}
return NULL; return NULL;
} }

10
vphysics-physx/physics_environment.cpp

@ -1886,11 +1886,11 @@ void CPhysicsEnvironment::PhantomAdd( CPhysicsObject *pObject )
void CPhysicsEnvironment::PhantomRemove( CPhysicsObject *pObject ) void CPhysicsEnvironment::PhantomRemove( CPhysicsObject *pObject )
{ {
IVP_Controller_Phantom *pPhantom = pObject->GetObject()->get_controller_phantom(); // IVP_Controller_Phantom *pPhantom = pObject->GetObject()->get_controller_phantom();
if ( pPhantom ) // if ( pPhantom )
{ // {
pPhantom->remove_listener_phantom( m_pCollisionListener ); // pPhantom->remove_listener_phantom( m_pCollisionListener );
} // }
} }

1
vphysics-physx/physics_environment.h

@ -47,6 +47,7 @@ public:
virtual IPhysicsObject *UnserializeObjectFromBuffer( void *pGameData, unsigned char *pBuffer, unsigned int bufferSize, bool enableCollisions ); virtual IPhysicsObject *UnserializeObjectFromBuffer( void *pGameData, unsigned char *pBuffer, unsigned int bufferSize, bool enableCollisions );
PxScene *GetPxScene() { return m_pPxScene; }
IPhysicsSpring *CreateSpring( IPhysicsObject *pObjectStart, IPhysicsObject *pObjectEnd, springparams_t *pParams ); IPhysicsSpring *CreateSpring( IPhysicsObject *pObjectStart, IPhysicsObject *pObjectEnd, springparams_t *pParams );
IPhysicsFluidController *CreateFluidController( IPhysicsObject *pFluidObject, fluidparams_t *pParams ); IPhysicsFluidController *CreateFluidController( IPhysicsObject *pFluidObject, fluidparams_t *pParams );

3
vphysics-physx/physics_globals.h

@ -5,5 +5,4 @@ using namespace physx;
extern PxFoundation *gPxFoundation; extern PxFoundation *gPxFoundation;
extern PxPvd *gPxPvd; extern PxPvd *gPxPvd;
extern PxPhysics *gPxPhysics; extern PxPhysics *gPxPhysics;
extern PxCooking *gPxCooking;

26
vphysics-physx/physics_object.cpp

@ -1072,6 +1072,32 @@ CPhysicsObject *CreatePhysicsObject( CPhysicsEnvironment *pEnvironment, const CP
// IVP_Polygon *realObject = pEnvironment->GetIVPEnvironment()->create_polygon(pSurman, &objectTemplate, &rotation, &pos); // IVP_Polygon *realObject = pEnvironment->GetIVPEnvironment()->create_polygon(pSurman, &objectTemplate, &rotation, &pos);
PxScene *scene = pEnvironment->GetPxScene();
PxConvexMesh *mesh = (PxConvexMesh*)pCollisionModel;
if( pCollisionModel && mesh->getNbVertices() != 0 )
{
RadianEuler radian(angles);
Quaternion qw(radian);
PxQuat q( qw.x, qw.y, qw.z, qw.w );
PxTransform t(PxVec3(position.x, position.y, position.z), q);
PxMaterial *mat = gPxPhysics->createMaterial(0.5f, 0.5f, 0.6f);
PxRigidStatic* body = gPxPhysics->createRigidStatic(t);
if( body )
{
PxShape* aConvexShape = PxRigidActorExt::createExclusiveShape(*body,
PxConvexMeshGeometry(mesh), *mat);
scene->addActor(*body);
}
//PxRigidActorExt::createExclusiveShape(*aConvexActor,
// PxConvexMeshGeometry(mesh), aMaterial);
}
pObject->Init( pCollisionModel, NULL, materialIndex, pParams->volume, pParams->dragCoefficient, pParams->dragCoefficient ); pObject->Init( pCollisionModel, NULL, materialIndex, pParams->volume, pParams->dragCoefficient, pParams->dragCoefficient );
pObject->SetGameData( pParams->pGameData ); pObject->SetGameData( pParams->pGameData );

6
vphysics-physx/trace.cpp

@ -321,8 +321,8 @@ public:
CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles ); CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles );
~CTraceIVP() ~CTraceIVP()
{ {
if ( m_pVisitHash ) // if ( m_pVisitHash )
FreeVisitHash(m_pVisitHash); // FreeVisitHash(m_pVisitHash);
} }
virtual int SupportMap( const Vector &dir, Vector *pOut ) const; virtual int SupportMap( const Vector &dir, Vector *pOut ) const;
virtual Vector GetVertByIndex( int index ) const; virtual Vector GetVertByIndex( int index ) const;
@ -479,6 +479,8 @@ FORCEINLINE fltx4 ConvertDirectionToIVP( const fltx4 & a )
CTraceIVP::CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles ) CTraceIVP::CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles )
{ {
return;
if( !pCollide ) if( !pCollide )
return; return;

Loading…
Cancel
Save