diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx | 63 |
1 files changed, 29 insertions, 34 deletions
diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx index 7175daf12..97f1d3f6c 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationWeight.tsx @@ -85,9 +85,9 @@ interface IState { updatedStartPosX: any; updatedStartPosY: any; walls: IWallProps[]; - xPosition: any; + xPosition: number; xVelocity: number; - yPosition: any; + yPosition: number; yVelocity: number; xAccel: number; yAccel: number; @@ -105,13 +105,13 @@ export default class Weight extends React.Component<IWeightProps, IState> { kineticFriction: false, maxPosYConservation: 0, timer: 0, - updatedStartPosX: this.props.startPosX ?? 0, - updatedStartPosY: this.props.startPosY ?? 0, + updatedStartPosX: this.props.startPosX, + updatedStartPosY: this.props.startPosY, walls: [], - xPosition: this.props.startPosX ?? 0, - xVelocity: this.props.startVelX ? this.props.startVelX : 0, - yPosition: this.props.startPosY ?? 0, - yVelocity: this.props.startVelY ? this.props.startVelY : 0, + xPosition: this.props.startPosX, + xVelocity: this.props.startVelX, + yPosition: this.props.startPosY, + yVelocity: this.props.startVelY, xAccel: 0, yAccel: 0, }; @@ -124,7 +124,7 @@ export default class Weight extends React.Component<IWeightProps, IState> { // Constants @computed get draggable() { - return this.props.simulationType != 'Inclined Plane' && this.props.simulationType != 'Pendulum' && this.props.simulationMode == 'Freeform'; + return !['Inclined Plane', 'Pendulum'].includes(this.props.simulationType) && this.props.simulationMode === 'Freeform'; } epsilon = 0.0001; labelBackgroundColor = `rgba(255,255,255,0.5)`; @@ -190,15 +190,13 @@ export default class Weight extends React.Component<IWeightProps, IState> { this.props.setPosition(undefined, this.getDisplayYPos(y)); if (this.props.displayXVelocity != this.state.xVelocity) { - const x = this.props.displayXVelocity; - this.setState({ xVelocity: x }); - this.props.setVelocity(x, undefined); + this.setState({ xVelocity: this.props.displayXVelocity }); + this.props.setVelocity(this.props.displayXVelocity, undefined); } if (this.props.displayYVelocity != -this.state.yVelocity) { - const y = this.props.displayYVelocity; - this.setState({ yVelocity: -y }); - this.props.setVelocity(undefined, y); + this.setState({ yVelocity: -this.props.displayYVelocity }); + this.props.setVelocity(undefined, this.props.displayYVelocity); } } @@ -293,13 +291,9 @@ export default class Weight extends React.Component<IWeightProps, IState> { directionInDegrees: 270, component: false, }; - if (this.props.coefficientOfKineticFriction != 0) { - this.props.setForcesUpdated([gravityForce, normalForce, frictionForce]); - this.props.setComponentForces([frictionForceComponent, normalForceComponent, gravityParallel, gravityPerpendicular]); - } else { - this.props.setForcesUpdated([gravityForce, normalForce]); - this.props.setComponentForces([normalForceComponent, gravityParallel, gravityPerpendicular]); - } + const kineticFriction = this.props.coefficientOfKineticFriction != 0; + this.props.setForcesUpdated([gravityForce, normalForce, ...(kineticFriction ? [frictionForce] : [])]); + this.props.setComponentForces([normalForceComponent, gravityParallel, gravityPerpendicular, ...(kineticFriction ? [frictionForceComponent] : [])]); } } @@ -569,16 +563,17 @@ export default class Weight extends React.Component<IWeightProps, IState> { const forces = this.getForces(xPos, yPos, xVel, yVel); const xAcc = this.getNewAccelerationX(forces); const yAcc = this.getNewAccelerationY(forces); + const coeff = (this.props.timestepSize * 1.0) / 6.0; for (let i = 0; i < this.props.simulationSpeed; i++) { const k1 = this.evaluate(xPos, yPos, xVel, yVel, xVel, yVel, xAcc, yAcc, 0); const k2 = this.evaluate(xPos, yPos, xVel, yVel, k1.deltaXPos, k1.deltaYPos, k1.deltaXVel, k1.deltaYVel, this.props.timestepSize * 0.5); const k3 = this.evaluate(xPos, yPos, xVel, yVel, k2.deltaXPos, k2.deltaYPos, k2.deltaXVel, k2.deltaYVel, this.props.timestepSize * 0.5); const k4 = this.evaluate(xPos, yPos, xVel, yVel, k3.deltaXPos, k3.deltaYPos, k3.deltaXVel, k3.deltaYVel, this.props.timestepSize); - xVel += ((this.props.timestepSize * 1.0) / 6.0) * (k1.deltaXVel + 2 * (k2.deltaXVel + k3.deltaXVel) + k4.deltaXVel); - yVel += ((this.props.timestepSize * 1.0) / 6.0) * (k1.deltaYVel + 2 * (k2.deltaYVel + k3.deltaYVel) + k4.deltaYVel); - xPos += ((this.props.timestepSize * 1.0) / 6.0) * (k1.deltaXPos + 2 * (k2.deltaXPos + k3.deltaXPos) + k4.deltaXPos); - yPos += ((this.props.timestepSize * 1.0) / 6.0) * (k1.deltaYPos + 2 * (k2.deltaYPos + k3.deltaYPos) + k4.deltaYPos); + xVel += coeff * (k1.deltaXVel + 2 * (k2.deltaXVel + k3.deltaXVel) + k4.deltaXVel); + yVel += coeff * (k1.deltaYVel + 2 * (k2.deltaYVel + k3.deltaYVel) + k4.deltaYVel); + xPos += coeff * (k1.deltaXPos + 2 * (k2.deltaXPos + k3.deltaXPos) + k4.deltaXPos); + yPos += coeff * (k1.deltaYPos + 2 * (k2.deltaYPos + k3.deltaYPos) + k4.deltaYPos); } // make sure harmonic motion maintained and errors don't propagate switch (this.props.simulationType) { @@ -1054,10 +1049,10 @@ export default class Weight extends React.Component<IWeightProps, IState> { if (force.magnitude < this.epsilon) { return; } - const arrowStartY: number = this.state.yPosition + this.props.radius; - const arrowStartX: number = this.state.xPosition + this.props.radius; - const arrowEndY: number = arrowStartY - Math.abs(force.magnitude) * 20 * Math.sin((force.directionInDegrees * Math.PI) / 180); - const arrowEndX: number = arrowStartX + Math.abs(force.magnitude) * 20 * Math.cos((force.directionInDegrees * Math.PI) / 180); + const arrowStartY = this.state.yPosition + this.props.radius; + const arrowStartX = this.state.xPosition + this.props.radius; + const arrowEndY = arrowStartY - Math.abs(force.magnitude) * 20 * Math.sin((force.directionInDegrees * Math.PI) / 180); + const arrowEndX = arrowStartX + Math.abs(force.magnitude) * 20 * Math.cos((force.directionInDegrees * Math.PI) / 180); const color = '#0d0d0d'; @@ -1119,10 +1114,10 @@ export default class Weight extends React.Component<IWeightProps, IState> { if (force.magnitude < this.epsilon) { return; } - const arrowStartY: number = this.state.yPosition + this.props.radius; - const arrowStartX: number = this.state.xPosition + this.props.radius; - const arrowEndY: number = arrowStartY - Math.abs(force.magnitude) * 20 * Math.sin((force.directionInDegrees * Math.PI) / 180); - const arrowEndX: number = arrowStartX + Math.abs(force.magnitude) * 20 * Math.cos((force.directionInDegrees * Math.PI) / 180); + const arrowStartY = this.state.yPosition + this.props.radius; + const arrowStartX = this.state.xPosition + this.props.radius; + const arrowEndY = arrowStartY - Math.abs(force.magnitude) * 20 * Math.sin((force.directionInDegrees * Math.PI) / 180); + const arrowEndX = arrowStartX + Math.abs(force.magnitude) * 20 * Math.cos((force.directionInDegrees * Math.PI) / 180); const color = '#0d0d0d'; |