fixes for physics
This commit is contained in:
@@ -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
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user