From 4832bb8e9589a786ac08d24b6e55bb23d19ce855 Mon Sep 17 00:00:00 2001 From: bobzel Date: Tue, 23 May 2023 11:31:49 -0400 Subject: fixed physics force assignment to fields --- .../nodes/PhysicsBox/PhysicsSimulationBox.tsx | 164 ++++++++++----------- .../nodes/PhysicsBox/PhysicsSimulationWeight.tsx | 66 ++++----- 2 files changed, 107 insertions(+), 123 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx index 0f5bdf316..9d75944f4 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx @@ -102,26 +102,21 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent(); this.dataDoc.questionPartOne = ''; this.dataDoc.questionPartTwo = ''; - this.dataDoc.selectedSolutions = []; this.dataDoc.selectedQuestion = this.dataDoc.selectedQuestion ?? questions.inclinePlane[0]; // this.dataDoc.sketching = this.dataDoc.sketching ?? false; @@ -182,33 +177,33 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent { - const forces: IForce[] = []; - for (let i = 0; i < json.length; i++) { - const force: IForce = { - description: json[i].description, - magnitude: json[i].magnitude, - directionInDegrees: json[i].directionInDegrees, - component: json[i].component, - }; - forces.push(force); - } - return forces; - }; + public static parseJSON(json: string) { + return !json ? [] : (JSON.parse(json) as IForce[]); + } // Handle force change in review mode updateReviewModeValues = () => { @@ -868,10 +845,21 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass1_componentForces)); + setComponentForces1 = (forces: IForce[]) => (this.dataDoc.mass1_componentForces = JSON.stringify(forces)); + componentForces2 = () => PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass2_componentForces)); + setComponentForces2 = (forces: IForce[]) => (this.dataDoc.mass2_componentForces = JSON.stringify(forces)); + startForces1 = () => PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass1_forcesStart)); + startForces2 = () => PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass2_forcesStart)); + forcesUpdated1 = () => PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass1_forcesUpdated)); + setForcesUpdated1 = (forces: IForce[]) => (this.dataDoc.mass1_forcesUpdated = JSON.stringify(forces)); + forcesUpdated2 = () => PhysicsSimulationBox.parseJSON(StrCast(this.dataDoc.mass2_forcesUpdated)); + setForcesUpdated2 = (forces: IForce[]) => (this.dataDoc.mass2_forcesUpdated = JSON.stringify(forces)); + render() { return (
@@ -900,7 +888,8 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent @@ -1289,11 +1281,11 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent + disabled={this.dataDoc.tutorial_stepNumber === this.dataDoc.tutorial.steps.length - 1}>
@@ -1709,7 +1701,7 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent 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 { 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, prevState: Readonly, snapshot?: any): void { @@ -265,13 +266,6 @@ export default class Weight extends React.Component { } } - // 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { } 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 { 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 { 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 { top: 30 + 'px', backgroundColor: this.labelBackgroundColor, }}> - {Math.round(this.props.pendulum_angle * 100) / 100}° + {Math.round(this.props.pendulumAngle * 100) / 100}°

)} @@ -1157,8 +1150,8 @@ export default class Weight extends React.Component { { )} {!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 { })} {!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