Skip to content

Commit

Permalink
feat: update to @dimforge/rapier3d-compat 0.13.1 (pmndrs#677)
Browse files Browse the repository at this point in the history
* feat: bump @dimforge/rapier3d-compat to 0.13.1, expose new functionality

* chore: prettier format

* chore: add contact skin demo

* fix: add softCcdPrediction to cleanRigidBodyPropsForCollider

* chore: update changeset

* fix: prettier format

* chore: change @react-three/rapier-addons to depend on @react-three/rapier ^1.3.1
  • Loading branch information
isaac-mason authored Jun 14, 2024
1 parent 14dbc0e commit a155277
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 23 deletions.
10 changes: 10 additions & 0 deletions .changeset/little-doors-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@react-three/rapier": minor
---

feat: bump @dimforge/rapier3d-compat from 0.12.0 to 0.13.1

- Added World prop `lengthUnit`
- Renamed World props `allowedLinearError` and `predictionDistance` to `normalizedAllowedLinearError` and `normalizedPredictionDistance`, matching upstream changes
- Added `softCcdPrediction` RigidBody prop
- Added `contactSkin` collider prop
4 changes: 3 additions & 1 deletion demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Colliders } from "./examples/colliders/CollidersExample";
import { CollisionEventsExample } from "./examples/collision-events/CollisionEventsExample";
import { ComponentsExample } from "./examples/components/ComponentsExample";
import { ContactForceEventsExample } from "./examples/contact-force-events/ContactForceEventsExample";
import { ContactSkinExample } from "./examples/contact-skin/ContactSkinExample";
import { CradleExample } from "./examples/cradle/CradleExample";
import { Damping } from "./examples/damping/DampingExample";
import { DynamicTypeChangeExample } from "./examples/dynamic-type-change/DynamicTypeChangeExample";
Expand Down Expand Up @@ -119,7 +120,8 @@ const routes: Record<string, ReactNode> = {
"immutable-props": <ImmutablePropsExample />,
snapshot: <SnapshotExample />,
spring: <SpringExample />,
"rope-joint": <RopeJointExample />
"rope-joint": <RopeJointExample />,
"contact-skin": <ContactSkinExample />
};

export const App = () => {
Expand Down
34 changes: 34 additions & 0 deletions demo/src/examples/contact-skin/ContactSkinExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Box, Sphere } from "@react-three/drei";
import { RapierRigidBody, RigidBody } from "@react-three/rapier";
import { useRef } from "react";

const Ball = () => {
const rb = useRef<RapierRigidBody>(null);

return (
<RigidBody ref={rb} colliders="ball" position={[0, 1, 0]} contactSkin={0.5}>
<Sphere castShadow>
<meshStandardMaterial color="orange" />
</Sphere>
</RigidBody>
);
};

const Floor = () => {
return (
<RigidBody type="fixed" colliders="cuboid" position={[0, -2, 0]}>
<Box args={[20, 1, 20]} receiveShadow>
<meshStandardMaterial color="white" />
</Box>
</RigidBody>
);
};

export const ContactSkinExample = () => {
return (
<group>
<Ball />
<Floor />
</group>
);
};
4 changes: 2 additions & 2 deletions packages/react-three-rapier-addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"sideEffects": false,
"peerDependencies": {
"@react-three/fiber": "*",
"@react-three/rapier": "1.3.1",
"@react-three/rapier": "^1.3.1",
"react": ">=18.0.0",
"three": "*"
},
Expand All @@ -16,7 +16,7 @@
},
"devDependencies": {
"@react-three/fiber": "8.9.1",
"@react-three/rapier": "1.3.1",
"@react-three/rapier": "^1.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"three": "0.146.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-three-rapier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"three": ">=0.139.0"
},
"dependencies": {
"@dimforge/rapier3d-compat": "0.12.0",
"@dimforge/rapier3d-compat": "0.13.1",
"suspend-react": "^0.1.3",
"three-stdlib": "2.23.9"
},
Expand Down
23 changes: 19 additions & 4 deletions packages/react-three-rapier/src/components/Physics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ export interface PhysicsProps {
*/
erp?: number;

/**
* The approximate size of most dynamic objects in the scene.
*
* This value is used internally to estimate some length-based tolerance.
* This value can be understood as the number of units-per-meter in your physical world compared to a human-sized world in meter.
*
* @defaultValue 1
*/
lengthUnit?: number;

/**
* Set the base automatic colliders for this physics world
* All Meshes inside RigidBodies will generate a collider
Expand Down Expand Up @@ -407,7 +417,8 @@ export const Physics: FC<PhysicsProps> = (props) => {
numInternalPgsIterations = 1,
minIslandSize = 128,
maxCcdSubsteps = 1,
erp = 0.8
erp = 0.8,
lengthUnit = 1
} = props;
const rapier = suspend(importRapier, ["@react-thee/rapier", importRapier]);
const { invalidate } = useThree();
Expand Down Expand Up @@ -451,11 +462,14 @@ export const Physics: FC<PhysicsProps> = (props) => {
worldProxy.integrationParameters.numInternalPgsIterations =
numInternalPgsIterations;

worldProxy.integrationParameters.allowedLinearError = allowedLinearError;
worldProxy.integrationParameters.normalizedAllowedLinearError =
allowedLinearError;
worldProxy.integrationParameters.minIslandSize = minIslandSize;
worldProxy.integrationParameters.maxCcdSubsteps = maxCcdSubsteps;
worldProxy.integrationParameters.predictionDistance = predictionDistance;
worldProxy.integrationParameters.normalizedPredictionDistance =
predictionDistance;
worldProxy.integrationParameters.erp = erp;
worldProxy.lengthUnit = lengthUnit;
}, [
worldProxy,
...gravity,
Expand All @@ -466,7 +480,8 @@ export const Physics: FC<PhysicsProps> = (props) => {
minIslandSize,
maxCcdSubsteps,
predictionDistance,
erp
erp,
lengthUnit
]);

const getSourceFromColliderHandle = useCallback((handle: ColliderHandle) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-three-rapier/src/hooks/joints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RevoluteImpulseJoint,
RopeImpulseJoint,
SphericalImpulseJoint,
SpringImpulseJoint,
SpringImpulseJoint
} from "@dimforge/rapier3d-compat";
import { RefObject, useRef } from "react";
import {
Expand All @@ -17,7 +17,7 @@ import {
SphericalJointParams,
SpringJointParams,
UseImpulseJoint,
useRapier,
useRapier
} from "..";
import {
vector3ToRapierVector,
Expand Down
33 changes: 29 additions & 4 deletions packages/react-three-rapier/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ export interface ColliderOptions<ColliderArgs extends Array<unknown>> {
angularInertiaLocalFrame: Rotation;
};

/**
* The contact skin of the collider.
*
* The contact skin acts as if the collider was enlarged with a skin of width contactSkin around it, keeping objects further apart when colliding.
*
* A non-zero contact skin can increase performance, and in some cases, stability.
* However it creates a small gap between colliding object (equal to the sum of their skin).
* If the skin is sufficiently small, this might not be visually significant or can be hidden by the rendering assets.
*
* @defaultValue 0
*/
contactSkin?: number;

/**
* Sets whether or not this collider is a sensor.
*/
Expand Down Expand Up @@ -362,6 +375,21 @@ export interface RigidBodyOptions extends ColliderProps {
*/
ccd?: boolean;

/**
* The maximum prediction distance Soft Continuous Collision-Detection.
*
* When set to 0, soft-CCD is disabled.
*
* Soft-CCD helps prevent tunneling especially of slow-but-thin to moderately fast objects.
* The soft CCD prediction distance indicates how far in the object’s path the CCD algorithm is allowed to inspect.
* Large values can impact performance badly by increasing the work needed from the broad-phase.
*
* It is a generally cheaper variant of regular CCD since it relies on predictive constraints instead of shape-cast and substeps.
*
* @defaultValue 0
*/
softCcdPrediction?: number;

/**
* Initial position of the RigidBody
*/
Expand Down Expand Up @@ -460,10 +488,7 @@ export interface RigidBodyOptions extends ColliderProps {
}

// Joints
export type SphericalJointParams = [
body1Anchor: Vector3,
body2Anchor: Vector3
];
export type SphericalJointParams = [body1Anchor: Vector3, body2Anchor: Vector3];

export type FixedJointParams = [
body1Anchor: Vector3,
Expand Down
8 changes: 6 additions & 2 deletions packages/react-three-rapier/src/utils/singleton-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
*/
export const createSingletonProxy = <
SingletonClass extends object,
CreationFn extends () => SingletonClass = () => SingletonClass,
CreationFn extends () => SingletonClass = () => SingletonClass
>(
/**
* A function that returns a new instance of the class
*/
createInstance: CreationFn
): { proxy: SingletonClass; reset: () => void, set: (newInstance: SingletonClass) => void } => {
): {
proxy: SingletonClass;
reset: () => void;
set: (newInstance: SingletonClass) => void;
} => {
let instance: SingletonClass | undefined;

const handler: ProxyHandler<SingletonClass> = {
Expand Down
4 changes: 4 additions & 0 deletions packages/react-three-rapier/src/utils/utils-collider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ const mutableColliderOptions: MutableColliderOptions = {
restitutionCombineRule: (collider, value) => {
collider.setRestitutionCombineRule(value);
},
contactSkin: (collider, value: number) => {
collider.setContactSkin(value);
},
// To make sure the options all mutable options are listed
quaternion: () => {},
position: () => {},
Expand Down Expand Up @@ -476,6 +479,7 @@ export const cleanRigidBodyPropsForCollider = (props: RigidBodyProps = {}) => {
canSleep,
ccd,
gravityScale,
softCcdPrediction,
...rest
} = props;

Expand Down
3 changes: 3 additions & 0 deletions packages/react-three-rapier/src/utils/utils-rigidbody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export const mutableRigidBodyOptions: MutableRigidBodyOptions = {
ccd: (rb: RigidBody, value: boolean) => {
rb.enableCcd(value);
},
softCcdPrediction: (rb: RigidBody, value: number) => {
rb.setSoftCcdPrediction(value);
},
userData: (rb: RigidBody, value: { [key: string]: any }) => {
rb.userData = value;
},
Expand Down
7 changes: 5 additions & 2 deletions packages/react-three-rapier/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useRef } from "react";
import {
Quaternion as RapierQuaternion,
Vector3 as RapierVector3,
Vector3 as RapierVector3
} from "@dimforge/rapier3d-compat";
import { Euler, Quaternion, Vector3 } from "three";
import { _euler, _quaternion, _vector3 } from "./shared-objects";
import { RigidBodyTypeString, Vector3Tuple } from "../types";
import { Vector3 as Vector3Like, Quaternion as QuaternionLike } 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;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-three-rapier/tests/physics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe("physics", () => {
});

expect(vec3(rigidBody.current?.translation()).toArray()).to.deep.eq([
0.6666666269302368, 0.6649635434150696, 0.6666666269302368,
0.6666666269302368, 0.6649635434150696, 0.6666666269302368
]);

await pause(100);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1595,10 +1595,10 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@dimforge/rapier3d-compat@0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@dimforge/rapier3d-compat/-/rapier3d-compat-0.12.0.tgz#7b3365e1dfdc5cd957b45afe920b4ac06c7cd389"
integrity sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==
"@dimforge/rapier3d-compat@0.13.1":
version "0.13.1"
resolved "https://registry.yarnpkg.com/@dimforge/rapier3d-compat/-/rapier3d-compat-0.13.1.tgz#d9da32941de5fcfae55ffb6ac26c38539f57ddf4"
integrity sha512-SsQ/MTH4Vvs8f62g31iVV1VOmzwNwsJl91rVzafi5B2mCrI2OsQ3VyjJOb4/tvS5VNc6OLjIDgfClcOAkW+O2A==

"@esbuild/[email protected]":
version "0.17.14"
Expand Down

0 comments on commit a155277

Please sign in to comment.