#![allow(dead_code)]
use na::{Isometry3, Matrix4, Point3, Quaternion, Translation3, Unit, UnitQuaternion, Vector3};
use physx::articulation_joint_base::JointMap;
use physx::cooking::{
ConvexMeshCookingResult, PxConvexMeshDesc, PxCooking, PxCookingParams, PxHeightFieldDesc,
PxTriangleMeshDesc, TriangleMeshCookingResult,
};
use physx::foundation::DefaultAllocator;
use physx::prelude::*;
use physx::scene::FrictionType;
use physx::traits::Class;
use physx_sys::{
FilterShaderCallbackInfo, PxArticulationLink_getInboundJoint, PxBitAndByte, PxConvexFlags,
PxConvexMeshGeometryFlags, PxHeightFieldSample, PxMeshGeometryFlags, PxMeshScale_new,
PxRigidActor,
};
use rapier::counters::Counters;
use rapier::dynamics::{
ImpulseJointSet, IntegrationParameters, MultibodyJointSet, RigidBodyHandle, RigidBodySet,
};
use rapier::geometry::{Collider, ColliderSet};
use rapier::prelude::JointAxesMask;
use std::collections::HashMap;
trait IntoNa {
type Output;
fn into_na(self) -> Self::Output;
}
impl IntoNa for glam::Mat4 {
type Output = Matrix4<f32>;
fn into_na(self) -> Self::Output {
self.to_cols_array_2d().into()
}
}
impl IntoNa for PxVec3 {
type Output = Vector3<f32>;
fn into_na(self) -> Self::Output {
Vector3::new(self.x(), self.y(), self.z())
}
}
impl IntoNa for PxQuat {
type Output = Quaternion<f32>;
fn into_na(self) -> Self::Output {
Quaternion::new(self.w(), self.x(), self.y(), self.z())
}
}
impl IntoNa for PxTransform {
type Output = Isometry3<f32>;
fn into_na(self) -> Self::Output {
let tra = self.translation().into_na();
let quat = self.rotation().into_na();
let unit_quat = Unit::new_unchecked(quat);
Isometry3::from_parts(tra.into(), unit_quat)
}
}
trait IntoPhysx {
type Output;
fn into_physx(self) -> Self::Output;
}
impl IntoPhysx for Vector3<f32> {
type Output = PxVec3;
fn into_physx(self) -> Self::Output {
PxVec3::new(self.x, self.y, self.z)
}
}
impl IntoPhysx for Point3<f32> {
type Output = PxVec3;
fn into_physx(self) -> Self::Output {
PxVec3::new(self.x, self.y, self.z)
}
}
impl IntoPhysx for UnitQuaternion<f32> {
type Output = PxQuat;
fn into_physx(self) -> Self::Output {
PxQuat::new(self.i, self.j, self.k, self.w)
}
}
impl IntoPhysx for Isometry3<f32> {
type Output = PxTransform;
fn into_physx(self) -> Self::Output {
PxTransform::from_translation_rotation(
&self.translation.vector.into_physx(),
&self.rotation.into_physx(),
)
}
}
trait IntoGlam {
type Output;
fn into_glam(self) -> Self::Output;
}
impl IntoGlam for Vector3<f32> {
type Output = glam::Vec3;
fn into_glam(self) -> Self::Output {
glam::vec3(self.x, self.y, self.z)
}
}
impl IntoGlam for Point3<f32> {
type Output = glam::Vec3;
fn into_glam(self) -> Self::Output {
glam::vec3(self.x, self.y, self.z)
}
}
impl IntoGlam for Matrix4<f32> {
type Output = glam::Mat4;
fn into_glam(self) -> Self::Output {
glam::Mat4::from_cols_array_2d(&self.into())
}
}
impl IntoGlam for Isometry3<f32> {
type Output = glam::Mat4;
fn into_glam(self) -> Self::Output {
glam::Mat4::from_cols_array_2d(&self.to_homogeneous().into())
}
}
thread_local! {
pub static FOUNDATION: std::cell::RefCell<PxPhysicsFoundation> = std::cell::RefCell::new(PhysicsFoundation::default());
}
pub struct PhysxWorld {
// physics: Physics,
// cooking: Cooking,
materials: Vec<Owner<PxMaterial>>,
shapes: Vec<Owner<PxShape>>,
scene: Option<Owner<PxScene>>,
}
impl Drop for PhysxWorld {
fn drop(&mut self) {
let scene = self.scene.take();
// FIXME: we get a segfault if we don't forget the scene.
std::mem::forget(scene);
}
}
impl PhysxWorld {
pub fn from_rapier(
gravity: Vector3<f32>,