Project 27: Multiple Falling Objects
3D Engineer
Step 1 of 2
Project
Review: Physics Simulation
Simulate one falling, bouncing box with a velocity ref.
🎯 Your Task
Please add the exact code shown in the light gray box below to your editor.Do not delete your previous code, just insert these new lines in the correct place!
function FallingBox({ x }) {
const ref = useRef();
const velocity = useRef(0);
useFrame((state, delta) => {
velocity.current -= 9.8 * delta;
ref.current.position.y += velocity.current * delta;
if (ref.current.position.y < -1.5) {
ref.current.position.y = -1.5;
velocity.current *= -0.5;
}
});
return (
<mesh ref={ref} position={[x, 2, 0]}>
<boxGeometry args={[0.6, 0.6, 0.6]} />
<meshStandardMaterial color="orange" />
</mesh>
);
}
export default function Scene() {
return (
<Canvas>
<ambientLight intensity={0.7} />
<FallingBox x={-1} />
</Canvas>
);
}