imwithye commited on
Commit
aa74807
·
1 Parent(s): e9d3f6f

add speed control

Browse files
src/components/canvas.tsx CHANGED
@@ -7,13 +7,14 @@ import { Env } from "../components/env";
7
  import { RubiksCube } from "./rubiks-cube";
8
 
9
  export const Canvas = () => {
10
- const { roughness } = useControls({
11
- roughness: { value: 0.5, min: 0.2, max: 0.8 },
 
12
  });
13
 
14
  return (
15
  <ThreeCanvas shadows camera={{ position: [0, 0, 4.5], fov: 50 }}>
16
- <RubiksCube roughness={roughness} />
17
  <Env />
18
  </ThreeCanvas>
19
  );
 
7
  import { RubiksCube } from "./rubiks-cube";
8
 
9
  export const Canvas = () => {
10
+ const { cubeRoughness, cubeSpeed } = useControls({
11
+ cubeRoughness: { value: 0.5, min: 0.2, max: 0.8 },
12
+ cubeSpeed: { value: 2, min: 1, max: 10 },
13
  });
14
 
15
  return (
16
  <ThreeCanvas shadows camera={{ position: [0, 0, 4.5], fov: 50 }}>
17
+ <RubiksCube cubeRoughness={cubeRoughness} cubeSpeed={cubeSpeed} />
18
  <Env />
19
  </ThreeCanvas>
20
  );
src/components/rotator.tsx CHANGED
@@ -4,6 +4,7 @@ import { RotationPanel } from "./rotation-panel";
4
  import { Group } from "three";
5
  import { Fragment, useRef } from "react";
6
  import { useFrame } from "@react-three/fiber";
 
7
 
8
  type RotateArgs = {
9
  rotatingFaceDirection: FacingDirection;
@@ -11,7 +12,11 @@ type RotateArgs = {
11
  rotatingGroup: Group;
12
  };
13
 
14
- export const Rotator = () => {
 
 
 
 
15
  const { getCubes, cubeGroupRef } = useCubesContext();
16
  const isRotating = useRef(false);
17
  const rotateArgs = useRef<RotateArgs>({
@@ -26,12 +31,11 @@ export const Rotator = () => {
26
  const cubeGroup = cubeGroupRef.current;
27
  if (!isRotating.current || !cubeGroup) return;
28
 
29
- const speed = 2;
30
  let sign = 0;
31
  switch (rotatingFaceDirection) {
32
  case "front":
33
  sign = rotatingDirection === "clockwise" ? -1 : 1;
34
- rotatingGroup.rotation.z += sign * delta * speed;
35
  if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
36
  rotatingGroup.rotation.z = (Math.PI / 2) * sign;
37
  isRotating.current = false;
@@ -39,7 +43,7 @@ export const Rotator = () => {
39
  break;
40
  case "back":
41
  sign = rotatingDirection === "clockwise" ? 1 : -1;
42
- rotatingGroup.rotation.z += sign * delta * speed;
43
  if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
44
  rotatingGroup.rotation.z = (Math.PI / 2) * sign;
45
  isRotating.current = false;
@@ -47,7 +51,7 @@ export const Rotator = () => {
47
  break;
48
  case "left":
49
  sign = rotatingDirection === "clockwise" ? 1 : -1;
50
- rotatingGroup.rotation.x += sign * delta * speed;
51
  if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
52
  rotatingGroup.rotation.x = (Math.PI / 2) * sign;
53
  isRotating.current = false;
@@ -55,7 +59,7 @@ export const Rotator = () => {
55
  break;
56
  case "right":
57
  sign = rotatingDirection === "clockwise" ? -1 : 1;
58
- rotatingGroup.rotation.x += sign * delta * speed;
59
  if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
60
  rotatingGroup.rotation.x = (Math.PI / 2) * sign;
61
  isRotating.current = false;
@@ -63,7 +67,7 @@ export const Rotator = () => {
63
  break;
64
  case "top":
65
  sign = rotatingDirection === "clockwise" ? -1 : 1;
66
- rotatingGroup.rotation.y += sign * delta * speed;
67
  if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
68
  rotatingGroup.rotation.y = (Math.PI / 2) * sign;
69
  isRotating.current = false;
@@ -71,7 +75,7 @@ export const Rotator = () => {
71
  break;
72
  case "bottom":
73
  sign = rotatingDirection === "clockwise" ? 1 : -1;
74
- rotatingGroup.rotation.y += sign * delta * speed;
75
  if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
76
  rotatingGroup.rotation.y = (Math.PI / 2) * sign;
77
  isRotating.current = false;
 
4
  import { Group } from "three";
5
  import { Fragment, useRef } from "react";
6
  import { useFrame } from "@react-three/fiber";
7
+ import { useControls } from "leva";
8
 
9
  type RotateArgs = {
10
  rotatingFaceDirection: FacingDirection;
 
12
  rotatingGroup: Group;
13
  };
14
 
15
+ type RotatorProps = {
16
+ cubeSpeed: number;
17
+ };
18
+
19
+ export const Rotator = ({ cubeSpeed }: RotatorProps) => {
20
  const { getCubes, cubeGroupRef } = useCubesContext();
21
  const isRotating = useRef(false);
22
  const rotateArgs = useRef<RotateArgs>({
 
31
  const cubeGroup = cubeGroupRef.current;
32
  if (!isRotating.current || !cubeGroup) return;
33
 
 
34
  let sign = 0;
35
  switch (rotatingFaceDirection) {
36
  case "front":
37
  sign = rotatingDirection === "clockwise" ? -1 : 1;
38
+ rotatingGroup.rotation.z += sign * delta * cubeSpeed;
39
  if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
40
  rotatingGroup.rotation.z = (Math.PI / 2) * sign;
41
  isRotating.current = false;
 
43
  break;
44
  case "back":
45
  sign = rotatingDirection === "clockwise" ? 1 : -1;
46
+ rotatingGroup.rotation.z += sign * delta * cubeSpeed;
47
  if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
48
  rotatingGroup.rotation.z = (Math.PI / 2) * sign;
49
  isRotating.current = false;
 
51
  break;
52
  case "left":
53
  sign = rotatingDirection === "clockwise" ? 1 : -1;
54
+ rotatingGroup.rotation.x += sign * delta * cubeSpeed;
55
  if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
56
  rotatingGroup.rotation.x = (Math.PI / 2) * sign;
57
  isRotating.current = false;
 
59
  break;
60
  case "right":
61
  sign = rotatingDirection === "clockwise" ? -1 : 1;
62
+ rotatingGroup.rotation.x += sign * delta * cubeSpeed;
63
  if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
64
  rotatingGroup.rotation.x = (Math.PI / 2) * sign;
65
  isRotating.current = false;
 
67
  break;
68
  case "top":
69
  sign = rotatingDirection === "clockwise" ? -1 : 1;
70
+ rotatingGroup.rotation.y += sign * delta * cubeSpeed;
71
  if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
72
  rotatingGroup.rotation.y = (Math.PI / 2) * sign;
73
  isRotating.current = false;
 
75
  break;
76
  case "bottom":
77
  sign = rotatingDirection === "clockwise" ? 1 : -1;
78
+ rotatingGroup.rotation.y += sign * delta * cubeSpeed;
79
  if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
80
  rotatingGroup.rotation.y = (Math.PI / 2) * sign;
81
  isRotating.current = false;
src/components/rubiks-cube.tsx CHANGED
@@ -14,10 +14,11 @@ for (let x = -0.5; x <= 0.5; x += 1) {
14
  }
15
 
16
  type RubiksCubeProps = {
17
- roughness: number;
 
18
  };
19
 
20
- export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
21
  const cubeGroupRef = useRef<Group | null>(null);
22
  return (
23
  <CubesProvider cubeGroupRef={cubeGroupRef}>
@@ -26,11 +27,11 @@ export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
26
  <CubePiece
27
  key={position.join(",")}
28
  initialPosition={position}
29
- roughness={roughness}
30
  />
31
  ))}
32
  </group>
33
- <Rotator />
34
  </CubesProvider>
35
  );
36
  };
 
14
  }
15
 
16
  type RubiksCubeProps = {
17
+ cubeRoughness: number;
18
+ cubeSpeed: number;
19
  };
20
 
21
+ export const RubiksCube = ({ cubeRoughness, cubeSpeed }: RubiksCubeProps) => {
22
  const cubeGroupRef = useRef<Group | null>(null);
23
  return (
24
  <CubesProvider cubeGroupRef={cubeGroupRef}>
 
27
  <CubePiece
28
  key={position.join(",")}
29
  initialPosition={position}
30
+ roughness={cubeRoughness}
31
  />
32
  ))}
33
  </group>
34
+ <Rotator cubeSpeed={cubeSpeed} />
35
  </CubesProvider>
36
  );
37
  };