different stuff in physics

This commit is contained in:
2026-03-18 18:41:28 +02:00
parent b56d85f95d
commit 79ceac1005
8 changed files with 223 additions and 18 deletions

View File

@@ -80,6 +80,18 @@ public:
}
virtual CastResult_t RayCast( Vector vBegin, Vector vEnd ) override
{
return CRapierPhysicsWorld_RayCast(m_pWorld, vBegin, vEnd);
}
virtual CastResult_t ShapeCast( HShape hShape, Quat vOrientation, Vector vBegin, Vector vEnd ) override
{
return CRapierPhysicsWorld_ShapeCast(m_pWorld, (RapierShape_t*)hShape, vOrientation, vBegin, vEnd );
}
RapierWorld_t *m_pWorld = NULL;
};

View File

@@ -33,6 +33,14 @@ typedef struct Quat {
float w;
} Quat;
typedef struct CastResult_t {
bool m_bIsHit;
const struct RapierCollider_t *m_hCollider;
struct Vector m_vCollisionPoint;
float m_fTime;
float m_fDistance;
} CastResult_t;
typedef struct BallShape_t {
float m_fRadius;
} BallShape_t;
@@ -77,6 +85,16 @@ struct RapierPhysicsBody_t *CRapierPhysicsWorld_CreateRigidBody(struct RapierWor
void CRapierPhysicsWorld_Frame(struct RapierWorld_t *this_, float fDelta);
struct CastResult_t CRapierPhysicsWorld_RayCast(struct RapierWorld_t *this_,
struct Vector vBegin,
struct Vector vEnd);
struct CastResult_t CRapierPhysicsWorld_ShapeCast(struct RapierWorld_t *this_,
struct RapierShape_t *pShape,
struct Quat vOrientation,
struct Vector vBegin,
struct Vector vEnd);
struct RapierShape_t *CRapierPhysics_CreateBall(struct RapierPhysics_t *this_,
struct BallShape_t ball);

View File

@@ -16,10 +16,29 @@ macro_rules! V_malloc {
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 parry3d::{glamx::{Pose3A}, query::ShapeCastOptions};
use rapier3d::{geometry::Ball, na::{UnitQuaternion, Vector4, coordinates::XYZ}};
use rapier3d::prelude::*;
use libc::{malloc, free};
#[repr(C)]
#[derive(Default, Debug)]
pub struct Vector {
x: f32,
y: f32,
z: f32,
}
#[repr(C)]
#[derive(Default, Debug)]
pub struct Quat {
x: f32,
y: f32,
z: f32,
w: f32,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct BallShape_t
@@ -67,6 +86,17 @@ pub enum EPhysicsBodyType
#[repr(C)]
#[derive(Debug, Default)]
pub struct CastResult_t
{
m_bIsHit: bool,
m_hCollider: *const RapierCollider_t,
m_vCollisionPoint: Vector,
m_fTime: f32,
m_fDistance: f32,
}
#[derive(Clone)]
#[derive(Debug)]
pub struct RapierCollider_t
@@ -124,12 +154,6 @@ pub unsafe extern "C" fn CRapierPhysicsBody_SetRotation( this: *mut RapierPhysic
}
#[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
@@ -140,14 +164,6 @@ pub unsafe extern "C" fn CRapierPhysicsBody_GetPosition( this: *mut RapierPhysic
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
{
@@ -226,6 +242,85 @@ pub unsafe extern "C" fn CRapierPhysicsWorld_CreateRigidBody(
pBody
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsWorld_RayCast( this: *mut RapierWorld_t, vBegin: Vector, vEnd: Vector ) -> CastResult_t
{
let mut cast = CastResult_t::default();
let vDir = Vector{ x: vEnd.x-vBegin.x, y:vEnd.y-vBegin.y, z:vEnd.z-vBegin.z};
let fMaxDistance = f32::sqrt(vDir.x*vDir.x+vDir.y*vDir.y+vDir.z*vDir.z);
let vNormalizedDir = Vector{ x: vDir.x/fMaxDistance, y:vDir.y/fMaxDistance, z:vDir.z/fMaxDistance};
let ray = Ray::new(
Vec3 { x: vBegin.x, y: vBegin.y, z: vBegin.z },
Vec3 { x: vNormalizedDir.x, y: vNormalizedDir.y, z: vNormalizedDir.z });
let queryPipeline = (*this).m_broadPhase.as_query_pipeline(
(*this).m_narrowPhase.query_dispatcher(),
&(*this).m_rigidBodies,
&(*this).m_colliders,
QueryFilter::default(),
);
if let Some((handle, intersection)) = queryPipeline.cast_ray_and_get_normal(&ray, fMaxDistance, true)
{
cast.m_bIsHit = true;
cast.m_fDistance = intersection.time_of_impact;
cast.m_fTime = intersection.time_of_impact/fMaxDistance;
cast.m_vCollisionPoint = Vector{
x: vBegin.x + vNormalizedDir.x * intersection.time_of_impact,
y: vBegin.y + vNormalizedDir.y * intersection.time_of_impact,
z: vBegin.z + vNormalizedDir.z * intersection.time_of_impact,
};
}
cast
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysicsWorld_ShapeCast( this: *mut RapierWorld_t, pShape: *mut RapierShape_t, vOrientation: Quat, vBegin: Vector, vEnd: Vector ) -> CastResult_t
{
let mut cast = CastResult_t::default();
let vDir = Vector{ x: vEnd.x-vBegin.x, y:vEnd.y-vBegin.y, z:vEnd.z-vBegin.z};
let fMaxDistance = f32::sqrt(vDir.x*vDir.x+vDir.y*vDir.y+vDir.z*vDir.z);
let vNormalizedDir = Vector{ x: vDir.x/fMaxDistance, y:vDir.y/fMaxDistance, z:vDir.z/fMaxDistance};
let queryPipeline = (*this).m_broadPhase.as_query_pipeline(
(*this).m_narrowPhase.query_dispatcher(),
&(*this).m_rigidBodies,
&(*this).m_colliders,
QueryFilter::default(),
);
let vRustDir = Vector{ x: vNormalizedDir.x, y: vNormalizedDir.y, z: vNormalizedDir.z };
let mut castOptions = ShapeCastOptions::default();
castOptions.stop_at_penetration = true;
castOptions.max_time_of_impact = fMaxDistance;
let shapeStats = Pose3::from_parts(Vec3::from_array([vBegin.x, vBegin.y, vBegin.z]),glamx::Quat::from_xyzw(vOrientation.x, vOrientation.y, vOrientation.z, vOrientation.w));
let shape: &dyn Shape;
match (*pShape).m_sharedShape.as_typed_shape()
{
TypedShape::Ball(s) => { shape = s; }
TypedShape::Cuboid(s) => { shape = s; }
TypedShape::TriMesh(s) => { shape = s; }
default => {
return cast;
}
}
if let Some((handle, intersection)) = queryPipeline.cast_shape(
&shapeStats,
Vec3 { x: vNormalizedDir.x, y: vNormalizedDir.y, z: vNormalizedDir.z },
shape,
castOptions)
{
cast.m_bIsHit = true;
cast.m_fDistance = intersection.time_of_impact;
cast.m_fTime = intersection.time_of_impact/fMaxDistance;
cast.m_vCollisionPoint = Vector{
x: vBegin.x + vNormalizedDir.x * intersection.time_of_impact,
y: vBegin.y + vNormalizedDir.y * intersection.time_of_impact,
z: vBegin.z + vNormalizedDir.z * intersection.time_of_impact,
};
}
cast
}
#[no_mangle]
pub unsafe extern "C" fn CRapierPhysics_New() -> *mut RapierPhysics_t
{