Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const toggleStop = () => {
- setIsStoped(!isStoped);
- };
- React.useEffect(() => {
- if (!isStoped) {
- let lastTime = updateTime();
- const windshield = params.windshield;
- const friction = params.friction;
- const updatePosition = setInterval(() => {
- if (objectPositionY >= 0) {
- const newTime = updateTime();
- setObjectPositionX(
- objectPositionX + ((newTime - lastTime) / 1000.0) * objectSpeedX
- );
- setObjectPositionY(
- objectPositionY + ((newTime - lastTime) / 1000.0) * objectSpeedY
- );
- const accelerationX =
- (-friction * objectSpeedX -
- windshield *
- objectSpeedX *
- Math.sqrt(
- objectSpeedX * objectSpeedX + objectSpeedY * objectSpeedY
- )) /
- params.weight;
- const accelerationY =
- (friction * objectSpeedY +
- accelerationOfGravity * params.weight +
- windshield *
- objectSpeedY *
- Math.sqrt(
- objectSpeedX * objectSpeedX + objectSpeedY * objectSpeedY
- )) /
- params.weight;
- setObjectSpeedX(
- objectSpeedX + ((newTime - lastTime) / 1000.0) * accelerationX
- );
- setObjectSpeedY(
- objectSpeedY - ((newTime - lastTime) / 1000.0) * accelerationY
- );
- lastTime = newTime;
- } else {
- setObjectPositionY(0);
- }
- }, rerenderTime);
- return () => clearInterval(updatePosition);
- }
- }, [params, isStoped]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement