Advertisement
Guest User

Untitled

a guest
Feb 1st, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  const toggleStop = () => {
  2.    setIsStoped(!isStoped);
  3.  };
  4.  React.useEffect(() => {
  5.    if (!isStoped) {
  6.      let lastTime = updateTime();
  7.      const windshield = params.windshield;
  8.      const friction = params.friction;
  9.      const updatePosition = setInterval(() => {
  10.        if (objectPositionY >= 0) {
  11.          const newTime = updateTime();
  12.          setObjectPositionX(
  13.            objectPositionX + ((newTime - lastTime) / 1000.0) * objectSpeedX
  14.          );
  15.          setObjectPositionY(
  16.            objectPositionY + ((newTime - lastTime) / 1000.0) * objectSpeedY
  17.          );
  18.          const accelerationX =
  19.            (-friction * objectSpeedX -
  20.              windshield *
  21.                objectSpeedX *
  22.                Math.sqrt(
  23.                  objectSpeedX * objectSpeedX + objectSpeedY * objectSpeedY
  24.                )) /
  25.            params.weight;
  26.          const accelerationY =
  27.            (friction * objectSpeedY +
  28.              accelerationOfGravity * params.weight +
  29.              windshield *
  30.                objectSpeedY *
  31.                Math.sqrt(
  32.                  objectSpeedX * objectSpeedX + objectSpeedY * objectSpeedY
  33.                )) /
  34.            params.weight;
  35.  
  36.          setObjectSpeedX(
  37.            objectSpeedX + ((newTime - lastTime) / 1000.0) * accelerationX
  38.          );
  39.          setObjectSpeedY(
  40.            objectSpeedY - ((newTime - lastTime) / 1000.0) * accelerationY
  41.          );
  42.          lastTime = newTime;
  43.        } else {
  44.          setObjectPositionY(0);
  45.        }
  46.      }, rerenderTime);
  47.      return () => clearInterval(updatePosition);
  48.    }
  49.  }, [params, isStoped]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement