Skip to content

Commit 78c82ea

Browse files
committed
Finished first tutorial - box with physics and basic interactivity
1 parent 36ffe5b commit 78c82ea

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

src/App.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import React from 'react';
22
import { Canvas } from "react-three-fiber";
33
import { Box } from "./Objects/Box";
4-
import { OrbitControls } from "drei";
4+
import { OrbitControls, Stars } from "drei";
5+
import { Ground } from "./Objects/Ground";
6+
import { Physics } from "use-cannon";
7+
import { GlobalLights } from "./World/GlobalLights";
58

69
function App() {
710
return (
811
<Canvas>
912
<OrbitControls />
10-
<Box />
13+
<Stars />
14+
<GlobalLights />
15+
<Physics>
16+
<Box />
17+
<Ground />
18+
</Physics>
1119
</Canvas>
1220
);
1321
}

src/Objects/Box.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import React from "react";
22

3+
import { useBox } from "use-cannon";
4+
35
export const Box = () => {
6+
const [ref, api] = useBox(() => ({
7+
mass: 0.1,
8+
position: [0, 4, 0],
9+
rotation: [(Math.random() - 0.5) * 4 * Math.PI, (Math.random() - 0.5) * 4 * Math.PI, (Math.random() - 0.5) * 4 * Math.PI],
10+
}));
411

5-
return <mesh>
12+
return <mesh ref={ref} onClick={() => {
13+
api.velocity.set((Math.random() - 0.5) * 6, 3, (Math.random() - 0.5) * 6)
14+
}}>
615
<boxBufferGeometry attach="geometry" />
716
<meshLambertMaterial attach="material" color="royalblue" />
817
</mesh>;

src/Objects/Ground.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from "react";
2+
import { MeshProps } from "react-three-fiber";
3+
import { usePlane } from "use-cannon";
4+
5+
const groundRotation: MeshProps["rotation"] = [-Math.PI / 2, 0, 0];
6+
7+
export const Ground = () => {
8+
const [ref] = usePlane(() => ({ rotation: groundRotation as number[], position: [0, -2.5, 0] }));
9+
10+
return <mesh ref={ref}>
11+
<planeBufferGeometry attach="geometry" args={[200, 200]} />
12+
<meshLambertMaterial attach="material" color="green" />
13+
</mesh >;
14+
}

src/World/GlobalLights.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
3+
export const GlobalLights = () => {
4+
return <>
5+
<ambientLight intensity={0.5} />
6+
<spotLight position={[10, 20, 30]} angle={0.3} />
7+
</>;
8+
}

src/index.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ body {
88
height: 100%;
99
}
1010

11+
html,
12+
body,
13+
#root {
14+
width: 100vw;
15+
height: 100vh;
16+
margin: 0;
17+
padding: 0;
18+
-webkit-touch-callout: none;
19+
-webkit-user-select: none;
20+
-khtml-user-select: none;
21+
-moz-user-select: none;
22+
-ms-user-select: none;
23+
user-select: none;
24+
/* overflow: hidden; */
25+
background: #272727;
26+
}
27+
1128
code {
1229
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
1330
monospace;

0 commit comments

Comments
 (0)