fixes for physics

This commit is contained in:
2026-03-17 01:01:10 +02:00
parent f3ed144fb5
commit 0a506f7185
24 changed files with 179 additions and 74 deletions

View File

@@ -117,6 +117,7 @@ extern "C" void FunnyMain( int argc, char **argv )
SteamAPI_RunCallbacks();
#endif
double fCurrent = Plat_GetTime();
double fStart = Plat_GetTime();
double fDelta = fCurrent-fPrevious;
fPrevious = fCurrent;
@@ -124,15 +125,23 @@ extern "C" void FunnyMain( int argc, char **argv )
g_pHumanDeviceManager->Frame();
g_pServerGame->m_pBridge->Frame(fDelta);
fCurrent = Plat_GetTime();
if (!stConstants.m_bIsDedicated)
{
g_pWindowManager->Frame(fDelta);
g_pClientGame->m_pBridge->Frame(fDelta);
g_pRenderContext->Frame(fDelta);
fCurrent = Plat_GetTime();
}
Console()->Execute();
fCurrent = Plat_GetTime();
};
};

2
external/SDL vendored

2
external/cglm vendored

2
external/slang vendored

2
external/stb vendored

2
external/volk vendored

2
external/xtool vendored

View File

@@ -1,4 +1,4 @@
{
"Type": "Triangles",
"Mesh": "game/core/maps/test/test0.fpx"
"Type": "TriangleMesh",
"Mesh": "game/core/maps/test/test0.fmesh_c"
}

View File

@@ -155,6 +155,8 @@ void CFunnyGameBridge::Frame( float fDelta )
float fTickRate = 1.0/60.0;
m_fNetUpdateTimer += fDelta;
if (m_fNetUpdateTimer >= fTickRate)
m_fNetUpdateTimer = fTickRate;
while (m_fNetUpdateTimer >= fTickRate)
{
m_fNetUpdateTimer-=fTickRate;

View File

@@ -93,6 +93,7 @@ public:
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) override;
CAssetArc<FunnyModel_t, MAX_MODEL_COUNT> m_models = {};
CAssetArc<FunnyMesh_t, MAX_MODEL_COUNT> m_meshes = {};
CAssetArc<FunnyPhysics_t, MAX_PHYSICS_COUNT> m_physics = {};
};
@@ -108,6 +109,7 @@ FunnyMaterial_t *CAssetManager::GetMaterialByIndex( uint32_t uIndex )
FunnyMesh_t *CAssetManager::GetMeshByIndex( uint32_t uIndex )
{
return m_meshes.GetObjectPtr(uIndex);
}
@@ -179,7 +181,30 @@ HFunnyMesh CAssetManager::LoadMesh( const char *szName )
{
return 0;
bool bHasBeenCreated = false;
HFunnyMesh hAsset = m_meshes.GetOrCreateObject(szName, &bHasBeenCreated);
if (!bHasBeenCreated)
return hAsset;
FunnyMesh_t *pMesh = m_meshes.GetObjectPtr(hAsset);
IFileHandle *hMesh = filesystem->Open(szName, FILEMODE_READ);
if ( hMesh == NULL )
{
V_printf("Failed to load %s\n", szName);
m_meshes.UnrefObject(hAsset);
return 0;
}
pMesh->m_nPositionCount = filesystem->Size(hMesh)/4;
pMesh->m_pfPositions = new float[pMesh->m_nPositionCount];
filesystem->Read( hMesh, pMesh->m_pfPositions, filesystem->Size(hMesh));
pMesh->m_nIndiciesCount = 0;
pMesh->m_puIndicies = 0;
return hAsset;
}
void CAssetManager::UnrefMesh( uint32_t uIndex )
@@ -231,8 +256,25 @@ HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
{
pPhysics->m_hShape = g_pPhysics->CreateBall({1.0});
}
if (szType == "Triangles")
if (szType == "TriangleMesh")
{
V_printf("hi\n");
IJSONValue *pMeshValue = pMainObject->GetValue("Mesh");
V_printf("hi\n");
if (!pMeshValue)
{
V_printf("\"Mesh\" must be specified\n");
m_physics.UnrefObject(hPhysics);
return 0;
}
CUtlString szMesh = pMeshValue->GetStringValue();
V_printf("%s\n", szMesh.GetString());
HFunnyMesh hMesh = LoadMesh(szMesh);
FunnyMesh_t *pMesh = GetMeshByIndex(hMesh);
pPhysics->m_hShape = g_pPhysics->CreateTriangleMesh({pMesh->m_pfPositions, pMesh->m_nPositionCount});
V_printf("%p\n", pPhysics->m_hShape);
pPhysics->m_hMesh = hMesh;
}
return hPhysics;

View File

@@ -16,11 +16,20 @@ struct FunnyMaterial_t
struct FunnyMesh_t
{
float *m_pfPositions;
uint32_t m_nPositionCount;
uint32_t *m_puIndicies;
uint32_t m_nIndiciesCount;
};
struct FunnyPhysics_t
{
HShape m_hShape;
union
{
HFunnyMesh m_hMesh;
};
};
struct FunnyModel_t

View File

@@ -205,6 +205,8 @@ void CFunnyGameBridge::Frame( float fDelta )
float fTickRate = 1.0/60.0;
m_fNetUpdateTimer += fDelta;
if (m_fNetUpdateTimer >= 0.3)
m_fNetUpdateTimer = 0.3;
while (m_fNetUpdateTimer >= fTickRate)
{
m_fNetUpdateTimer-=fTickRate;

View File

@@ -22,6 +22,7 @@ void CMOBAPlayer::Think( float fDelta )
vPosition.x += m_vMovementVector.x*fDelta*5;
vPosition.z += m_vMovementVector.z*fDelta*5;
SetAbsOrigin(vPosition);
V_printf("player: %f %f %f\n", vPosition.x, vPosition.y, vPosition.z);
};
LINK_ENTITY_TO_CLASS(player, CMOBAPlayer)

View File

@@ -11,8 +11,6 @@ void CPhysicsProp::Spawn()
{
CBaseEntity::Spawn();
SetThink(Think);
V_printf("hi\n");
}
void CPhysicsProp::Think( float fDelta )
@@ -60,6 +58,8 @@ void CPhysicsProp::OnModelChanged( const char *szName )
m_pPhysics = g_pAssetManager->GetPhysicsByIndex(m_pModel->m_hPhysics);
m_hCollider = g_pPhysics->CreateCollider(m_pPhysics->m_hShape);
m_pBody = g_pPhysicsWorld->CreateRigidBody(m_hCollider, k_EPhysics_Dynamic);
m_pBody->SetPosition({0,0,0});
m_pBody->SetRotation({0,0,0});
}
void CPhysicsProp::EnableMovement()
@@ -71,5 +71,8 @@ void CPhysicsProp::DisableMovement()
{
}
BEGIN_DATADESC(CPhysicsProp)
DEFINE_KEYFIELD(m_ePhysicsType, FIELD_INTEGER, "physics_type")
END_DATADESC()
LINK_ENTITY_TO_CLASS(prop_physics, CPhysicsProp)

View File

@@ -8,7 +8,8 @@ class CPhysicsProp: public CBaseModelEntity
friend CBaseEntity;
friend CBaseModelEntity;
public:
DECLARE_CLASS(CPhysicsProp, CBaseModelEntity);
DECLARE_CLASS(CPhysicsProp, CBaseModelEntity)
DECLARE_DATADESC()
virtual void Spawn() override;
virtual void Precache() override;
@@ -20,14 +21,18 @@ public:
virtual void EnableMovement();
virtual void DisableMovement();
virtual void SetPhysics( EPhysicsBodyType eType );
void Think( float fDelta );
private:
EPhysicsBodyType m_ePhysicsType;
EPhysicsBodyType m_eCurrentPhysicsType;
HFunnyModel m_hModel = 0;
FunnyModel_t *m_pModel = NULL;
FunnyPhysics_t *m_pPhysics = NULL;
HCollider m_hCollider = 0;
IPhysicsBody *m_pBody = NULL;
};
#endif

View File

@@ -17,7 +17,7 @@ struct CuboidShape_t
float m_fExtentZ;
};
struct TrianglesShape_t
struct TriangleMeshShape_t
{
float *m_pfPositions;
uint32_t m_nPositionCount;
@@ -45,6 +45,7 @@ public:
virtual void SetRotation( Quat vRotation ) = 0;
virtual Vector GetPosition() = 0;
virtual Quat GetRotation() = 0;
virtual void SetType( EPhysicsBodyType eType ) = 0;
virtual void SetGravityScale( float fScale ) = 0;
};
@@ -66,7 +67,7 @@ abstract_class IPhysics
public:
virtual HShape CreateBall( BallShape_t ball ) = 0;
virtual HShape CreateCube( CuboidShape_t ball ) = 0;
virtual HShape CreateTriangles( TrianglesShape_t shape ) = 0;
virtual HShape CreateTriangleMesh( TriangleMeshShape_t shape ) = 0;
virtual void DestroyShape( HShape hShape ) = 0;
virtual HCollider CreateCollider( HShape hShape ) = 0;

View File

@@ -17,11 +17,10 @@ DECLARE_BUILD_STAGE(rapier)
CUtlString szTarget = target.GetTriplet();
if (CommandLine()->CheckParam("-norust"))
return 0;
rapier_lib = CUtlString("target/%s/release/librapier_rtt.a",szTarget.GetString());
rapier_lib = CUtlString("target/%s/debug/librapier_rtt.a",szTarget.GetString());
V_printf("%s\n",rapier_lib.GetString());
CUtlVector<CUtlString> cargo_args = {
"build",
"--release",
"--target",
szTarget
};

View File

@@ -4,6 +4,7 @@
//
//===========================================================================//
#include "tier0/lib.h"
#include "physics.h"
#define PHYSICS_OBJECT_DEFINED
#define TRIG_H
@@ -95,9 +96,9 @@ public:
return CRapierPhysics_CreateCube(m_pRustHandle, ball);
}
virtual HShape CreateTriangles( TrianglesShape_t shape ) override
virtual HShape CreateTriangleMesh( TriangleMeshShape_t shape ) override
{
return CRapierPhysics_CreateCube(m_pRustHandle, shape);
return CRapierPhysics_CreateTriangleMesh(m_pRustHandle, shape);
}
virtual void DestroyShape( HShape hShape ) override

View File

@@ -43,6 +43,13 @@ typedef struct CuboidShape_t {
float m_fExtentZ;
} CuboidShape_t;
typedef struct TriangleMeshShape_t {
const float *m_pfPositions;
uint32_t m_nPositionCount;
const float *m_puIndicies;
uint32_t m_nIndiciesCount;
} TriangleMeshShape_t;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@@ -62,6 +69,8 @@ void CRapierPhysicsBody_SetRotation(struct RapierPhysicsBody_t *this_,
float fZ,
float fW);
void CRapierPhysicsBody_SetType(struct RapierPhysicsBody_t *this_, enum EPhysicsBodyType eType);
struct RapierPhysicsBody_t *CRapierPhysicsWorld_CreateRigidBody(struct RapierWorld_t *this_,
struct RapierCollider_t *pCollider,
enum EPhysicsBodyType eType);
@@ -77,6 +86,9 @@ struct RapierCollider_t *CRapierPhysics_CreateCollider(struct RapierPhysics_t *t
struct RapierShape_t *CRapierPhysics_CreateCube(struct RapierPhysics_t *this_,
struct CuboidShape_t cuboid);
struct RapierShape_t *CRapierPhysics_CreateTriangleMesh(struct RapierPhysics_t *this_,
struct TriangleMeshShape_t triangle);
struct RapierWorld_t *CRapierPhysics_CreateWorld(struct RapierPhysics_t *this_);
struct RapierPhysics_t *CRapierPhysics_New(void);

View File

@@ -13,7 +13,7 @@ macro_rules! V_malloc {
};
}
use std::{default, ptr::{self, null}, sync::Arc};
use std::{default, ops::Index, ptr::{self, null, null_mut}, slice::from_raw_parts, sync::Arc};
use parry3d::{glamx::vec3, shape::{Shape, ShapeType, SharedShape}};
use rapier3d::{geometry::Ball, na::{UnitQuaternion, Vector4}};
@@ -37,38 +37,21 @@ pub struct CuboidShape_t
m_fExtentZ: f32,
}
#[derive(Clone)]
#[repr(C)]
#[derive(Clone, Copy)]
#[derive(Debug)]
pub enum RapierShapeType {
Ball(Ball),
Cuboid(Cuboid),
Capsule(Capsule),
Segment(Segment),
Triangle(Triangle),
Voxels(Voxels),
TriMesh(TriMesh),
Polyline(Polyline),
HalfSpace(HalfSpace),
HeightField(HeightField),
Compound(Compound),
ConvexPolyhedron(ConvexPolyhedron),
Cylinder(Cylinder),
Cone(Cone),
RoundCuboid(RoundCuboid),
RoundTriangle(RoundTriangle),
RoundCylinder(RoundCylinder),
RoundCone(RoundCone),
RoundConvexPolyhedron(RoundConvexPolyhedron),
#[allow(dead_code)]
Custom,
pub struct TriangleMeshShape_t
{
m_pfPositions: *const f32,
m_nPositionCount: u32,
m_puIndicies: *const f32,
m_nIndiciesCount: u32,
}
#[derive(Clone)]
#[derive(Debug)]
pub struct RapierShape_t
{
m_shape: RapierShapeType,
m_sharedShape: SharedShape,
}
@@ -122,13 +105,6 @@ pub struct RapierPhysics_t
}
struct TrianglesShape_t
{
m_pfPositions: *const f32,
m_nPositionCount: const u32,
m_puIndicies,
m_nIndiciesCount,
};
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsBody_SetPosition( this: *mut RapierPhysicsBody_t, fX: f32, fY: f32, fZ: f32 )
@@ -157,7 +133,7 @@ pub struct Vector {
pub unsafe extern "C" fn CRapierPhysicsBody_GetPosition( this: *mut RapierPhysicsBody_t ) -> Vector
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
let position = world.m_rigidBodies[(*this).m_hRigidBodyHandle]
let mut position = world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.translation().to_array();
return Vector { x: position[0], y: position[1], z: position[2]}
}
@@ -179,6 +155,22 @@ pub unsafe extern "C" fn CRapierPhysicsBody_GetRotation( this: *mut RapierPhysic
Quat{ x: rotationVector[0], y: rotationVector[1], z: rotationVector[2], w: rotationVector[3]}
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsBody_SetType( this: *mut RapierPhysicsBody_t, eType: EPhysicsBodyType )
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
let eRapierBodyType: RigidBodyType;
match eType
{
EPhysicsBodyType::k_EPhysics_Static => eRapierBodyType = RigidBodyType::Fixed,
EPhysicsBodyType::k_EPhysics_Dynamic => eRapierBodyType = RigidBodyType::Dynamic,
EPhysicsBodyType::k_EPhysics_KinematicPositionBased => eRapierBodyType = RigidBodyType::KinematicPositionBased,
EPhysicsBodyType::k_EPhysics_KinematicVelocityBased => eRapierBodyType = RigidBodyType::KinematicVelocityBased,
}
world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.set_body_type(eRapierBodyType, true);
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsWorld_Frame( this: *mut RapierWorld_t, fDelta: f32 )
{
@@ -245,14 +237,8 @@ pub unsafe extern "C" fn CRapierPhysics_New() -> *mut RapierPhysics_t
pub unsafe extern "C" fn CRapierPhysics_CreateBall( this: *mut RapierPhysics_t, ball: BallShape_t ) -> *mut RapierShape_t
{
let pShapeMemory: *mut RapierShape_t = V_malloc!(RapierShape_t, 1);
std::ptr::write(&mut (*pShapeMemory).m_shape, RapierShapeType::Ball(Ball::new(ball.m_fRadius)));
match ((*pShapeMemory).m_shape)
{
RapierShapeType::Ball(b) => {
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(b));
}
_ => {}
}
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(Ball::new(ball.m_fRadius)));
pShapeMemory
}
@@ -260,15 +246,52 @@ pub unsafe extern "C" fn CRapierPhysics_CreateBall( this: *mut RapierPhysics_t,
pub unsafe extern "C" fn CRapierPhysics_CreateCube( this: *mut RapierPhysics_t, cuboid: CuboidShape_t ) -> *mut RapierShape_t
{
let pShapeMemory: *mut RapierShape_t = V_malloc!(RapierShape_t, 1);
std::ptr::write(&mut (*pShapeMemory).m_shape, RapierShapeType::Cuboid(
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(
Cuboid::new(vec3(cuboid.m_fExtentX, cuboid.m_fExtentY, cuboid.m_fExtentZ)))
);
match ((*pShapeMemory).m_shape)
pShapeMemory
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysics_CreateTriangleMesh( this: *mut RapierPhysics_t, triangle: TriangleMeshShape_t ) -> *mut RapierShape_t
{
let pShapeMemory: *mut RapierShape_t = V_malloc!(RapierShape_t, 1);
let positions = Vec::from_raw_parts(triangle.m_pfPositions as *mut Vec3, triangle.m_nPositionCount as usize, triangle.m_nPositionCount as usize);
let mut indices: Vec<[u32; 3]>;
if ( triangle.m_nIndiciesCount == 0 )
{
RapierShapeType::Cuboid(b) => {
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(b));
if ( triangle.m_nPositionCount % 3 != 0 )
{
return null_mut();
}
_ => {}
indices = vec![[0,0,0]; (triangle.m_nPositionCount/3) as usize];
for i in 0..indices.len()/3
{
let u = i as u32;
indices[i][0] = u*3;
indices[i][1] = u*3+1;
indices[i][2] = u*3+2;
}
}
else
{
if ( triangle.m_nIndiciesCount % 3 != 0 )
{
return null_mut();
}
indices = Vec::from_raw_parts(triangle.m_puIndicies as *mut [u32; 3], triangle.m_nIndiciesCount as usize, triangle.m_nIndiciesCount as usize);
}
let mesh = TriMesh::new(positions, indices);
match mesh
{
Ok(m) =>
{
std::ptr::write(&mut (*pShapeMemory).m_sharedShape, SharedShape::new(m));
}
default => {}
}
pShapeMemory
}
@@ -276,13 +299,9 @@ pub unsafe extern "C" fn CRapierPhysics_CreateCube( this: *mut RapierPhysics_t,
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysics_CreateCollider( this: *mut RapierPhysics_t, pShape: *mut RapierShape_t ) -> *mut RapierCollider_t
{
println!("this {:?}", pShape);
let shape: &SharedShape = &(*pShape).m_sharedShape;
println!("this {:?}", pShape);
let pCollider = V_malloc!(RapierCollider_t, 1);
println!("this {:?}", pShape);
std::ptr::write(&mut (*pCollider).m_collider, ColliderBuilder::new(shape.clone()).build());
println!("this {:?}", pShape);
pCollider
}