Skip to content

Commit

Permalink
fix: additionalSolverIterations setter + change default Physics value…
Browse files Browse the repository at this point in the history
…s + set all integrationParams
  • Loading branch information
0xtito committed Feb 6, 2024
1 parent db0e02a commit 77b95b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
30 changes: 16 additions & 14 deletions packages/react-three-rapier/src/components/Physics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
RigidBodyAutoCollider,
Vector3Tuple
} from "../types";

import {
_matrix4,
_position,
Expand All @@ -39,7 +38,8 @@ import {
import {
rapierQuaternionToQuaternion,
useConst,
vectorArrayToVector3
vectorArrayToVector3,
vector3ToRapierVector
} from "../utils/utils";
import FrameStepper from "./FrameStepper";
import { Debug } from "./Debug";
Expand Down Expand Up @@ -284,7 +284,7 @@ export interface PhysicsProps {
* 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;
Expand All @@ -302,14 +302,14 @@ export interface PhysicsProps {
*
* @defaultValue 128
*/
minIslandSize?: number
minIslandSize?: number;

/**
* Maximum number of substeps performed by the solver
*
* @defaultValue 1
*/
maxCcdSubsteps?: number
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.
Expand Down Expand Up @@ -401,13 +401,13 @@ export const Physics: FC<PhysicsProps> = (props) => {

gravity = [0, -9.81, 0],
allowedLinearError = 0.001,
predictionDistance = 4,
predictionDistance = 0.002,
numSolverIterations = 4,
numAdditionalFrictionIterations = 4,
numInternalPgsIterations = 1,
minIslandSize = 128,
maxCcdSubsteps = 1,
erp = 0.2
erp = 0.8
} = props;
const rapier = useAsset(importRapier);
const { invalidate } = useThree();
Expand Down Expand Up @@ -443,15 +443,17 @@ export const Physics: FC<PhysicsProps> = (props) => {

// Update mutable props
useEffect(() => {
worldProxy.gravity = vectorArrayToVector3(gravity);
worldProxy.gravity = vector3ToRapierVector(gravity);

worldProxy.numSolverIterations = numSolverIterations
worldProxy.numAdditionalFrictionIterations = numAdditionalFrictionIterations
worldProxy.numInternalPgsIterations = numInternalPgsIterations
worldProxy.integrationParameters.numSolverIterations = numSolverIterations;
worldProxy.integrationParameters.numAdditionalFrictionIterations =
numAdditionalFrictionIterations;
worldProxy.integrationParameters.numInternalPgsIterations =
numInternalPgsIterations;

worldProxy.integrationParameters.allowedLinearError = allowedLinearError
worldProxy.integrationParameters.minIslandSize = minIslandSize
worldProxy.integrationParameters.maxCcdSubsteps = maxCcdSubsteps
worldProxy.integrationParameters.allowedLinearError = allowedLinearError;
worldProxy.integrationParameters.minIslandSize = minIslandSize;
worldProxy.integrationParameters.maxCcdSubsteps = maxCcdSubsteps;
worldProxy.integrationParameters.predictionDistance = predictionDistance;
worldProxy.integrationParameters.erp = erp;
}, [
Expand Down
5 changes: 4 additions & 1 deletion packages/react-three-rapier/src/utils/utils-rigidbody.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RigidBody, RigidBodyDesc } from "@dimforge/rapier3d-compat";
import React, { MutableRefObject, useEffect, useMemo } from "react";
import { useEffect, useMemo } from "react";
import { Matrix4, Object3D, Vector3 } from "three";
import { Boolean3Tuple, RigidBodyProps, Vector3Tuple } from "..";
import {
Expand Down Expand Up @@ -81,6 +81,9 @@ export const mutableRigidBodyOptions: MutableRigidBodyOptions = {
gravityScale: (rb: RigidBody, value: number) => {
rb.setGravityScale(value, true);
},
additionalSolverIterations(rb: RigidBody, value: number) {
rb.setAdditionalSolverIterations(value);
},
linearDamping: (rb: RigidBody, value: number) => {
rb.setLinearDamping(value);
},
Expand Down

0 comments on commit 77b95b6

Please sign in to comment.