-
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.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
22 changed files
with
822 additions
and
159 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
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
57 changes: 57 additions & 0 deletions
57
demo/src/examples/active-collision-types/ActiveCollisionTypesExample.tsx
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,57 @@ | ||
import { ActiveCollisionTypes } from "@dimforge/rapier3d-compat"; | ||
import { Box, Sphere } from "@react-three/drei"; | ||
import { useFrame } from "@react-three/fiber"; | ||
import { RapierRigidBody, RigidBody } from "@react-three/rapier"; | ||
import { useRef, useState } from "react"; | ||
|
||
const activeCollisionTypes = | ||
ActiveCollisionTypes.DEFAULT | ActiveCollisionTypes.KINEMATIC_FIXED; | ||
|
||
const Ball = () => { | ||
const rb = useRef<RapierRigidBody>(null); | ||
|
||
const [color, setColor] = useState("blue"); | ||
|
||
useFrame(({ clock: { elapsedTime } }) => { | ||
if (!rb.current) return; | ||
|
||
rb.current.setTranslation( | ||
{ x: Math.sin(elapsedTime) * 3, y: 0, z: 0 }, | ||
true | ||
); | ||
}); | ||
|
||
return ( | ||
<RigidBody | ||
ref={rb} | ||
colliders="ball" | ||
type="kinematicPosition" | ||
activeCollisionTypes={activeCollisionTypes} | ||
onCollisionEnter={() => setColor("green")} | ||
onCollisionExit={() => setColor("blue")} | ||
> | ||
<Sphere> | ||
<meshStandardMaterial color={color} /> | ||
</Sphere> | ||
</RigidBody> | ||
); | ||
}; | ||
|
||
const Wall = () => { | ||
return ( | ||
<RigidBody type="fixed" colliders="cuboid"> | ||
<Box args={[0.5, 5, 2]}> | ||
<meshStandardMaterial color="white" transparent opacity={0.5} /> | ||
</Box> | ||
</RigidBody> | ||
); | ||
}; | ||
|
||
export const ActiveCollisionTypesExample = () => { | ||
return ( | ||
<group> | ||
<Ball /> | ||
<Wall /> | ||
</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,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> | ||
); | ||
}; |
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> | ||
); | ||
}; |
Oops, something went wrong.