Skip to content

Commit

Permalink
implement basic collision loading
Browse files Browse the repository at this point in the history
  • Loading branch information
nillerusr committed Oct 17, 2023
1 parent 83e334f commit 5324d8f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ waf3*/
.vscode/
.depproj/
source-engine.sln
*.save
14 changes: 9 additions & 5 deletions vphysics-physx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "tier1/tier1.h"
#include "ivu_vhash.hxx"
#include "PxPhysicsAPI.h"
#include "convert.h"

using namespace physx;

Expand Down Expand Up @@ -80,7 +81,6 @@ class CPhysicsCollisionSet : public IPhysicsCollisionSet
unsigned int m_bits[32];
};


//-----------------------------------------------------------------------------
// Main physics interface
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -127,6 +127,9 @@ InitReturnVal_t CPhysicsInterface::Init()

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);

if( !gPxFoundation )
Expand All @@ -142,23 +145,24 @@ InitReturnVal_t CPhysicsInterface::Init()

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 )
{
Error("PxCreatePhysics failed!\n");
return INIT_FAILED;
}

gPxCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gPxFoundation , PxCookingParams(PxTolerancesScale()));


gPxCooking = PxCreateCooking(PX_PHYSICS_VERSION, *gPxFoundation , PxCookingParams(scale));
return INIT_OK;
}


void CPhysicsInterface::Shutdown()
{
if( gPxCooking )
gPxCooking->release();

if( gPxPhysics )
gPxPhysics->release();

Expand Down
95 changes: 68 additions & 27 deletions vphysics-physx/physics_collide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "mathlib/polyhedron.h"
#include "tier1/byteswap.h"
#include "physics_globals.h"
#include "tier1/smartptr.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
Expand Down Expand Up @@ -319,32 +320,6 @@ struct moppheader_t : public physcollideheader_t
};
#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
{
public:
Expand Down Expand Up @@ -1525,6 +1500,8 @@ CPhysConvex *CPhysicsCollision::BBoxToConvex( const Vector &mins, const Vector &

CPhysCollide *CPhysicsCollision::BBoxToCollide( const Vector &mins, const Vector &maxs )
{
printf("BBoxToCollide\n");

// can't create a collision model for an empty box !
if ( mins == maxs )
{
Expand Down Expand Up @@ -1604,7 +1581,8 @@ int CPhysicsCollision::CollideWrite( char *pDest, CPhysCollide *pCollide, bool b

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
Expand Down Expand Up @@ -1851,8 +1829,60 @@ float CPhysicsCollision::CollideSurfaceArea( CPhysCollide *pCollide )
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)
{


if (!pNode || !vecOut) return;

if ( !pNode->IsTerminal() )
Expand All @@ -1874,6 +1904,17 @@ CPhysCollide *DeserializeIVP_Poly( const ivp_compat::compactsurface_t* pSurface

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;
}

Expand Down
18 changes: 9 additions & 9 deletions vphysics-physx/physics_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,11 @@ CPhysicsEnvironment::CPhysicsEnvironment( void )

// PHYSX_BEGIN

PxSceneDesc sceneDesc(gPxPhysics->getTolerancesScale());
sceneDesc.gravity = physx::PxVec3(0.0f, -9.81f, 0.0f);
PxSceneDesc sceneDesc(gPxPhysics->getTolerancesScale());
sceneDesc.gravity = physx::PxVec3(0.0f, -9.81f, 0.0f);

m_pPxDispatcher = PxDefaultCpuDispatcherCreate(2);
sceneDesc.cpuDispatcher = m_pPxDispatcher;
m_pPxDispatcher = PxDefaultCpuDispatcherCreate(2);
sceneDesc.cpuDispatcher = m_pPxDispatcher;
sceneDesc.filterShader = PxDefaultSimulationFilterShader;

m_pPxScene = gPxPhysics->createScene(sceneDesc);
Expand Down Expand Up @@ -1886,11 +1886,11 @@ void CPhysicsEnvironment::PhantomAdd( CPhysicsObject *pObject )

void CPhysicsEnvironment::PhantomRemove( CPhysicsObject *pObject )
{
IVP_Controller_Phantom *pPhantom = pObject->GetObject()->get_controller_phantom();
if ( pPhantom )
{
pPhantom->remove_listener_phantom( m_pCollisionListener );
}
// IVP_Controller_Phantom *pPhantom = pObject->GetObject()->get_controller_phantom();
// if ( pPhantom )
// {
// pPhantom->remove_listener_phantom( m_pCollisionListener );
// }
}


Expand Down
1 change: 1 addition & 0 deletions vphysics-physx/physics_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CPhysicsEnvironment : public IPhysicsEnvironment
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 );
IPhysicsFluidController *CreateFluidController( IPhysicsObject *pFluidObject, fluidparams_t *pParams );
Expand Down
3 changes: 1 addition & 2 deletions vphysics-physx/physics_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ using namespace physx;
extern PxFoundation *gPxFoundation;
extern PxPvd *gPxPvd;
extern PxPhysics *gPxPhysics;


extern PxCooking *gPxCooking;
26 changes: 26 additions & 0 deletions vphysics-physx/physics_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,32 @@ CPhysicsObject *CreatePhysicsObject( CPhysicsEnvironment *pEnvironment, const CP

// 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->SetGameData( pParams->pGameData );

Expand Down
6 changes: 4 additions & 2 deletions vphysics-physx/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ class CTraceIVP : public ITraceObject
CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles );
~CTraceIVP()
{
if ( m_pVisitHash )
FreeVisitHash(m_pVisitHash);
// if ( m_pVisitHash )
// FreeVisitHash(m_pVisitHash);
}
virtual int SupportMap( const Vector &dir, Vector *pOut ) const;
virtual Vector GetVertByIndex( int index ) const;
Expand Down Expand Up @@ -479,6 +479,8 @@ FORCEINLINE fltx4 ConvertDirectionToIVP( const fltx4 & a )

CTraceIVP::CTraceIVP( const CPhysCollide *pCollide, const Vector &origin, const QAngle &angles )
{
return;

if( !pCollide )
return;

Expand Down

0 comments on commit 5324d8f

Please sign in to comment.