From d071f4d835bca978833c6b7adeda56f41e82ef06 Mon Sep 17 00:00:00 2001 From: brynnchernosky <56202540+brynnchernosky@users.noreply.github.com> Date: Mon, 1 May 2023 15:07:43 -0400 Subject: refactor weight --- .../nodes/PhysicsBox/PhysicsSimulationWeight.tsx | 125 +++++++++++---------- 1 file changed, 64 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx index 3bc02cfdd..ab23c42c0 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx @@ -206,43 +206,46 @@ export default class Weight extends React.Component { }; + + // Compute x acceleration from forces, F=ma - const getNewAccelerationX = (forceList: IForce[]) => { + getNewAccelerationX = (forceList: IForce[]) => { let newXAcc = 0; - forceList.forEach((force) => { - if (force.component == false) { + if (forceList) { + forceList.forEach((force) => { newXAcc += (force.magnitude * Math.cos((force.directionInDegrees * Math.PI) / 180)) / - mass; - } - }); + this.props.mass; + }); + } return newXAcc; }; + // Compute y acceleration from forces, F=ma - const getNewAccelerationY = (forceList: IForce[]) => { + getNewAccelerationY = (forceList: IForce[]) => { let newYAcc = 0; - forceList.forEach((force) => { - if (force.component == false) { + if (forceList) { + forceList.forEach((force) => { newYAcc += (-1 * (force.magnitude * Math.sin((force.directionInDegrees * Math.PI) / 180))) / - mass; - } - }); + this.props.mass; + }); + } return newYAcc; }; // Compute uniform circular motion forces given x, y positions - const getNewCircularMotionForces = (xPos: number, yPos: number) => { - let deltaX = (xMin + xMax) / 2 - (xPos + radius); - let deltaY = yPos + radius - (yMin + yMax) / 2; + getNewCircularMotionForces = (xPos: number, yPos: number) => { + let deltaX = (this.props.xMin + this.props.xMax) / 2 - (xPos + this.props.radius); + let deltaY = yPos + this.props.radius - (this.props.yMin + this.props.yMax) / 2; let dir = (Math.atan2(deltaY, deltaX) * 180) / Math.PI; const tensionForce: IForce = { description: "Centripetal Force", - magnitude: (startVelX ** 2 * mass) / circularMotionRadius, + magnitude: (this.props.startVelX ** 2 * this.props.mass) / this.props.circularMotionRadius, directionInDegrees: dir, component: false, }; @@ -250,24 +253,24 @@ export default class Weight extends React.Component { }; // Compute spring forces given y position - const getNewSpringForces = (yPos: number) => { + getNewSpringForces = (yPos: number) => { let springForce: IForce = { description: "Spring Force", magnitude: 0, directionInDegrees: 90, component: false, }; - if (yPos - springRestLength > 0) { + if (yPos - this.props.springRestLength > 0) { springForce = { description: "Spring Force", - magnitude: springConstant * (yPos - springRestLength), + magnitude: this.props.springConstant * (yPos - this.props.springRestLength), directionInDegrees: 90, component: false, }; - } else if (yPos - springRestLength < 0) { + } else if (yPos - this.props.springRestLength < 0) { springForce = { description: "Spring Force", - magnitude: springConstant * (springRestLength - yPos), + magnitude: this.props.springConstant * (this.props.springRestLength - yPos), directionInDegrees: 270, component: false, }; @@ -276,7 +279,7 @@ export default class Weight extends React.Component { return [ { description: "Gravity", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: 270, component: false, }, @@ -285,14 +288,14 @@ export default class Weight extends React.Component { }; // Compute pendulum forces given position, velocity - const getNewPendulumForces = ( + getNewPendulumForces = ( xPos: number, yPos: number, xVel: number, yVel: number ) => { - const x = xMax / 2 - xPos - radius; - const y = yPos + radius + 5; + const x = this.props.xMax / 2 - xPos - this.props.radius; + const y = yPos + this.props.radius + 5; let angle = (Math.atan(y / x) * 180) / Math.PI; if (angle < 0) { angle += 180; @@ -303,11 +306,11 @@ export default class Weight extends React.Component { } const pendulumLength = Math.sqrt(x * x + y * y); - setPendulumAngle(oppositeAngle); + this.props.dataDoc['pendulumAngle'] = oppositeAngle; const mag = - mass * Math.abs(gravity) * Math.cos((oppositeAngle * Math.PI) / 180) + - (mass * (xVel * xVel + yVel * yVel)) / pendulumLength; + this.props.mass * 9.81 * Math.cos((oppositeAngle * Math.PI) / 180) + + (this.props.mass * (xVel * xVel + yVel * yVel)) / pendulumLength; const forceOfTension: IForce = { description: "Tension", @@ -319,7 +322,7 @@ export default class Weight extends React.Component { return [ { description: "Gravity", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: 270, component: false, }, @@ -330,29 +333,29 @@ export default class Weight extends React.Component { // Check for collisions in x direction const checkForCollisionsWithWall = () => { let collision = false; - const minX = xPosition; - const maxX = xPosition + 2 * radius; - if (xVelocity != 0) { - walls.forEach((wall) => { + const minX = this.state.xPosition; + const maxX = this.state.xPosition + 2 * this.props.radius; + if (this.state.xVelocity != 0) { + this.state.walls.forEach((wall) => { if (wall.angleInDegrees == 90) { const wallX = (wall.xPos / 100) * window.innerWidth; if (wall.xPos < 0.35) { if (minX <= wallX) { - setXPosition(wallX + 0.01); - if (elasticCollisions) { - setXVelocity(-xVelocity); + this.setState({xPosition: wallX+0.01}); + if (this.props.elasticCollisions) { + this.setState({xVelocity: -this.state.xVelocity}); } else { - setXVelocity(0); + this.setState({xVelocity: 0}); } collision = true; } } else { if (maxX >= wallX) { - setXPosition(wallX - 2 * radius - 0.01); - if (elasticCollisions) { - setXVelocity(-xVelocity); + this.setState({xPosition: wallX- 2 * this.props.radius-0.01}); + if (this.props.elasticCollisions) { + this.setState({xVelocity: -this.state.xVelocity}); } else { - setXVelocity(0); + this.setState({xVelocity: 0}); } collision = true; } @@ -366,46 +369,46 @@ export default class Weight extends React.Component { // Check for collisions in y direction const checkForCollisionsWithGround = () => { let collision = false; - const minY = yPosition; - const maxY = yPosition + 2 * radius; - if (yVelocity > 0) { - walls.forEach((wall) => { + const minY = this.state.yPosition; + const maxY = this.state.yPosition + 2 * this.props.radius; + if (this.state.yVelocity > 0) { + this.state.walls.forEach((wall) => { if (wall.angleInDegrees == 0 && wall.yPos > 0.4) { const groundY = (wall.yPos / 100) * window.innerHeight; if (maxY > groundY) { - setYPosition(groundY - 2 * radius - 0.01); - if (elasticCollisions) { - setYVelocity(-yVelocity); + this.setState({yPosition: groundY- 2 * this.props.radius-0.01}); + if (this.props.elasticCollisions) { + this.setState({yVelocity: -this.state.yVelocity}); } else { - setYVelocity(0); + this.setState({yVelocity: 0}); if (this.props.dataDoc['simulationType'] != "Two Weights") { const forceOfGravity: IForce = { description: "Gravity", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: 270, component: false, }; const normalForce: IForce = { description: "Normal force", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: wall.angleInDegrees + 90, component: false, }; - setUpdatedForces([forceOfGravity, normalForce]); + this.props.dataDoc['updatedForces'] = ([forceOfGravity, normalForce]); if (this.props.dataDoc['simulationType'] == "Inclined Plane") { const forceOfGravityC: IForce = { description: "Gravity", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: 270, component: true, }; const normalForceC: IForce = { description: "Normal force", - magnitude: Math.abs(gravity) * mass, + magnitude: Math.abs(this.props.gravity) * this.props.mass, directionInDegrees: wall.angleInDegrees + 90, component: true, }; - setComponentForces([forceOfGravityC, normalForceC]); + this.props.dataDoc['componentForces'] = ([forceOfGravityC, normalForceC]); } } } @@ -414,16 +417,16 @@ export default class Weight extends React.Component { } }); } - if (yVelocity < 0) { - walls.forEach((wall) => { + if (this.state.yVelocity < 0) { + this.state.walls.forEach((wall) => { if (wall.angleInDegrees == 0 && wall.yPos < 0.4) { const groundY = (wall.yPos / 100) * window.innerHeight; if (minY < groundY) { - setYPosition(groundY + 5); - if (elasticCollisions) { - setYVelocity(-yVelocity); + this.setState({yPosition: groundY + 0.01}); + if (this.props.elasticCollisions) { + this.setState({yVelocity: -this.state.yVelocity}); } else { - setYVelocity(0); + this.setState({yVelocity: 0}); } collision = true; } -- cgit v1.2.3-70-g09d2