diff --git a/packages/react-three-rapier/src/hooks/joints.ts b/packages/react-three-rapier/src/hooks/joints.ts index afa097e6..657502bb 100644 --- a/packages/react-three-rapier/src/hooks/joints.ts +++ b/packages/react-three-rapier/src/hooks/joints.ts @@ -6,8 +6,6 @@ import { RopeImpulseJoint, SphericalImpulseJoint, SpringImpulseJoint, - JointType as ImpulseJointType, - Vector } from "@dimforge/rapier3d-compat"; import { RefObject, useRef } from "react"; import { @@ -20,17 +18,14 @@ import { SpringJointParams, UseImpulseJoint, useRapier, - vec3 } from ".."; import { - tupleToObject, - vectorArrayToVector3, - vector3ToRapierVector + vector3ToRapierVector, + quaternionToRapierQuaternion } from "../utils/utils"; import type Rapier from "@dimforge/rapier3d-compat"; import { useImperativeInstance } from "./use-imperative-instance"; -import { Vector3 } from "@react-three/fiber"; /** * @internal @@ -93,10 +88,10 @@ export const useFixedJoint: UseImpulseJoint< body1, body2, rapier.JointData.fixed( - vectorArrayToVector3(body1Anchor), - tupleToObject(body1LocalFrame, ["x", "y", "z", "w"] as const), - vectorArrayToVector3(body2Anchor), - tupleToObject(body2LocalFrame, ["x", "y", "z", "w"] as const) + vector3ToRapierVector(body1Anchor), + quaternionToRapierQuaternion(body1LocalFrame), + vector3ToRapierVector(body2Anchor), + quaternionToRapierQuaternion(body2LocalFrame) ) ); }; @@ -119,8 +114,8 @@ export const useSphericalJoint: UseImpulseJoint< body1, body2, rapier.JointData.spherical( - vectorArrayToVector3(body1Anchor), - vectorArrayToVector3(body2Anchor) + vector3ToRapierVector(body1Anchor), + vector3ToRapierVector(body2Anchor) ) ); }; @@ -139,9 +134,9 @@ export const useRevoluteJoint: UseImpulseJoint< const { rapier } = useRapier(); const params = rapier.JointData.revolute( - vectorArrayToVector3(body1Anchor), - vectorArrayToVector3(body2Anchor), - vectorArrayToVector3(axis) + vector3ToRapierVector(body1Anchor), + vector3ToRapierVector(body2Anchor), + vector3ToRapierVector(axis) ); if (limits) { @@ -166,9 +161,9 @@ export const usePrismaticJoint: UseImpulseJoint< const { rapier } = useRapier(); const params = rapier.JointData.prismatic( - vectorArrayToVector3(body1Anchor), - vectorArrayToVector3(body2Anchor), - vectorArrayToVector3(axis) + vector3ToRapierVector(body1Anchor), + vector3ToRapierVector(body2Anchor), + vector3ToRapierVector(axis) ); if (limits) { diff --git a/packages/react-three-rapier/src/types.ts b/packages/react-three-rapier/src/types.ts index debf6118..7581dce3 100644 --- a/packages/react-three-rapier/src/types.ts +++ b/packages/react-three-rapier/src/types.ts @@ -9,7 +9,7 @@ import { TempContactManifold } from "@dimforge/rapier3d-compat"; import { Rotation, Vector } from "@dimforge/rapier3d-compat/math"; -import { Object3DProps, Vector3 } from "@react-three/fiber"; +import { Object3DProps, Vector3, Quaternion } from "@react-three/fiber"; import { Object3D } from "three"; import { ColliderProps } from "."; import { RigidBodyState } from "./components/Physics"; @@ -461,28 +461,28 @@ export interface RigidBodyOptions extends ColliderProps { // Joints export type SphericalJointParams = [ - body1Anchor: Vector3Tuple, - body2Anchor: Vector3Tuple + body1Anchor: Vector3, + body2Anchor: Vector3 ]; export type FixedJointParams = [ - body1Anchor: Vector3Tuple, - body1LocalFrame: Vector4Tuple, - body2Anchor: Vector3Tuple, - body2LocalFrame: Vector4Tuple + body1Anchor: Vector3, + body1LocalFrame: Quaternion, + body2Anchor: Vector3, + body2LocalFrame: Quaternion ]; export type PrismaticJointParams = [ - body1Anchor: Vector3Tuple, - body2Anchor: Vector3Tuple, - axis: Vector3Tuple, + body1Anchor: Vector3, + body2Anchor: Vector3, + axis: Vector3, limits?: [min: number, max: number] ]; export type RevoluteJointParams = [ - body1Anchor: Vector3Tuple, - body2Anchor: Vector3Tuple, - axis: Vector3Tuple, + body1Anchor: Vector3, + body2Anchor: Vector3, + axis: Vector3, limits?: [min: number, max: number] ]; diff --git a/packages/react-three-rapier/src/utils/utils.ts b/packages/react-three-rapier/src/utils/utils.ts index c7166a47..3ffce46e 100644 --- a/packages/react-three-rapier/src/utils/utils.ts +++ b/packages/react-three-rapier/src/utils/utils.ts @@ -1,37 +1,18 @@ import { useRef } from "react"; import { Quaternion as RapierQuaternion, - Vector3 as RapierVector3 + Vector3 as RapierVector3, } from "@dimforge/rapier3d-compat"; - -import { Euler, Quaternion, Shape, Vector3 } from "three"; +import { Euler, Quaternion, Vector3 } from "three"; import { _euler, _quaternion, _vector3 } from "./shared-objects"; import { RigidBodyTypeString, Vector3Tuple } from "../types"; -import { Vector3 as Vector3ish } from "@react-three/fiber"; +import { Vector3 as Vector3Like, Quaternion as QuaternionLike } from "@react-three/fiber"; export const vectorArrayToVector3 = (arr: Vector3Tuple) => { const [x, y, z] = arr; return new Vector3(x, y, z); }; -export const tupleToObject = < - T extends readonly any[], - K extends readonly string[] ->( - tuple: T, - keys: K -) => { - return keys.reduce( - (obj, key, i) => { - obj[key as K[number]] = tuple[i]; - return obj; - }, - {} as { - [Key in K[number]]: T[number]; - } - ); -}; - export const vector3ToQuaternion = (v: Vector3) => { return _quaternion.setFromEuler(_euler.setFromVector3(v)); }; @@ -46,6 +27,25 @@ export const rapierQuaternionToQuaternion = ({ w }: RapierQuaternion) => _quaternion.set(x, y, z, w); +export const vector3ToRapierVector = (v: Vector3Like) => { + if (Array.isArray(v)) { + return new RapierVector3(v[0], v[1], v[2]); + } else if (typeof v === "number") { + return new RapierVector3(v, v, v); + } else { + const threeVector3 = v as THREE.Vector3; + return new RapierVector3(threeVector3.x, threeVector3.y, threeVector3.z); + } +}; + +export const quaternionToRapierQuaternion = (v: QuaternionLike) => { + if (Array.isArray(v)) { + return new RapierQuaternion(v[0], v[1], v[2], v[3]); + } else { + return new RapierQuaternion(v.x, v.y, v.z, v.w); + } +}; + const rigidBodyTypeMap = { fixed: 1, dynamic: 0, @@ -100,13 +100,3 @@ export function useConst(initialValue: T | (() => T)): T { } return ref.current.value; } - -export const vector3ToRapierVector = (v: Vector3ish) => { - if (Array.isArray(v)) { - return new RapierVector3(v[0], v[1], v[2]); - } else if (typeof v === "number") { - return new RapierVector3(v, v, v); - } else { - return v as THREE.Vector3; - } -};