-
Notifications
You must be signed in to change notification settings - Fork 54
/
CarExample.tsx
89 lines (84 loc) 路 2.08 KB
/
CarExample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Box, Cylinder } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import {
RapierRigidBody,
RigidBody,
useRevoluteJoint,
Vector3Array
} from "@react-three/rapier";
import { createRef, RefObject, useRef } from "react";
import { Demo } from "../../App";
const WheelJoint = ({
body,
wheel,
bodyAnchor,
wheelAnchor,
rotationAxis
}: {
body: RefObject<RapierRigidBody>;
wheel: RefObject<RapierRigidBody>;
bodyAnchor: Vector3Array;
wheelAnchor: Vector3Array;
rotationAxis: Vector3Array;
}) => {
const joint = useRevoluteJoint(body, wheel, [
bodyAnchor,
wheelAnchor,
rotationAxis
]);
useFrame(() => {
if (joint.current) {
joint.current.configureMotorVelocity(20, 10);
}
});
return null;
};
export const Car: Demo = () => {
const bodyRef = useRef<RapierRigidBody>(null);
const wheelPositions: [number, number, number][] = [
[-3, 0, 2],
[-3, 0, -2],
[3, 0, 2],
[3, 0, -2]
];
const wheelRefs = useRef(
wheelPositions.map(() => createRef<RapierRigidBody>())
);
return (
<group>
<RigidBody colliders="cuboid" ref={bodyRef} type="dynamic">
<Box scale={[6, 1, 1.9]} castShadow receiveShadow name="chassis">
<meshStandardMaterial color={"red"} />
</Box>
</RigidBody>
{wheelPositions.map((wheelPosition, index) => (
<RigidBody
position={wheelPosition}
colliders="hull"
type="dynamic"
key={index}
ref={wheelRefs.current[index]}
>
<Cylinder
rotation={[Math.PI / 2, 0, 0]}
args={[1, 1, 1, 32]}
castShadow
receiveShadow
>
<meshStandardMaterial color={"grey"} />
</Cylinder>
</RigidBody>
))}
{wheelPositions.map((wheelPosition, index) => (
<WheelJoint
key={index}
body={bodyRef}
wheel={wheelRefs.current[index]}
bodyAnchor={wheelPosition}
wheelAnchor={[0, 0, 0]}
rotationAxis={[0, 0, 1]}
/>
))}
</group>
);
};