-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: continuation of update @dimforge/rapier3d-compat to 0.12.0 + op…
…timizations (#619)
- Loading branch information
Showing
16 changed files
with
579 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
"@react-three/rapier": minor | ||
--- | ||
|
||
feat: update @dimforge/rapier3d-compat to 0.12.0 (@0xtito, @isaac-mason) | ||
|
||
- Change Physics component props to match the new rapier version's integration parameter changes. | ||
- There aren't direct alternatives for all old parameters. See the Physics component docs for more information on the new parameters: https://pmndrs.github.io/react-three-rapier/interfaces/PhysicsProps.html | ||
- Add `additionalSolverIterations` prop to `RigidBodyOptions`. | ||
- See: https://pmndrs.github.io/react-three-rapier/interfaces/RigidBodyOptions.html#additionalSolverIterations | ||
- Add `useSpringJoint` | ||
- Add `useRopeJoint` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"mode": "pre", | ||
"tag": "canary", | ||
"initialVersions": { | ||
"demo": "0.0.0", | ||
"@react-three/rapier": "1.2.1", | ||
"@react-three/rapier-addons": "3.0.3" | ||
}, | ||
"changesets": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Sphere, Box } from "@react-three/drei"; | ||
import { | ||
BallCollider, | ||
RapierRigidBody, | ||
RigidBody, | ||
RigidBodyOptions, | ||
useRopeJoint | ||
} from "@react-three/rapier"; | ||
import { useRef } from "react"; | ||
import { Demo } from "../../App"; | ||
import { Vector3 } from "@react-three/fiber"; | ||
|
||
const WALL_COLORS = ["#50514F", "#CBD4C2", "#FFFCFF", "#247BA0", "#C3B299"]; | ||
|
||
interface BoxRigidBodyProps extends RigidBodyOptions { | ||
color: string; | ||
} | ||
|
||
interface BoxWallProps extends RigidBodyOptions { | ||
height: number; | ||
width: number; | ||
} | ||
|
||
interface RopeJointProps { | ||
anchorPosition: Vector3; | ||
ballPosition: Vector3; | ||
ropeLength: number; | ||
} | ||
|
||
const Floor = (props: RigidBodyOptions) => { | ||
return ( | ||
<RigidBody type="fixed" colliders="cuboid" position={[0, -1, 0]} {...props}> | ||
<Box args={[20, 1, 20]}> | ||
<meshStandardMaterial color="white" /> | ||
</Box> | ||
</RigidBody> | ||
); | ||
}; | ||
|
||
const BoxRigidBody = (props: BoxRigidBodyProps) => { | ||
return ( | ||
<RigidBody {...props}> | ||
<Box castShadow receiveShadow> | ||
<meshStandardMaterial color={props.color} /> | ||
</Box> | ||
</RigidBody> | ||
); | ||
}; | ||
|
||
const BoxWall = ({ height, width, ...props }: BoxWallProps) => { | ||
const wall = []; | ||
|
||
for (let i = 0; i < height; i++) { | ||
for (let j = 0; j < width; j++) { | ||
const position: [number, number, number] = [j, i, 0]; | ||
wall.push( | ||
<BoxRigidBody | ||
key={`${i}-${j}`} | ||
{...props} | ||
density={2} | ||
color={WALL_COLORS[i % 5]} | ||
position={position} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
return ( | ||
<group name="wall" rotation-y={-0.7853982} position={[-1.8, 0, -1.8]}> | ||
{wall.map((box, i) => box)} | ||
</group> | ||
); | ||
}; | ||
|
||
const RopeJoint = ({ | ||
anchorPosition, | ||
ballPosition, | ||
ropeLength | ||
}: RopeJointProps) => { | ||
const anchor = useRef<RapierRigidBody>(null); | ||
const ball = useRef<RapierRigidBody>(null); | ||
|
||
useRopeJoint(anchor, ball, [[0, 0, 0], [0, 0, 0], ropeLength]); | ||
|
||
return ( | ||
<group> | ||
{/* Anchor */} | ||
<RigidBody ref={anchor} position={anchorPosition} /> | ||
|
||
{/* Wrecking Ball */} | ||
<RigidBody | ||
position={ballPosition} | ||
ref={ball} | ||
restitution={1.2} | ||
density={30} | ||
colliders={false} | ||
> | ||
<Sphere args={[2]} receiveShadow castShadow> | ||
<meshStandardMaterial metalness={1} roughness={0.3} /> | ||
</Sphere> | ||
|
||
<BallCollider args={[2]} /> | ||
</RigidBody> | ||
</group> | ||
); | ||
}; | ||
|
||
export const RopeJointExample: Demo = () => { | ||
return ( | ||
<group position={[0, 0, 0]} scale={3}> | ||
<Floor /> | ||
<BoxWall height={10} width={6} /> | ||
<RopeJoint | ||
ropeLength={35} | ||
anchorPosition={[0, 15, 0]} | ||
ballPosition={[-8, 15, 8]} | ||
/> | ||
</group> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Box, Sphere } from "@react-three/drei"; | ||
import { | ||
BallCollider, | ||
RapierRigidBody, | ||
RigidBody, | ||
RigidBodyOptions, | ||
useSpringJoint | ||
} from "@react-three/rapier"; | ||
import { forwardRef, useMemo, useRef } from "react"; | ||
import { Demo } from "../../App"; | ||
import { useForwardedRef } from "@react-three/rapier/src/hooks/use-forwarded-ref"; | ||
import { vectorArrayToVector3 } from "@react-three/rapier/src/utils/utils"; | ||
|
||
const COLORS_ARR = ["#335C67", "#FFF3B0", "#E09F3E", "#9E2A2B", "#540B0E"]; | ||
|
||
interface BallSpringProps extends RigidBodyOptions { | ||
jointNum: number; | ||
total: number; | ||
} | ||
|
||
interface BoxRigidBodyProps extends RigidBodyOptions { | ||
color: string; | ||
} | ||
|
||
const BoxRigidBody = ({ color, ...props }: BoxRigidBodyProps) => { | ||
return ( | ||
<RigidBody {...props} ccd canSleep={false}> | ||
<Box castShadow receiveShadow> | ||
<meshStandardMaterial color={color} /> | ||
</Box> | ||
</RigidBody> | ||
); | ||
}; | ||
|
||
const BallSpring = forwardRef<RapierRigidBody, BallSpringProps>( | ||
(props, floorRef) => { | ||
const floor = useForwardedRef(floorRef); | ||
const ball = useRef<RapierRigidBody>(null); | ||
|
||
const stiffness = 1.0e3; | ||
const criticalDamping = 2.0 * Math.sqrt(stiffness * (props.mass ?? 1)); | ||
const dampingRatio = props.jointNum / (props.total / 2); | ||
const damping = dampingRatio * criticalDamping; | ||
|
||
const ballPos = props.position as THREE.Vector3; | ||
|
||
if (!ballPos) { | ||
throw new Error("BallSpring requires a position prop"); | ||
} | ||
|
||
useSpringJoint(ball, floor, [ | ||
[0, 0, 0], | ||
[ballPos.x, ballPos.y - 3, ballPos.z], | ||
0, | ||
stiffness, | ||
damping | ||
]); | ||
|
||
return ( | ||
<RigidBody | ||
key={`spring-${props.jointNum}`} | ||
{...props} | ||
ref={ball} | ||
ccd | ||
name={`spring-${props.jointNum}`} | ||
position={ballPos} | ||
colliders={false} | ||
canSleep={false} | ||
> | ||
<Sphere args={[0.5]} castShadow receiveShadow> | ||
<meshStandardMaterial color="#E09F3E" /> | ||
</Sphere> | ||
<BallCollider args={[0.5]} /> | ||
</RigidBody> | ||
); | ||
} | ||
); | ||
|
||
export const SpringExample: Demo = () => { | ||
const floor = useRef<RapierRigidBody>(null); | ||
|
||
const vectorArr = useMemo(() => { | ||
return Array.from({ length: 30 }).map((_, i) => { | ||
return vectorArrayToVector3([-20 + 1.5 * (i + 1), 7.5, -30]); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<RigidBody ref={floor} position={[0, 0, 0]} type="fixed" /> | ||
|
||
{vectorArr.map((_, i) => ( | ||
<group key={`box-ball-${i}`}> | ||
<BallSpring | ||
key={`ball-${i}`} | ||
ref={floor} | ||
position={vectorArr[i]} | ||
mass={1} | ||
jointNum={i} | ||
total={30} | ||
/> | ||
<BoxRigidBody | ||
key={`box-${i}`} | ||
color={COLORS_ARR[i % 5]} | ||
position={[vectorArr[i].x, vectorArr[i].y + 3, vectorArr[i].z]} | ||
colliders="cuboid" | ||
density={100} | ||
/> | ||
</group> | ||
))} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.