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

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