#![warn(clippy::all)]
use glam::{Mat4, Quat, Vec3};
use physx_sys::{
PxIdentity, PxQuat, PxQuat_new_3, PxTransform, PxTransform_new_2, PxTransform_new_4, PxVec3,
PxVec3_new_3,
};
pub fn px_identity_tf() -> PxTransform {
unsafe { PxTransform_new_2(PxIdentity) }
}
struct PxQuatWrap(PxQuat);
impl From<Quat> for PxQuatWrap {
fn from(other: Quat) -> Self {
let (x, y, z, w) = other.into();
PxQuatWrap(unsafe { PxQuat_new_3(x, y, z, w) })
}
}
impl Into<PxQuat> for PxQuatWrap {
fn into(self) -> PxQuat {
self.0
}
}
pub fn gl_to_px_tf(trans: Mat4) -> PxTransform {
gl_to_px_tf_ref(&trans)
}
pub fn gl_to_px_tf_ref(trans: &Mat4) -> PxTransform {
let quat: PxQuatWrap = Quat::from_rotation_mat4(&trans).into();
let (x, y, z, _) = trans.w_axis().into();
unsafe { PxTransform_new_4(x, y, z, quat.0) }
}
pub fn px_to_gl_tf(trans: PxTransform) -> Mat4 {
let translation = px_to_gl_v3(trans.p);
let quaternion = px_to_gl_q(trans.q);
Mat4::from_rotation_translation(quaternion, translation)
}
#[inline(always)]
pub fn px_to_gl_q(quat: PxQuat) -> Quat {
Quat::from_xyzw(quat.x, quat.y, quat.z, quat.w)
}
#[inline(always)]
pub fn gl_to_px_q(quat: Quat) -> PxQuat {
PxQuatWrap::from(quat).into()
}
#[inline(always)]
pub fn px_to_gl_v3(pos: PxVec3) -> Vec3 {
Vec3::new(pos.x, pos.y, pos.z)
}
#[inline(always)]
pub fn gl_to_px_v3(pos: Vec3) -> PxVec3 {
let (x, y, z) = pos.into();
unsafe { PxVec3_new_3(x, y, z) }
}