better physics

This commit is contained in:
2026-03-05 00:30:27 +02:00
parent ddbdef713b
commit 2da75ebdd8
68 changed files with 743 additions and 262450 deletions

View File

@@ -9,8 +9,7 @@
#define MAX_TEXTURE_COUNT 4096
#define MAX_SHADER_COUNT 1024
#define MAX_PHYSICAL_MESH_COUNT 1024
#define MAX_PHYSICAL_MATERIAL_COUNT 512
#define MAX_PHYSICS_COUNT 1024
template<typename T, uint32_t nCount>
class CAssetArc
@@ -89,13 +88,9 @@ public:
virtual FunnyMaterial_t *GetMaterialByIndex( HFunnyMaterial hMat ) override;
virtual void UnrefMaterial( HFunnyMaterial hMat ) override;
virtual HFunnyPhysicalMesh LoadPhysicalMesh( const char *szName ) override;
virtual FunnyPhysicalMesh_t *GetPhysicalMeshByIndex( HFunnyPhysicalMesh hPhysicalMesh ) override;
virtual void UnrefPhysicalMesh( HFunnyPhysicalMesh hPhysicalMesh ) override;
virtual HFunnyPhysicalMaterial LoadPhysicalMaterial( const char *szName ) override;
virtual FunnyPhysicalMaterial_t *GetPhysicalMaterialByIndex( HFunnyPhysicalMaterial hPhysicalMaterial ) override;
virtual void UnrefPhysicalMaterial( HFunnyPhysicalMaterial hPhysicalMaterial ) override;
virtual HFunnyPhysics LoadPhysics( const char *szName ) override;
virtual FunnyPhysics_t *GetPhysicsByIndex( HFunnyPhysics hPhysics ) override;
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) override;
uint32_t LoadShader( const char *szName );
IShader **GetShaderByIndex( uint32_t hShader );
@@ -105,6 +100,7 @@ public:
CAssetArc<FunnyMesh_t, MAX_MESH_COUNT> m_meshes = {};
CAssetArc<FunnyMaterial_t, MAX_MATERIAL_COUNT> m_materials = {};
CAssetArc<IShader*, MAX_SHADER_COUNT> m_shaders = {};
CAssetArc<FunnyPhysics_t, MAX_PHYSICS_COUNT> m_physics = {};
};
@@ -163,12 +159,15 @@ uint32_t CAssetManager::LoadModel( const char *szName )
return 0;
}
IJSONValue *pMesh = pMainObject->GetValue("mesh");
IJSONValue *pMaterial = pMainObject->GetValue("material");
IJSONValue *pMesh = pMainObject->GetValue("Mesh");
IJSONValue *pMaterial = pMainObject->GetValue("Material");
IJSONValue *pPhysics = pMainObject->GetValue("Physics");
if (pMesh)
pModel->m_hMesh = LoadMesh(pMesh->GetStringValue());
if (pMaterial)
pModel->m_hMaterial = LoadMaterial(pMaterial->GetStringValue());
if (pPhysics)
pModel->m_hPhysics = LoadPhysics(pPhysics->GetStringValue());
@@ -290,6 +289,7 @@ uint32_t CAssetManager::LoadShader( const char *szName )
if ( pShader == NULL )
{
V_printf("Failed to load %s\n", szName);
m_shaders.UnrefObject(hShader);
return 0;
}
*ppShader = pShader;
@@ -298,40 +298,73 @@ uint32_t CAssetManager::LoadShader( const char *szName )
void CAssetManager::UnrefShader( uint32_t uIndex )
{
m_shaders.UnrefObject(uIndex);
}
HFunnyPhysicalMesh CAssetManager::LoadPhysicalMesh( const char *szName )
HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
{
bool bHasBeenCreated = false;
HFunnyPhysics hPhysics = m_physics.GetOrCreateObject(szName, &bHasBeenCreated);
if (!bHasBeenCreated)
return hPhysics;
FunnyPhysics_t *pPhysics = m_physics.GetObjectPtr(hPhysics);
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);
if (!pHandle)
return 0;
CUtlString szProperties = filesystem->ReadString(pHandle);
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
if (!pRoot)
return 0;
IJSONObject *pMainObject;
switch (pRoot->GetType())
{
case JSON_PARAMETER_OBJECT:
{
pMainObject = pRoot->GetObject();
if (!pMainObject)
{
V_printf("Failed to load properties\n");
m_physics.UnrefObject(hPhysics);
return 0;
}
IJSONValue *pTypeValue = pMainObject->GetValue("Type");
if (!pTypeValue)
{
V_printf("\"Type\" must be specified\n");
m_physics.UnrefObject(hPhysics);
return 0;
}
CUtlString szType = pTypeValue->GetStringValue();
if (szType == "Sphere")
{
pPhysics->m_hShape = g_pPhysics->CreateBall({1.0});
}
return hPhysics;
}
break;
default:
return 0;
}
return hPhysics;
}
FunnyPhysicalMesh_t *CAssetManager::GetPhysicalMeshByIndex( HFunnyPhysicalMesh hPhysicalMesh )
FunnyPhysics_t *CAssetManager::GetPhysicsByIndex( HFunnyPhysics hPhysics )
{
return m_physics.GetObjectPtr(hPhysics);
}
void CAssetManager::UnrefPhysicalMesh( HFunnyPhysicalMesh hPhysicalMesh )
void CAssetManager::UnrefPhysics( HFunnyPhysics hPhysics )
{
m_physics.UnrefObject(hPhysics);
}
HFunnyPhysicalMaterial CAssetManager::LoadPhysicalMaterial( const char *szName )
{
}
FunnyPhysicalMaterial_t *CAssetManager::GetPhysicalMaterialByIndex( HFunnyPhysicalMaterial hPhysicalMaterial )
{
}
void CAssetManager::UnrefPhysicalMaterial( HFunnyPhysicalMaterial hPhysicalMaterial )
{
}
static CAssetManager s_assetmgr;

View File

@@ -10,8 +10,7 @@
typedef uint32_t HFunnyMaterial;
typedef uint32_t HFunnyMesh;
typedef uint32_t HFunnyPhysicalMesh;
typedef uint32_t HFunnyPhysicalMaterial;
typedef uint32_t HFunnyPhysics;
typedef uint32_t HFunnyModel;
struct FunnyMaterial_t
@@ -26,11 +25,7 @@ struct FunnyMesh_t
IMesh *m_pMesh;
};
struct FunnyPhysicalMaterial_t
{
};
struct FunnyPhysicalMesh_t
struct FunnyPhysics_t
{
HShape m_hShape;
};
@@ -39,8 +34,7 @@ struct FunnyModel_t
{
HFunnyMesh m_hMesh;
HFunnyMaterial m_hMaterial;
HFunnyPhysicalMesh m_hPhysicalMesh;
HFunnyPhysicalMaterial m_hPhysicalMaterial;
HFunnyPhysics m_hPhysics;
};
@@ -59,17 +53,11 @@ public:
virtual FunnyMaterial_t *GetMaterialByIndex( HFunnyMaterial hMat ) = 0;
virtual void UnrefMaterial( HFunnyMaterial hMat ) = 0;
virtual HFunnyPhysicalMesh LoadPhysicalMesh( const char *szName ) = 0;
virtual FunnyPhysicalMesh_t *GetPhysicalMeshByIndex( HFunnyPhysicalMesh hPhysicalMesh ) = 0;
virtual void UnrefPhysicalMesh( HFunnyPhysicalMesh hPhysicalMesh ) = 0;
virtual HFunnyPhysicalMaterial LoadPhysicalMaterial( const char *szName ) = 0;
virtual FunnyPhysicalMaterial_t *GetPhysicalMaterialByIndex( HFunnyPhysicalMaterial hPhysicalMaterial ) = 0;
virtual void UnrefPhysicalMaterial( HFunnyPhysicalMaterial hPhysicalMaterial ) = 0;
virtual HFunnyPhysics LoadPhysics( const char *szName ) = 0;
virtual FunnyPhysics_t *GetPhysicsByIndex( HFunnyPhysics hPhysics ) = 0;
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) = 0;
};
extern IAssetManager *g_pAssetManager;
#define ASSET_MANAGER_INTERFACE_VERSION "AssetMgr001"
#endif

View File

@@ -22,11 +22,16 @@ void C_BaseEntity::Spawn()
{
Precache();
SetAbsOrigin({0, 0, 0});
SetAbsAngles(0, 0, 0);
SetAbsQAngles(0, 0, 0);
SetScale(1);
}
void C_BaseEntity::SetAbsAngles( float fPitch, float fYaw, float fRoll )
void C_BaseEntity::SetAbsAngles( Quat vQuat )
{
m_vRotation = vQuat;
}
void C_BaseEntity::SetAbsQAngles( float fPitch, float fYaw, float fRoll )
{
versor q;
glm_euler_yzx_quat((vec3){fPitch, fYaw, fRoll}, q);

View File

@@ -61,7 +61,8 @@ public:
virtual void Precache();
virtual void Spawn();
virtual void SetAbsAngles( float fPitch, float fYaw, float fRoll );
virtual void SetAbsAngles( Quat vQuat );
virtual void SetAbsQAngles( float fPitch, float fYaw, float fRoll );
virtual void SetAbsOrigin( Vector origin );
virtual void SetScale( float fScale );

View File

@@ -20,12 +20,12 @@ void C_BaseModelEntity::Think( float fDelta )
void C_BaseModelEntity::SetModel( const char *szName )
{
if (m_uModelIndex)
if (m_hModelHandle)
{
g_pAssetManager->UnrefModel(m_uModelIndex);
g_pAssetManager->UnrefModel(m_hModelHandle);
}
m_uModelIndex = g_pAssetManager->LoadModel(szName);
m_pModel = g_pAssetManager->GetModelByIndex(m_uModelIndex);
m_hModelHandle = g_pAssetManager->LoadModel(szName);
m_pModel = g_pAssetManager->GetModelByIndex(m_hModelHandle);
FunnyMesh_t *pMesh = g_pAssetManager->GetMeshByIndex(m_pModel->m_hMesh);
m_pInstance = g_pWorldRenderer->CreateInstance(pMesh->m_pMesh);
@@ -35,7 +35,7 @@ C_BaseModelEntity::~C_BaseModelEntity()
{
FunnyMesh_t *pMesh = g_pAssetManager->GetMeshByIndex(m_pModel->m_hMesh);
g_pWorldRenderer->DestroyMeshInstance(pMesh->m_pMesh, m_pInstance);
g_pAssetManager->UnrefModel(m_uModelIndex);
g_pAssetManager->UnrefModel(m_hModelHandle);
}
BEGIN_DATADESC(C_BaseModelEntity)

View File

@@ -16,8 +16,10 @@
#endif
IFileSystem *filesystem;
IRenderContext *g_pRenderContext;
IGameWindow *g_pMainWindow;
static CEngineVars s_vars;
CEngineVars *g_pEngineVars = &s_vars;
EngineConsts_t *g_pEngineConstants;
@@ -25,6 +27,9 @@ EngineConsts_t *g_pEngineConstants;
INetworkBase *g_pServerBridge;
INetworkBase *g_pServerConnection;
IPhysics *g_pPhysics;
IPhysicsWorld *g_pPhysicsWorld;
class CFunnyGameBridge: public IEngineBridge
{
virtual void Init() override;
@@ -70,6 +75,10 @@ void CFunnyGameBridge::Init()
};
g_pServerBridge->SendPacket({&join, sizeof(join)});
}
CreateInterfaceFn fnPhysicsFactory = Sys_GetFactory("RapierPhysics");
g_pPhysics = (IPhysics*)fnPhysicsFactory(PHYSICS_INTERFACE_VERSION, NULL);
g_pPhysicsWorld = g_pPhysics->CreateWorld();
}
void CFunnyGameBridge::Tick( float fDelta )