|
| 1 | +import { Text } from "@react-three/drei"; |
| 2 | +import { useFrame } from "@react-three/fiber"; |
| 3 | +import { |
| 4 | + CuboidCollider, |
| 5 | + CylinderCollider, |
| 6 | + RigidBody, |
| 7 | +} from "@react-three/rapier"; |
| 8 | +import { useRef, useMemo } from "react"; |
| 9 | +import * as THREE from "three"; |
| 10 | + |
| 11 | +export default function DynamicPlatforms() { |
| 12 | + const sideMovePlatformRef = useRef(); |
| 13 | + const verticalMovePlatformRef = useRef(); |
| 14 | + const rotatePlatformRef = useRef(); |
| 15 | + const rotationDrumRef = useRef(); |
| 16 | + |
| 17 | + // Initializ animation settings |
| 18 | + let time = null; |
| 19 | + const xRotationAxies = new THREE.Vector3(1, 0, 0); |
| 20 | + const yRotationAxies = new THREE.Vector3(0, 1, 0); |
| 21 | + const zRotationAxies = new THREE.Vector3(0, 0, 1); |
| 22 | + const quaternionRotation = useMemo(() => new THREE.Quaternion(), []); |
| 23 | + |
| 24 | + useFrame((state) => { |
| 25 | + time = state.clock.elapsedTime; |
| 26 | + |
| 27 | + // Move platform |
| 28 | + sideMovePlatformRef.current?.setNextKinematicTranslation({ |
| 29 | + x: 5 * Math.sin(time / 2) - 12, |
| 30 | + y: -0.5, |
| 31 | + z: -10, |
| 32 | + }); |
| 33 | + |
| 34 | + // Elevate platform |
| 35 | + verticalMovePlatformRef.current?.setNextKinematicTranslation({ |
| 36 | + x: -25, |
| 37 | + y: 2 * Math.sin(time / 2) + 2, |
| 38 | + z: 0, |
| 39 | + }); |
| 40 | + verticalMovePlatformRef.current?.setNextKinematicRotation( |
| 41 | + quaternionRotation.setFromAxisAngle(yRotationAxies, time * 0.5) |
| 42 | + ); |
| 43 | + |
| 44 | + // Rotate platform |
| 45 | + rotatePlatformRef.current?.setNextKinematicRotation( |
| 46 | + quaternionRotation.setFromAxisAngle(yRotationAxies, time * 0.5) |
| 47 | + ); |
| 48 | + |
| 49 | + // Rotate drum |
| 50 | + rotationDrumRef.current?.setNextKinematicRotation( |
| 51 | + quaternionRotation.setFromAxisAngle(xRotationAxies, time * 0.5) |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + return ( |
| 56 | + <> |
| 57 | + {/* Moving platform */} |
| 58 | + <RigidBody |
| 59 | + type="kinematicPosition" |
| 60 | + ref={sideMovePlatformRef} |
| 61 | + colliders={false} |
| 62 | + > |
| 63 | + <Text |
| 64 | + scale={0.5} |
| 65 | + color="black" |
| 66 | + maxWidth={10} |
| 67 | + textAlign="center" |
| 68 | + position={[0, 2.5, 0]} |
| 69 | + > |
| 70 | + Kinematic Moving Platform |
| 71 | + </Text> |
| 72 | + <CuboidCollider args={[2.5, 0.1, 2.5]} /> |
| 73 | + <mesh receiveShadow castShadow> |
| 74 | + <boxGeometry args={[5, 0.2, 5]} /> |
| 75 | + <meshStandardMaterial color={"moccasin"} /> |
| 76 | + </mesh> |
| 77 | + </RigidBody> |
| 78 | + |
| 79 | + {/* Elevating platform */} |
| 80 | + <RigidBody |
| 81 | + type="kinematicPosition" |
| 82 | + position={[-25, 0, 0]} |
| 83 | + ref={verticalMovePlatformRef} |
| 84 | + colliders={false} |
| 85 | + > |
| 86 | + <Text |
| 87 | + scale={0.5} |
| 88 | + color="black" |
| 89 | + maxWidth={10} |
| 90 | + textAlign="center" |
| 91 | + position={[0, 2.5, 0]} |
| 92 | + rotation={[0, Math.PI / 2, 0]} |
| 93 | + > |
| 94 | + Kinematic Elevating Platform |
| 95 | + </Text> |
| 96 | + <CuboidCollider args={[2.5, 0.1, 2.5]} /> |
| 97 | + <mesh receiveShadow castShadow> |
| 98 | + <boxGeometry args={[5, 0.2, 5]} /> |
| 99 | + <meshStandardMaterial color={"moccasin"} /> |
| 100 | + </mesh> |
| 101 | + </RigidBody> |
| 102 | + |
| 103 | + {/* Rotating Platform */} |
| 104 | + <RigidBody |
| 105 | + type="kinematicPosition" |
| 106 | + position={[-25, -0.5, -10]} |
| 107 | + ref={rotatePlatformRef} |
| 108 | + colliders={false} |
| 109 | + > |
| 110 | + <Text |
| 111 | + scale={0.5} |
| 112 | + color="black" |
| 113 | + maxWidth={10} |
| 114 | + textAlign="center" |
| 115 | + position={[0, 2.5, 0]} |
| 116 | + > |
| 117 | + Kinematic Rotating Platform |
| 118 | + </Text> |
| 119 | + <CuboidCollider args={[2.5, 0.1, 2.5]} /> |
| 120 | + <mesh receiveShadow castShadow> |
| 121 | + <boxGeometry args={[5, 0.2, 5]} /> |
| 122 | + <meshStandardMaterial color={"moccasin"} /> |
| 123 | + </mesh> |
| 124 | + </RigidBody> |
| 125 | + |
| 126 | + {/* Rotating drum */} |
| 127 | + <Text |
| 128 | + scale={0.5} |
| 129 | + color="black" |
| 130 | + maxWidth={10} |
| 131 | + textAlign="center" |
| 132 | + position={[-15, 2.5, -15]} |
| 133 | + > |
| 134 | + Kinematic Rotating Drum |
| 135 | + </Text> |
| 136 | + <RigidBody |
| 137 | + colliders={false} |
| 138 | + type="kinematicPosition" |
| 139 | + position={[-15, -1, -15]} |
| 140 | + ref={rotationDrumRef} |
| 141 | + > |
| 142 | + <group rotation={[0, 0, Math.PI / 2]}> |
| 143 | + <CylinderCollider args={[5, 1]} /> |
| 144 | + <mesh receiveShadow> |
| 145 | + <cylinderGeometry args={[1, 1, 10]} /> |
| 146 | + <meshStandardMaterial color={"moccasin"} /> |
| 147 | + </mesh> |
| 148 | + </group> |
| 149 | + </RigidBody> |
| 150 | + </> |
| 151 | + ); |
| 152 | +} |
0 commit comments