work on physics begun

This commit is contained in:
2026-03-03 00:51:04 +02:00
parent 0dc8f1b36f
commit 02898d62c0
10 changed files with 132 additions and 388 deletions

View File

@@ -14,6 +14,7 @@ ADD_DEPENDENCY_BUILD_FILE(tier0, "external/funnystdlib/tier0/build.cpp");
ADD_DEPENDENCY_BUILD_FILE(fsc, "shadercompiler/build.cpp"); ADD_DEPENDENCY_BUILD_FILE(fsc, "shadercompiler/build.cpp");
ADD_DEPENDENCY_BUILD_FILE(server, "game/server/build.cpp"); ADD_DEPENDENCY_BUILD_FILE(server, "game/server/build.cpp");
ADD_DEPENDENCY_BUILD_FILE(client, "game/client/build.cpp"); ADD_DEPENDENCY_BUILD_FILE(client, "game/client/build.cpp");
ADD_DEPENDENCY_BUILD_FILE(rapier, "rapier/build.cpp");
#define EXTERNAL "external/" #define EXTERNAL "external/"
DECLARE_BUILD_STAGE(install_game) DECLARE_BUILD_STAGE(install_game)

View File

@@ -10,6 +10,7 @@ struct FunnyShader_t
}; };
#define MAX_MODEL_COUNT 2048 #define MAX_MODEL_COUNT 2048
#define MAX_MATERIAL_COUNT 2048 #define MAX_MATERIAL_COUNT 2048
#define MAX_TEXTURE_COUNT 4096
#define MAX_SHADER_COUNT 1024 #define MAX_SHADER_COUNT 1024
class CAssetManager: public IAssetManager class CAssetManager: public IAssetManager
@@ -30,9 +31,13 @@ public:
FunnyModel_t *m_models[MAX_MODEL_COUNT] = {}; FunnyModel_t *m_models[MAX_MODEL_COUNT] = {};
uint32_t m_modelUsages[MAX_MODEL_COUNT] = {}; uint32_t m_modelUsages[MAX_MODEL_COUNT] = {};
FunnyMaterial_t *m_materials[MAX_MATERIAL_COUNT] = {}; FunnyMaterial_t *m_materials[MAX_MATERIAL_COUNT] = {};
uint32_t m_materialsUsages[MAX_MATERIAL_COUNT] = {}; uint32_t m_materialsUsages[MAX_MATERIAL_COUNT] = {};
FunnyMaterial_t *m_textures[MAX_TEXTURE_COUNT] = {};
uint32_t m_textureUsages[MAX_TEXTURE_COUNT] = {};
FunnyShader_t *m_shaders[MAX_SHADER_COUNT] = {}; FunnyShader_t *m_shaders[MAX_SHADER_COUNT] = {};
uint32_t m_shaderUsages[MAX_SHADER_COUNT] = {}; uint32_t m_shaderUsages[MAX_SHADER_COUNT] = {};

View File

@@ -16,6 +16,9 @@ public:
void Think( float fDelta ); void Think( float fDelta );
float m_fTimer; float m_fTimer;
int m_bIsShooting;
float m_fFBWalkingDirection;
float m_fLRWalkingDirection;
}; };
#endif #endif

62
public/iphysics.h Normal file
View File

@@ -0,0 +1,62 @@
#ifndef PHYSICS_H
#define PHYSICS_H
#include "tier0/platform.h"
#include "trig.h"
struct BallShape_t
{
float m_fRadius;
};
struct CuboidShape_t
{
float m_fExtentX;
float m_fExtentY;
float m_fExtentZ;
};
typedef void *HShape;
typedef void *HCollider;
enum EPhysicsBodyType
{
k_EPhysics_Static,
k_EPhysics_Dynamic,
k_EPhysics_KinematicPositionBased,
k_EPhysics_KinematicRotationBased,
};
abstract_class IPhysicsBody
{
public:
virtual void SetPosition( Vector vPosition ) = 0;
virtual void SetRotation( Quat vRotation ) = 0;
virtual void SetGravityScale( float fScale ) = 0;
};
abstract_class IPhysicsWorld
{
public:
virtual void SetGravity( float fGravity ) = 0;
};
abstract_class IPhysics
{
public:
virtual HShape CreateBall( BallShape_t ball ) = 0;
virtual HShape CreateCube( CuboidShape_t ball ) = 0;
virtual void DestroyShape( HShape hShape ) = 0;
virtual HCollider CreateCollider( HShape hShape ) = 0;
virtual IPhysicsWorld *CreateWorld() = 0;
virtual void DestroyWorld( IPhysicsWorld *pWorld ) = 0;
virtual IPhysicsBody *CreateRigidBody( HCollider hCollider, EPhysicsBodyType eType) = 0;
virtual void DestroyPhysicsBody( IPhysicsBody *pBody ) = 0;
};
#endif

View File

@@ -3,98 +3,17 @@
typedef struct funnyphysics funnyphysics; typedef struct RapierShape_t RapierShape_t;
typedef struct px_collider_params { typedef struct BallShape_t {
float friction; float m_fRadius;
} px_collider_params; } BallShape_t;
typedef struct px_vec3 {
float m[3];
} px_vec3;
typedef struct px_cast_result {
int8_t hit;
float time;
struct px_vec3 witness1;
struct px_vec3 witness2;
struct px_vec3 normal1;
struct px_vec3 normal2;
} px_cast_result;
typedef struct px_matrix {
float m[16];
} px_matrix;
typedef struct px_rigidbody_params {
float velocity[3];
float angular_velocity[3];
float gravity_scale;
float linear_damping;
float angular_damping;
uint8_t continous;
uint8_t sleeping;
uint8_t lockrotx;
uint8_t lockroty;
uint8_t lockrotz;
uint8_t lockposx;
uint8_t lockposy;
uint8_t lockposz;
int8_t dominance;
} px_rigidbody_params;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // __cplusplus #endif // __cplusplus
Collider *px_ball(float radius, struct px_collider_params params); struct RapierShape_t *CRapierPhysics_CreateBall(struct BallShape_t ball);
Collider *px_box(float x, float y, float z, struct px_collider_params params);
struct px_cast_result px_box_cast(struct funnyphysics *px_world,
float x,
float y,
float z,
struct px_vec3 pos,
struct px_vec3 rot,
struct px_vec3 vel,
float time);
void px_fixedbody(struct funnyphysics *px_world, Collider *collider);
void px_frame(struct funnyphysics *px_world, float delta);
struct px_matrix px_getmatrix(struct funnyphysics *px_world, RigidBodyHandle *body);
struct px_vec3 px_getposition(struct funnyphysics *px_world, RigidBodyHandle *body);
struct px_vec3 px_getvelocity(struct funnyphysics *px_world, RigidBodyHandle *body);
struct funnyphysics *px_init(void);
RigidBodyHandle *px_kinematic_position_body(struct funnyphysics *px_world,
Collider *collider,
struct px_matrix m,
struct px_rigidbody_params params);
RigidBodyHandle *px_rigidbody(struct funnyphysics *px_world,
Collider *collider,
struct px_matrix m,
struct px_rigidbody_params params);
void px_setposition(struct funnyphysics *px_world, RigidBodyHandle *body, struct px_vec3 vec);
void px_setvelocity(struct funnyphysics *px_world, RigidBodyHandle *body, struct px_vec3 vec);
RigidBodyHandle *px_staticbody(struct funnyphysics *px_world,
Collider *collider,
struct px_matrix m);
Collider *px_trimesh(const Point<float> *ptr_vert,
size_t len_vert,
const uint32_t (*ptr_ind)[3],
size_t len_ind,
struct px_collider_params params);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View File

@@ -1,15 +1,16 @@
[package] [package]
name = "rapier_rtt" name = "rapier_rtt"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2021"
[target.aarch64-apple-ios] [target.aarch64-apple-ios]
rustflags = ["-C", "link-arg=-mios-version-min=13.0"] rustflags = ["-C", "link-arg=-mios-version-min=13.0"]
[lib] [lib]
crate-type = ["staticlib"] crate-type = ["staticlib"]
path = "px.rs" path = "physics.rs"
[dependencies] [dependencies]
libc = "0.2.182"
parry3d = "*" parry3d = "*"
rapier3d = { version = "*", features = [ "simd-stable", "parallel" ] } rapier3d = { version = "*", features = [ "simd-stable", "parallel" ] }

View File

@@ -7,6 +7,9 @@
CUtlString rapier_lib; CUtlString rapier_lib;
DECLARE_BUILD_STAGE(rapier) DECLARE_BUILD_STAGE(rapier)
{ {
Target_t target = Target_t::DefaultTarget();
target.abi = TARGET_ABI_GNU;
CUtlString szTarget = target.GetTriplet();
if (CommandLine()->CheckParam("-norust")) if (CommandLine()->CheckParam("-norust"))
return 0; return 0;
rapier_lib = CUtlString("rapier/target/%s/release/librapier_rtt.a",szTarget.GetString()); rapier_lib = CUtlString("rapier/target/%s/release/librapier_rtt.a",szTarget.GetString());

0
rapier/physics.cpp Normal file
View File

50
rapier/physics.rs Normal file
View File

@@ -0,0 +1,50 @@
/*
* Rapier bindings are defined here
*/
#![allow(nonstandard_style)]
macro_rules! V_malloc {
($t:ty, $count:expr) => {
malloc(size_of::<$t>() * $count) as *mut $t
};
}
use std::ptr::{self, null};
use std::mem::transmute;
use parry3d::shape::{Shape, ShapeType};
use rapier3d::geometry::Ball;
use libc::{malloc, free};
#[repr(C)]
pub struct BallShape_t
{
m_fRadius: f32,
}
pub struct RapierShape_t
{
m_eType: ShapeType,
m_shape: *mut dyn Shape,
}
pub struct RapierPhysics
{
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CRapierPhysics_CreateBall( ball: BallShape_t ) -> *mut RapierShape_t
{
let rapierShape = Ball::new(ball.m_fRadius);
let rapierShapeMemory: *mut Ball = V_malloc!(Ball, 1);
*rapierShapeMemory = rapierShape;
let shape: RapierShape_t = RapierShape_t { m_eType: ShapeType::Ball, m_shape: rapierShapeMemory };
let shapeMemory: *mut RapierShape_t = V_malloc!(RapierShape_t, 1);
*shapeMemory = shape;
shapeMemory
}

View File

@@ -1,300 +0,0 @@
/*
* Rapier bindings are defined here
* We use crust btw
*/
extern crate rapier3d;
extern crate parry3d;
use rapier3d::{na::{Isometry, Vector3}, prelude::*, pipeline};
use self::parry3d::query::*;
use std::num::NonZeroUsize;
use std::arch::asm;
pub struct funnyphysics {
rigid_body_set: RigidBodySet,
collider_set: ColliderSet,
physics_pipeline: PhysicsPipeline,
island_manager: IslandManager,
broad_phase: DefaultBroadPhase,
narrow_phase: NarrowPhase,
impulse_joint_set: ImpulseJointSet,
multibody_joint_set: MultibodyJointSet,
ccd_solver: CCDSolver,
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_init() -> *mut funnyphysics {
let mut px = Box::new(funnyphysics
{
rigid_body_set:RigidBodySet::new(),
collider_set:ColliderSet::new(),
physics_pipeline : PhysicsPipeline::new(),
island_manager : IslandManager::new(),
broad_phase : DefaultBroadPhase::new(),
narrow_phase : NarrowPhase::new(),
impulse_joint_set : ImpulseJointSet::new(),
multibody_joint_set : MultibodyJointSet::new(),
ccd_solver : CCDSolver::new(),
});
return Box::into_raw(px);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_frame(px_world: *mut funnyphysics, delta:f32) {
let gravity = vector![0.0, 0.0, -9.81];
let mut integration_parameters = IntegrationParameters::default();
integration_parameters.dt=delta;
integration_parameters.min_island_size=256;
integration_parameters.num_solver_iterations=NonZeroUsize::new(8).unwrap();
integration_parameters.num_internal_stabilization_iterations=4;
let physics_hooks = ();
let event_handler = ();
if let Some(px) = px_world.as_mut() {
px.physics_pipeline.step(
&gravity,
&integration_parameters,
&mut px.island_manager,
&mut px.broad_phase,
&mut px.narrow_phase,
&mut px.rigid_body_set,
&mut px.collider_set,
&mut px.impulse_joint_set,
&mut px.multibody_joint_set,
&mut px.ccd_solver,
&physics_hooks,
&event_handler,
);
};
}
#[repr(C)]
pub struct px_collider_params {
friction:f32
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_ball(radius:f32, params:px_collider_params) -> *mut Collider {
Box::into_raw(Box::new(ColliderBuilder::ball(radius).friction(params.friction).build()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_box(x:f32,y:f32,z:f32, params:px_collider_params) -> *mut Collider {
Box::into_raw(Box::new(ColliderBuilder::cuboid(x, y, z).friction(params.friction).build()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_trimesh(ptr_vert: *const Point<f32>, len_vert: usize, ptr_ind: *const [u32; 3], len_ind: usize, params:px_collider_params) -> *mut Collider {
let vertices: &[Point<f32>] = std::slice::from_raw_parts(ptr_vert, len_vert);
let indices: &[[u32; 3]] = std::slice::from_raw_parts(ptr_ind, len_ind);
Box::into_raw(Box::new(ColliderBuilder::trimesh(vertices.to_vec(), indices.to_vec()).unwrap().friction(params.friction).build()))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_staticbody(px_world: *mut funnyphysics, collider: *mut Collider, m: px_matrix) -> *mut RigidBodyHandle {
let c = &mut *collider;
let px = px_world.as_mut().unwrap();
let rigid_body = RigidBodyBuilder::fixed()
.translation(vector![m.m[3],m.m[7],m.m[11]])
.dominance_group(127)
.build();
let body = px.rigid_body_set.insert(rigid_body);
px.collider_set.insert_with_parent(c.clone(),body,&mut px.rigid_body_set);
Box::into_raw(Box::new(body))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_fixedbody(px_world: *mut funnyphysics, collider: *mut Collider) {
let c = &mut *collider;
let px = px_world.as_mut().unwrap();
px.collider_set.insert(c.clone());
}
#[repr(C)]
pub struct px_matrix {
m: [f32; 16],
}
#[repr(C)]
#[derive(Default)]
pub struct px_vec3 {
m: [f32; 3],
}
#[repr(C)]
pub struct px_rigidbody_params {
velocity: [f32;3],
angular_velocity: [f32;3],
gravity_scale: f32,
linear_damping: f32,
angular_damping: f32,
continous: u8,
sleeping: u8,
lockrotx: u8,
lockroty: u8,
lockrotz: u8,
lockposx: u8,
lockposy: u8,
lockposz: u8,
dominance: i8,
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_kinematic_position_body(px_world: *mut funnyphysics, collider: *mut Collider, m:px_matrix,params:px_rigidbody_params) -> *mut RigidBodyHandle {
let c = &mut *collider;
let px = px_world.as_mut().unwrap();
let rigid_body = RigidBodyBuilder::kinematic_position_based()
.translation(vector![m.m[12],m.m[13],m.m[14]])
.can_sleep(false)
.gravity_scale(1.0)
.enabled_rotations(params.lockrotx==0, params.lockroty==0, params.lockrotz==0)
.enabled_translations(params.lockposx==0, params.lockposy==0, params.lockposz==0)
.dominance_group(params.dominance)
.linear_damping(0.0)
.angular_damping(0.0)
.ccd_enabled(params.continous == 1)
.build();
let body = px.rigid_body_set.insert(rigid_body);
px.collider_set.insert_with_parent(c.clone(),body,&mut px.rigid_body_set);
Box::into_raw(Box::new(body))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_rigidbody(px_world: *mut funnyphysics, collider: *mut Collider, m:px_matrix,params:px_rigidbody_params) -> *mut RigidBodyHandle {
let c = &mut *collider;
let px = px_world.as_mut().unwrap();
let rigid_body = RigidBodyBuilder::dynamic()
.translation(vector![m.m[12],m.m[13],m.m[14]])
.can_sleep(false)
.gravity_scale(params.gravity_scale)
.enabled_rotations(params.lockrotx==0, params.lockroty==0, params.lockrotz==0)
.enabled_translations(params.lockposx==0, params.lockposy==0, params.lockposz==0)
.dominance_group(params.dominance)
.linear_damping(0.0)
.angular_damping(0.0)
.ccd_enabled(params.continous == 1)
.build();
let body = px.rigid_body_set.insert(rigid_body);
px.collider_set.insert_with_parent(c.clone(),body,&mut px.rigid_body_set);
Box::into_raw(Box::new(body))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_getposition(px_world: *mut funnyphysics, body: *mut RigidBodyHandle) -> px_vec3 {
let c = &mut *body;
let px = px_world.as_mut().unwrap();
let mut m = px_vec3 { m: [0.0,0.0,0.0]};
let b = &px.rigid_body_set[*c];
m.m[0]=b.translation().x;
m.m[1]=b.translation().y;
m.m[2]=b.translation().z;
m
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_setposition(px_world: *mut funnyphysics, body: *mut RigidBodyHandle, vec: px_vec3) {
let c = &mut *body;
let px = px_world.as_mut().unwrap();
let b = &mut px.rigid_body_set[*c];
b.set_translation(vector![vec.m[0],vec.m[1],vec.m[2]],true);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_getvelocity(px_world: *mut funnyphysics, body: *mut RigidBodyHandle) -> px_vec3 {
let c = &mut *body;
let px = px_world.as_mut().unwrap();
let mut m = px_vec3 { m: [0.0,0.0,0.0]};
let b = &px.rigid_body_set[*c];
m.m[0]=b.linvel().x;
m.m[1]=b.linvel().y;
m.m[2]=b.linvel().z;
m
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_setvelocity(px_world: *mut funnyphysics, body: *mut RigidBodyHandle, vec: px_vec3) {
let c = &mut *body;
let px = px_world.as_mut().unwrap();
let b = &mut px.rigid_body_set[*c];
b.set_linvel(vector![vec.m[0],vec.m[1],vec.m[2]],true);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_getmatrix(px_world: *mut funnyphysics, body: *mut RigidBodyHandle) -> px_matrix {
let c = &mut *body;
let px = px_world.as_mut().unwrap();
let mut m = px_matrix { m: [
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0,
]};
let b = &px.rigid_body_set[*c];
let mat = b.rotation().to_rotation_matrix();
m.m[0]=mat[(0,0)];
m.m[4]=mat[(0,1)];
m.m[8]=mat[(0,2)];
m.m[1]=mat[(1,0)];
m.m[5]=mat[(1,1)];
m.m[9]=mat[(1,2)];
m.m[2]=mat[(2,0)];
m.m[6]=mat[(2,1)];
m.m[10]=mat[(2,2)];
m.m[12]=b.translation().x;
m.m[13]=b.translation().y;
m.m[14]=b.translation().z;
m
}
#[repr(C)]
#[derive(Default)]
pub struct px_cast_result {
hit: i8,
time: f32,
witness1: px_vec3,
witness2: px_vec3,
normal1: px_vec3,
normal2: px_vec3,
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn px_box_cast(px_world: *mut funnyphysics, x:f32,y:f32,z:f32, pos:px_vec3, rot: px_vec3, vel: px_vec3, time:f32) -> px_cast_result {
let c = Cuboid::new(vector![x,y,z]);
let mut res = px_cast_result::default();
let px = px_world.as_mut().unwrap();
let filter = QueryFilter::default();
let options = ShapeCastOptions {
max_time_of_impact: time,
target_distance: 0.0,
stop_at_penetration: false,
compute_impact_geometry_on_penetration: false,
};
let shape_pos = Isometry { translation: Translation{vector: vector![pos.m[0],pos.m[1],pos.m[2]]}, rotation: Rotation::from_euler_angles(rot.m[0], rot.m[1], rot.m[2])};
let shape_vel=vector![vel.m[0],vel.m[1],vel.m[2]];
if let Some((handle, hit)) = px.broad_phase.as_query_pipeline(
px.narrow_phase.query_dispatcher(),
&px.rigid_body_set,
&px.collider_set,
QueryFilter::default()
).cast_shape(
&shape_pos, &shape_vel, &c, options,
) {
res.time=hit.time_of_impact;
res.normal1.m[0] = hit.normal1.x;
res.normal1.m[1] = hit.normal1.y;
res.normal1.m[2] = hit.normal1.z;
res.hit = 1;
} else {
res.time = time;
}
res
}