Skip to content

Commit

Permalink
refactor: change conversion from threejs Vector3 to Rapier Vector for…
Browse files Browse the repository at this point in the history
… all joint hooks
  • Loading branch information
isaac-mason committed Feb 6, 2024
1 parent eb3229f commit f592050
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 64 deletions.
33 changes: 14 additions & 19 deletions packages/react-three-rapier/src/hooks/joints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
RopeImpulseJoint,
SphericalImpulseJoint,
SpringImpulseJoint,
JointType as ImpulseJointType,
Vector
} from "@dimforge/rapier3d-compat";
import { RefObject, useRef } from "react";
import {
Expand All @@ -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
Expand Down Expand Up @@ -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)
)
);
};
Expand All @@ -119,8 +114,8 @@ export const useSphericalJoint: UseImpulseJoint<
body1,
body2,
rapier.JointData.spherical(
vectorArrayToVector3(body1Anchor),
vectorArrayToVector3(body2Anchor)
vector3ToRapierVector(body1Anchor),
vector3ToRapierVector(body2Anchor)
)
);
};
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
26 changes: 13 additions & 13 deletions packages/react-three-rapier/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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]
];

Expand Down
54 changes: 22 additions & 32 deletions packages/react-three-rapier/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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));
};
Expand All @@ -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,
Expand Down Expand Up @@ -100,13 +100,3 @@ export function useConst<T>(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;
}
};

0 comments on commit f592050

Please sign in to comment.