Skip to content

Commit

Permalink
feat: update @dimforge/rapier3d-compat to 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Jan 28, 2024
1 parent e2b361f commit 2ff8e9f
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 82 deletions.
10 changes: 10 additions & 0 deletions .changeset/cold-apples-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@react-three/rapier": minor
---

feat: update @dimforge/rapier3d-compat to 0.12.0

- Physics component
- World integration parameters have changed in the new rapier version. As the new version of rapier has changes to it's constraint solver, there aren't direct alternatives for all old parameters.
- Add 'useSpringJoint'
- Add 'useRopeJoint'
31 changes: 17 additions & 14 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Box, Environment, OrbitControls } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { Physics, RigidBody, useRapier } from "@react-three/rapier";
import { Physics, RigidBody } from "@react-three/rapier";
import { Perf } from "r3f-perf";
import {
createContext,
ReactNode,
StrictMode,
Suspense,
createContext,
useContext,
useState,
StrictMode,
useEffect
useState
} from "react";
import { NavLink, NavLinkProps, Route, Routes } from "react-router-dom";
import { AllCollidersExample } from "./examples/all-colliders/AllCollidersExample";
Expand All @@ -24,21 +23,23 @@ import { ComponentsExample } from "./examples/components/ComponentsExample";
import { ContactForceEventsExample } from "./examples/contact-force-events/ContactForceEventsExample";
import { CradleExample } from "./examples/cradle/CradleExample";
import { Damping } from "./examples/damping/DampingExample";
import { DynamicTypeChangeExample } from "./examples/dynamic-type-change/DynamicTypeChangeExample";
import { ImmutablePropsExample } from "./examples/immutable-props/ImmutablePropsExample";
import { InstancedMeshes } from "./examples/instanced-meshes/InstancedMeshesExample";
import { InstancedMeshesCompound } from "./examples/instances-meshes-compound/InstancedMeshesCompoundExample";
import { Joints } from "./examples/joints/JointsExample";
import { Kinematics } from "./examples/kinematics/KinematicsExample";
import { LockedTransformsExample } from "./examples/locked-transforms/LockedTransformsExample";
import { ManualStepExample } from "./examples/manual-step/ManualStepExamples";
import { MeshColliderTest } from "./examples/mesh-collider-test/MeshColliderExample";
import { SensorsExample } from "./examples/sensors/SensorsExample";
import Shapes from "./examples/plinko/ShapesExample";
import { Transforms } from "./examples/transforms/TransformsExample";
import { LockedTransformsExample } from "./examples/locked-transforms/LockedTransformsExample";
import { PerformanceExample } from "./examples/performance/PeformanceExample";
import { DynamicTypeChangeExample } from "./examples/dynamic-type-change/DynamicTypeChangeExample";
import Shapes from "./examples/plinko/ShapesExample";
import { RopeJointExample } from "./examples/rope-joint/RopeJointExample";
import { SensorsExample } from "./examples/sensors/SensorsExample";
import { SnapshotExample } from "./examples/snapshot/SnapshotExample";
import { SpringExample } from "./examples/spring/SpringExample";
import { StutteringExample } from "./examples/stuttering/StutteringExample";
import { ImmutablePropsExample } from "./examples/immutable-props/ImmutablePropsExample";
import { SnapshotExample } from './examples/snapshot/SnapshotExample';
import { Transforms } from "./examples/transforms/TransformsExample";

const demoContext = createContext<{
setDebug?(f: boolean): void;
Expand Down Expand Up @@ -116,7 +117,9 @@ const routes: Record<string, ReactNode> = {
"dynamic-type-changes": <DynamicTypeChangeExample />,
stuttering: <StutteringExample />,
"immutable-props": <ImmutablePropsExample />,
snapshot: <SnapshotExample />
snapshot: <SnapshotExample />,
spring: <SpringExample />,
ropeJoint: <RopeJointExample />
};

export const App = () => {
Expand Down Expand Up @@ -149,7 +152,7 @@ export const App = () => {
interpolate={interpolate}
debug={debug}
timeStep={1 / 60}
// erp={0.2}
// erp={0.2}
>
<directionalLight
castShadow
Expand Down
9 changes: 1 addition & 8 deletions demo/src/examples/cradle/CradleExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
RapierRigidBody,
RigidBody,
RigidBodyOptions,
useRapier,
useSphericalJoint
} from "@react-three/rapier";
import { useEffect, useRef } from "react";
import { useRef } from "react";
import { Demo } from "../../App";

const Rod = (props: RigidBodyOptions) => {
Expand All @@ -20,12 +19,6 @@ const Rod = (props: RigidBodyOptions) => {
[0, 0, 0]
]);

const { world } = useRapier();

useEffect(() => {
world.maxStabilizationIterations = 10;
}, []);

return (
<group>
<RigidBody ref={anchor} {...props} />
Expand Down
41 changes: 41 additions & 0 deletions demo/src/examples/rope-joint/RopeJointExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Sphere } from "@react-three/drei";
import {
BallCollider,
RapierRigidBody,
RigidBody,
RigidBodyOptions,
useRopeJoint
} from "@react-three/rapier";
import { useRef } from "react";
import { Demo } from "../../App";

// todo: flesh out this demo

const RopeJoint = (props: RigidBodyOptions) => {
const anchor = useRef<RapierRigidBody>(null);
const ball = useRef<RapierRigidBody>(null);

useRopeJoint(anchor, ball, [[0, 0, 0], [0, 0, 0], 1]);

return (
<group>
<RigidBody ref={anchor} {...props} />

<RigidBody ref={ball} {...props} colliders={false}>
<Sphere scale={0.2} receiveShadow castShadow>
<meshStandardMaterial metalness={1} roughness={0.3} />
</Sphere>

<BallCollider args={[0.2]} restitution={1.2} />
</RigidBody>
</group>
);
};

export const RopeJointExample: Demo = () => {
return (
<group rotation={[1, 0, 0]} scale={3}>
<RopeJoint position={[0, 0, 0]} />
</group>
);
};
39 changes: 39 additions & 0 deletions demo/src/examples/spring/SpringExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Sphere } from "@react-three/drei";
import {
BallCollider,
RapierRigidBody,
RigidBody,
RigidBodyOptions,
useSpringJoint
} from "@react-three/rapier";
import { useRef } from "react";
import { Demo } from "../../App";

const Spring = (props: RigidBodyOptions) => {
const anchor = useRef<RapierRigidBody>(null);
const ball = useRef<RapierRigidBody>(null);

useSpringJoint(anchor, ball, [[0, 0, 0], [0, 0, 0], 2, 20, 2]);

return (
<group>
<RigidBody ref={anchor} {...props} />

<RigidBody ref={ball} {...props} colliders={false}>
<Sphere scale={0.2} receiveShadow castShadow>
<meshStandardMaterial metalness={1} roughness={0.3} />
</Sphere>

<BallCollider args={[0.2]} restitution={1.2} />
</RigidBody>
</group>
);
};

export const SpringExample: Demo = () => {
return (
<group rotation={[1, 0, 0]} scale={3}>
<Spring position={[0, 0, 0]} />
</group>
);
};
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.11.2",
"@dimforge/rapier3d-compat": "0.12.0",
"three-stdlib": "2.23.9",
"use-asset": "1.0.4"
},
Expand Down
86 changes: 57 additions & 29 deletions packages/react-three-rapier/src/components/Physics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,32 +257,37 @@ export interface PhysicsProps {
gravity?: Vector3Tuple;

/**
* The maximum velocity iterations the velocity-based constraint solver can make to attempt
* to remove the energy introduced by constraint stabilization.
*
* @defaultValue 1
* Amount of penetration the engine wont attempt to correct
* @defaultValue 0.001
*/
maxStabilizationIterations?: number;
allowedLinearError?: number;

/**
* The maximum velocity iterations the velocity-based friction constraint solver can make.
*
* The greater this value is, the most realistic friction will be.
* The number of solver iterations run by the constraints solver for calculating forces.
* The greater this value is, the most rigid and realistic the physics simulation will be.
* However a greater number of iterations is more computationally intensive.
*
* @defaultValue 8
* @defaultValue 4
*/
maxVelocityFrictionIterations?: number;
numSolverIterations?: number;

/**
* The maximum velocity iterations the velocity-based force constraint solver can make.
*
* The greater this value is, the most rigid and realistic the physics simulation will be.
* Number of addition friction resolution iteration run during the last solver sub-step.
* The greater this value is, the most realistic friction will be.
* However a greater number of iterations is more computationally intensive.
*
* @defaultValue 4
*/
maxVelocityIterations?: number;
numAdditionalFrictionIterations?: number;

/**
* Number of internal Project Gauss Seidel (PGS) iterations run at each solver iteration.
* Increasing this parameter will improve stability of the simulation. It will have a lesser effect than
* increasing `numSolverIterations` but is also less computationally expensive.
*
* @defaultValue 1
*/
numInternalPgsIterations?: number;

/**
* The maximal distance separating two objects that will generate predictive contacts
Expand All @@ -293,7 +298,22 @@ export interface PhysicsProps {
predictionDistance?: number;

/**
* The Error Reduction Parameter in between 0 and 1, is the proportion of the positional error to be corrected at each time step
* Minimum number of dynamic bodies in each active island
*
* @defaultValue 128
*/
minIslandSize?: number

/**
* Maximum number of substeps performed by the solver
*
* @defaultValue 1
*/
maxCcdSubsteps?: number

/**
* The Error Reduction Parameter in between 0 and 1, is the proportion of the positional error to be corrected at each time step.
*
* @defaultValue 0.8
*/
erp?: number;
Expand Down Expand Up @@ -380,11 +400,14 @@ export const Physics: FC<PhysicsProps> = (props) => {
debug = false,

gravity = [0, -9.81, 0],
maxStabilizationIterations = 1,
maxVelocityFrictionIterations = 8,
maxVelocityIterations = 4,
predictionDistance = 0.002,
erp = 0.8
allowedLinearError = 0.001,
predictionDistance = 4,
numSolverIterations = 4,
numAdditionalFrictionIterations = 4,
numInternalPgsIterations = 1,
minIslandSize = 128,
maxCcdSubsteps = 1,
erp = 0.2
} = props;
const rapier = useAsset(importRapier);
const { invalidate } = useThree();
Expand Down Expand Up @@ -421,20 +444,25 @@ export const Physics: FC<PhysicsProps> = (props) => {
// Update mutable props
useEffect(() => {
worldProxy.gravity = vectorArrayToVector3(gravity);
worldProxy.integrationParameters.maxStabilizationIterations =
maxStabilizationIterations;
worldProxy.integrationParameters.maxVelocityFrictionIterations =
maxVelocityFrictionIterations;
worldProxy.integrationParameters.maxVelocityIterations =
maxVelocityIterations;

worldProxy.numSolverIterations = numSolverIterations
worldProxy.numAdditionalFrictionIterations = numAdditionalFrictionIterations
worldProxy.numInternalPgsIterations = numInternalPgsIterations

worldProxy.integrationParameters.allowedLinearError = allowedLinearError
worldProxy.integrationParameters.minIslandSize = minIslandSize
worldProxy.integrationParameters.maxCcdSubsteps = maxCcdSubsteps
worldProxy.integrationParameters.predictionDistance = predictionDistance;
worldProxy.integrationParameters.erp = erp;
}, [
worldProxy,
...gravity,
maxStabilizationIterations,
maxVelocityIterations,
maxVelocityFrictionIterations,
numSolverIterations,
numAdditionalFrictionIterations,
numInternalPgsIterations,
allowedLinearError,
minIslandSize,
maxCcdSubsteps,
predictionDistance,
erp
]);
Expand Down
Loading

0 comments on commit 2ff8e9f

Please sign in to comment.