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

@@ -6,7 +6,9 @@
#include "physics.h"
#define PHYSICS_OBJECT_DEFINED
#define TRIG_H
#include "iphysics.h"
#include "tier0/lib.h"
#include "tier1/interface.h"
class CRapierPhysicsBody: public IPhysicsBody
@@ -20,14 +22,28 @@ public:
virtual void SetRotation( Quat vRotation ) override
{
CRapierPhysicsBody_SetRotation(m_pBody, vRotation.x, vRotation.y, vRotation.z, vRotation.w);
}
virtual void SetGravityScale( float fScale ) override
{
}
RapierPhysicsBody_t *m_pBody;
virtual Vector GetPosition() override
{
return CRapierPhysicsBody_GetPosition(m_pBody);
}
virtual Quat GetRotation() override
{
return CRapierPhysicsBody_GetRotation(m_pBody);
}
RapierPhysicsBody_t *m_pBody = NULL;
};
class CRapierPhysicsWorld: public IPhysicsWorld
@@ -58,7 +74,7 @@ public:
}
RapierWorld_t *m_pWorld;
RapierWorld_t *m_pWorld = NULL;
};
class CRapierPhysics: public IPhysics
@@ -109,7 +125,7 @@ public:
RapierPhysics_t *m_pRustHandle;
RapierPhysics_t *m_pRustHandle = NULL;
};
EXPOSE_INTERFACE(CRapierPhysics, IPhysics, PHYSICS_INTERFACE_VERSION)

View File

@@ -20,6 +20,19 @@ typedef struct RapierShape_t RapierShape_t;
typedef struct RapierWorld_t RapierWorld_t;
typedef struct Vector {
float x;
float y;
float z;
} Vector;
typedef struct Quat {
float x;
float y;
float z;
float w;
} Quat;
typedef struct BallShape_t {
float m_fRadius;
} BallShape_t;
@@ -34,11 +47,21 @@ typedef struct CuboidShape_t {
extern "C" {
#endif // __cplusplus
struct Vector CRapierPhysicsBody_GetPosition(struct RapierPhysicsBody_t *this_);
struct Quat CRapierPhysicsBody_GetRotation(struct RapierPhysicsBody_t *this_);
void CRapierPhysicsBody_SetPosition(struct RapierPhysicsBody_t *this_,
float fX,
float fY,
float fZ);
void CRapierPhysicsBody_SetRotation(struct RapierPhysicsBody_t *this_,
float fX,
float fY,
float fZ,
float fW);
struct RapierPhysicsBody_t *CRapierPhysicsWorld_CreateRigidBody(struct RapierWorld_t *this_,
struct RapierCollider_t *pCollider,
enum EPhysicsBodyType eType);

View File

@@ -16,7 +16,7 @@ macro_rules! V_malloc {
use std::{default, ptr::{self, null}, sync::Arc};
use parry3d::{glamx::vec3, shape::{Shape, ShapeType, SharedShape}};
use rapier3d::{geometry::Ball};
use rapier3d::{geometry::Ball, na::{UnitQuaternion, Vector4}};
use rapier3d::prelude::*;
use libc::{malloc, free};
@@ -93,26 +93,67 @@ pub struct RapierPhysics_t
{
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsBody_SetPosition( this: *mut RapierPhysicsBody_t, fX: f32, fY: f32, fZ: f32 )
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.set_translation(vec3(fX, fY, fZ), true);
print!("{} {} {}\n", fX, fY, fZ);
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsBody_SetRotation( this: *mut RapierPhysicsBody_t, fX: f32, fY: f32, fZ: f32, fW: f32 )
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.set_translation(vec3(fX, fY, fZ), true);
}
#[repr(C)]
pub struct Vector {
x: f32,
y: f32,
z: f32,
}
#[no_mangle]
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]
.translation().to_array();
return Vector { x: position[0], y: position[1], z: position[2]}
}
#[repr(C)]
pub struct Quat {
x: f32,
y: f32,
z: f32,
w: f32,
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsBody_GetRotation( this: *mut RapierPhysicsBody_t ) -> Quat
{
let world: &mut RapierWorld_t = &mut *(*this).m_pWorld;
let rotationVector = world.m_rigidBodies[(*this).m_hRigidBodyHandle]
.rotation().to_array();
Quat{ x: rotationVector[0], y: rotationVector[1], z: rotationVector[2], w: rotationVector[3]}
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsWorld_Frame( this: *mut RapierWorld_t, fDelta: f32 )
{
let gravity = vec3(0.0, -9.81, 0.0);
let vGravity = vec3(0.0, -9.8, 0.0);
let mut integrationParameters = IntegrationParameters::default();
integrationParameters.dt = fDelta;
let mut physicsPipeline = PhysicsPipeline::new();
let physicsHooks = ();
let eventHandler = ();
physicsPipeline.step(
gravity,
vGravity,
&integrationParameters,
&mut (*this).m_islandManager,
&mut (*this).m_broadPhase,
@@ -125,10 +166,6 @@ pub unsafe extern "C" fn CRapierPhysicsWorld_Frame( this: *mut RapierWorld_t, fD
&physicsHooks,
&eventHandler,
);
for (h, b) in (*this).m_colliders.iter()
{
print!("{:?}\n",b.position())
}
}
#[no_mangle]
@@ -207,12 +244,8 @@ pub unsafe extern "C" fn CRapierPhysics_CreateCollider( this: *mut RapierPhysics
{
let pRapierShape = (*pShape).m_pShape;
let shape: &SharedShape = &(*pShape).m_sharedShape;
let rapierCollider = ColliderBuilder::new(shape.clone()).build();
let collider = RapierCollider_t {
m_collider: rapierCollider,
};
let pCollider = V_malloc!(RapierCollider_t, 1);
*pCollider = collider;
std::ptr::write(&mut (*pCollider).m_collider, ColliderBuilder::new(shape.clone()).build());
pCollider
}