From 4832bb8e9589a786ac08d24b6e55bb23d19ce855 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Tue, 23 May 2023 11:31:49 -0400
Subject: fixed physics force assignment to fields

---
 .../nodes/PhysicsBox/PhysicsSimulationWeight.tsx   | 66 ++++++++++------------
 1 file changed, 29 insertions(+), 37 deletions(-)

(limited to 'src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx')

diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx
index 1d980ebf4..94e101490 100644
--- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx
+++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx
@@ -1,7 +1,6 @@
 import { Doc, HeightSym, WidthSym } from '../../../../fields/Doc';
 import React = require('react');
 import './PhysicsSimulationBox.scss';
-import { NumCast } from '../../../../fields/Types';
 import { computed } from 'mobx';
 
 interface IWallProps {
@@ -24,7 +23,8 @@ export interface IWeightProps {
     circularMotionRadius: number;
     coefficientOfKineticFriction: number;
     color: string;
-    componentForces: IForce[];
+    componentForces: () => IForce[];
+    setComponentForces: (x: IForce[]) => {};
     displayXVelocity: number;
     displayYVelocity: number;
     elasticCollisions: boolean;
@@ -47,7 +47,7 @@ export interface IWeightProps {
     springConstant: number;
     springRestLength: number;
     springStartLength: number;
-    startForces: IForce[];
+    startForces: () => IForce[];
     startPendulumAngle: number;
     startPosX: number;
     startPosY: number;
@@ -56,7 +56,8 @@ export interface IWeightProps {
     timestepSize: number;
     updateXDisplay: number;
     updateYDisplay: number;
-    forcesUpdated: IForce[];
+    forcesUpdated: () => IForce[];
+    setForcesUpdated: (x: IForce[]) => {};
     wallPositions: IWallProps[];
     wedgeHeight: number;
     wedgeWidth: number;
@@ -188,15 +189,15 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         this.setYVelDisplay(yVel);
         this.setXVelDisplay(xVel);
         if (this.props.color == 'red') {
-            this.props.dataDoc.mass1_accelerationY = (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated) * 100)) / 100;
-            this.props.dataDoc.mass1_accelerationX = Math.round(this.getNewAccelerationX(this.props.forcesUpdated) * 100) / 100;
+            this.props.dataDoc.mass1_accelerationY = (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated()) * 100)) / 100;
+            this.props.dataDoc.mass1_accelerationX = Math.round(this.getNewAccelerationX(this.props.forcesUpdated()) * 100) / 100;
         } else {
-            this.props.dataDoc.mass2_accelerationY = (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated) * 100)) / 100;
-            this.props.dataDoc.mass2_accelerationX = Math.round(this.getNewAccelerationX(this.props.forcesUpdated) * 100) / 100;
+            this.props.dataDoc.mass2_accelerationY = (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated()) * 100)) / 100;
+            this.props.dataDoc.mass2_accelerationX = Math.round(this.getNewAccelerationX(this.props.forcesUpdated()) * 100) / 100;
         }
 
-        this.setState({ xAccel: Math.round(this.getNewAccelerationX(this.props.forcesUpdated) * 100) / 100 });
-        this.setState({ yAccel: (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated) * 100)) / 100 });
+        this.setState({ xAccel: Math.round(this.getNewAccelerationX(this.props.forcesUpdated()) * 100) / 100 });
+        this.setState({ yAccel: (-1 * Math.round(this.getNewAccelerationY(this.props.forcesUpdated()) * 100)) / 100 });
     };
 
     componentDidUpdate(prevProps: Readonly<IWeightProps>, prevState: Readonly<IState>, snapshot?: any): void {
@@ -265,13 +266,6 @@ export default class Weight extends React.Component<IWeightProps, IState> {
             }
         }
 
-        // Prevent bug when switching between sims
-        if (prevProps.startForces != this.props.startForces) {
-            this.setState({ xVelocity: this.props.startVelX });
-            this.setState({ yVelocity: this.props.startVelY });
-            this.setDisplayValues();
-        }
-
         // Make sure weight doesn't go above max height
         if ((prevState.updatedStartPosY != this.state.updatedStartPosY || prevProps.startVelY != this.props.startVelY) && !isNaN(this.state.updatedStartPosY) && !isNaN(this.props.startVelY)) {
             if (this.props.simulationType == 'One Weight') {
@@ -370,11 +364,11 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                     component: false,
                 };
                 if (this.props.coefficientOfKineticFriction != 0) {
-                    this.props.dataDoc.mass1_forcesUpdated = [gravityForce, normalForce, frictionForce];
-                    this.props.dataDoc.componentForces = [frictionForceComponent, normalForceComponent, gravityParallel, gravityPerpendicular];
+                    this.props.setForcesUpdated([gravityForce, normalForce, frictionForce]);
+                    this.props.setComponentForces([frictionForceComponent, normalForceComponent, gravityParallel, gravityPerpendicular]);
                 } else {
-                    this.props.dataDoc.mass1_forcesUpdated = [gravityForce, normalForce];
-                    this.props.dataDoc.componentForces = [normalForceComponent, gravityParallel, gravityPerpendicular];
+                    this.props.setForcesUpdated([gravityForce, normalForce]);
+                    this.props.setComponentForces([normalForceComponent, gravityParallel, gravityPerpendicular]);
                 }
             }
         }
@@ -444,8 +438,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         this.setState({ xVelocity: this.props.startVelX });
         this.setState({ yVelocity: this.props.startVelY });
         this.props.dataDoc.pendulum_angle = this.props.startPendulumAngle;
-        this.props.dataDoc.mass1_forcesUpdated = this.props.dataDoc.mass1_forcesStart;
-        this.props.dataDoc.mass2_forcesUpdated = this.props.dataDoc.mass2_forcesStart;
+        this.props.setForcesUpdated(this.props.startForces());
         if (this.props.color == 'red') {
             this.props.dataDoc.mass1_positionX = this.state.updatedStartPosX;
             this.props.dataDoc.mass1_positionY = this.state.updatedStartPosY;
@@ -634,7 +627,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                                     directionInDegrees: wall.angleInDegrees + 90,
                                     component: false,
                                 };
-                                this.props.dataDoc.mass1_forcesUpdated = [forceOfGravity, normalForce];
+                                this.props.setForcesUpdated([forceOfGravity, normalForce]);
                                 if (this.props.simulationType == 'Inclined Plane') {
                                     const forceOfGravityC: IForce = {
                                         description: 'Gravity',
@@ -648,7 +641,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                                         directionInDegrees: wall.angleInDegrees + 90,
                                         component: true,
                                     };
-                                    this.props.dataDoc.componentForces = [forceOfGravityC, normalForceC];
+                                    this.props.setComponentForces([forceOfGravityC, normalForceC]);
                                 }
                             }
                         }
@@ -684,7 +677,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         const newYVel = currentYVel + deltaYVel * dt;
         const newDeltaXPos = newXVel;
         const newDeltaYPos = newYVel;
-        let forces = this.props.forcesUpdated;
+        let forces = this.props.forcesUpdated();
         if (this.props.simulationType == 'Pendulum') {
             forces = this.getNewPendulumForces(newXPos, newYPos, newXVel, newYVel);
         } else if (this.props.simulationType == 'Spring') {
@@ -714,7 +707,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         let yPos = this.state.yPosition;
         let xVel = this.state.xVelocity;
         let yVel = this.state.yVelocity;
-        let forces: IForce[] = this.props.dataDoc.mass1_forcesUpdated;
+        let forces = this.props.forcesUpdated();
         if (this.props.simulationType == 'Pendulum') {
             forces = this.getNewPendulumForces(xPos, yPos, xVel, yVel);
         } else if (this.props.simulationType == 'Spring') {
@@ -772,7 +765,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         this.setState({ yVelocity: yVel });
         this.setState({ xPosition: xPos });
         this.setState({ yPosition: yPos });
-        let forcesn = this.props.dataDoc.mass1_forcesUpdated;
+        let forcesn = this.props.forcesUpdated();
         if (this.props.simulationType == 'Pendulum') {
             forcesn = this.getNewPendulumForces(xPos, yPos, xVel, yVel);
         } else if (this.props.simulationType == 'Spring') {
@@ -780,7 +773,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
         } else if (this.props.simulationType == 'Circular Motion') {
             forcesn = this.getNewCircularMotionForces(xPos, yPos);
         }
-        this.props.dataDoc.mass1_forcesUpdated = forcesn;
+        this.props.setForcesUpdated(forcesn);
 
         // set component forces if they change
         if (this.props.simulationType == 'Pendulum') {
@@ -821,7 +814,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                 gravityPerpendicular.magnitude = Math.abs(Math.abs(this.props.gravity) * Math.sin(((90 - angle) * Math.PI) / 180));
                 gravityPerpendicular.directionInDegrees = 180 - (90 - angle);
             }
-            this.props.dataDoc.componentForces = [tensionComponent, gravityParallel, gravityPerpendicular];
+            this.props.setComponentForces([tensionComponent, gravityParallel, gravityPerpendicular]);
         }
     };
 
@@ -931,7 +924,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                                     directionInDegrees: 270,
                                     component: false,
                                 };
-                                this.props.dataDoc.mass1_forcesUpdated = [tensionForce1, tensionForce2, grav];
+                                this.props.setForcesUpdated([tensionForce1, tensionForce2, grav]);
                             }
                         }
                     }}>
@@ -1115,7 +1108,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                                         top: 30 + 'px',
                                         backgroundColor: this.labelBackgroundColor,
                                     }}>
-                                    {Math.round(this.props.pendulum_angle * 100) / 100}°
+                                    {Math.round(this.props.pendulumAngle * 100) / 100}°
                                 </p>
                             </div>
                         )}
@@ -1157,8 +1150,8 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                                 <line
                                     x1={this.state.xPosition + this.props.radius}
                                     y1={this.state.yPosition + this.props.radius}
-                                    x2={this.state.xPosition + this.props.radius + this.getNewAccelerationX(this.props.forcesUpdated) * 15}
-                                    y2={this.state.yPosition + this.props.radius + this.getNewAccelerationY(this.props.forcesUpdated) * 15}
+                                    x2={this.state.xPosition + this.props.radius + this.getNewAccelerationX(this.props.forcesUpdated()) * 15}
+                                    y2={this.state.yPosition + this.props.radius + this.getNewAccelerationY(this.props.forcesUpdated()) * 15}
                                     stroke={'green'}
                                     strokeWidth="5"
                                     markerEnd="url(#accArrow)"
@@ -1220,7 +1213,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                 )}
                 {!this.state.dragging &&
                     this.props.showComponentForces &&
-                    this.props.componentForces.map((force, index) => {
+                    this.props.componentForces().map((force, index) => {
                         if (force.magnitude < this.epsilon) {
                             return;
                         }
@@ -1285,8 +1278,7 @@ export default class Weight extends React.Component<IWeightProps, IState> {
                     })}
                 {!this.state.dragging &&
                     this.props.showForces &&
-                    this.props.forcesUpdated &&
-                    this.props.forcesUpdated.map((force, index) => {
+                    this.props.forcesUpdated().map((force, index) => {
                         if (force.magnitude < this.epsilon) {
                             return;
                         }
-- 
cgit v1.2.3-70-g09d2

