From 18b4c2ff62eb6fc3d6c1b5d0c6aeeaa785208aa6 Mon Sep 17 00:00:00 2001 From: bobzel Date: Wed, 24 May 2023 12:38:21 -0400 Subject: physics cleanup for window resizing, etc --- .../nodes/PhysicsBox/PhysicsSimulationBox.tsx | 354 ++++++++------------- .../nodes/PhysicsBox/PhysicsSimulationWeight.tsx | 30 +- 2 files changed, 146 insertions(+), 238 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx index e2b882389..c4e8e7721 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationBox.tsx @@ -6,8 +6,11 @@ import QuestionMarkIcon from '@mui/icons-material/QuestionMark'; import ReplayIcon from '@mui/icons-material/Replay'; import { Box, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, FormControl, FormControlLabel, FormGroup, IconButton, LinearProgress, Stack } from '@mui/material'; import Typography from '@mui/material/Typography'; +import { computed } from 'mobx'; import { observer } from 'mobx-react'; -import { HeightSym, NumListCast, WidthSym } from '../../../../fields/Doc'; +import { NumListCast } from '../../../../fields/Doc'; +import { List } from '../../../../fields/List'; +import { BoolCast, NumCast, StrCast } from '../../../../fields/Types'; import { ViewBoxAnnotatableComponent } from '../../DocComponent'; import { FieldView, FieldViewProps } from './../FieldView'; import './PhysicsSimulationBox.scss'; @@ -17,9 +20,6 @@ import * as tutorials from './PhysicsSimulationTutorial.json'; import Wall from './PhysicsSimulationWall'; import Weight from './PhysicsSimulationWeight'; import React = require('react'); -import { BoolCast, NumCast, StrCast } from '../../../../fields/Types'; -import { List } from '../../../../fields/List'; -import { computed, trace } from 'mobx'; interface IWallProps { length: number; @@ -82,6 +82,14 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent { + return { + description: 'Gravity', + magnitude: mass * Math.abs(this.gravity), + directionInDegrees: 270, + component: false, + }; + }; @computed get simulationType() { return StrCast(this.dataDoc.simulation_type, 'Inclined Plane'); } @@ -129,6 +137,9 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent { + const radAng = (angle * Math.PI) / 180; this.dataDoc.wedge_width = this.xMax * 0.5; - this.dataDoc.wedge_height = Math.tan((angle * Math.PI) / 180) * this.xMax * 0.5; + this.dataDoc.wedge_height = Math.tan(radAng) * this.dataDoc.wedge_width; // update weight position based on updated wedge width/height - let yPos = this.yMax - 0.08 * this.props.PanelHeight() * 2 - Math.tan((angle * Math.PI) / 180) * this.xMax * 0.5; + let yPos = this.yMax - this.mass1Radius * 2 - this.dataDoc.wedge_height - this.mass1Radius * Math.cos(radAng) + this.mass1Radius; + let xPos = this.xMax * 0.25 + this.mass1Radius * Math.sin(radAng) - this.mass1Radius; - // adjust y position - if (angle >= 5 && angle < 10) { - yPos += 0.08 * this.props.PanelHeight() * 0.1; - } else if (angle >= 10 && angle < 15) { - yPos += 0.08 * this.props.PanelHeight() * 0.23; - } else if (angle >= 15 && angle < 20) { - yPos += 0.08 * this.props.PanelHeight() * 0.26; - } else if (angle >= 20 && angle < 25) { - yPos += 0.08 * this.props.PanelHeight() * 0.33; - } else if (angle >= 25 && angle < 30) { - yPos += 0.08 * this.props.PanelHeight() * 0.35; - } else if (angle >= 30 && angle < 35) { - yPos += 0.08 * this.props.PanelHeight() * 0.4; - } else if (angle >= 35 && angle < 40) { - yPos += 0.08 * this.props.PanelHeight() * 0.45; - } else if (angle >= 40 && angle < 45) { - yPos += 0.08 * this.props.PanelHeight() * 0.47; - } else if (angle >= 45) { - yPos += 0.08 * this.props.PanelHeight() * 0.52; - } - - this.dataDoc.mass1_positionXstart = this.xMax * 0.25; + this.dataDoc.mass1_positionXstart = xPos; this.dataDoc.mass1_positionYstart = yPos; if (this.simulationMode == 'Freeform') { - this.updateForcesWithFriction(NumCast(this.dataDoc.coefficientOfStaticFriction), this.xMax * 0.5, Math.tan((angle * Math.PI) / 180) * this.xMax * 0.5); + this.updateForcesWithFriction(NumCast(this.dataDoc.coefficientOfStaticFriction), this.dataDoc.wedge_width, Math.tan(radAng) * this.dataDoc.wedge_width); } }; @@ -730,24 +695,8 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent { this.dataDoc.simulation_showComponentForces = false; - const gravityForce: IForce = { - description: 'Gravity', - magnitude: Math.abs(this.gravity) * this.mass1, - directionInDegrees: 270, - component: false, - }; - this.dataDoc.mass1_forcesUpdated = JSON.stringify([gravityForce]); - this.dataDoc.mass1_forcesStart = JSON.stringify([gravityForce]); + this.dataDoc.mass1_forcesUpdated = JSON.stringify([this.gravityForce(this.mass1)]); + this.dataDoc.mass1_forcesStart = JSON.stringify([this.gravityForce(this.mass1)]); this.dataDoc.mass1_positionXstart = this.xMax / 2 - 0.08 * this.props.PanelHeight(); this.dataDoc.mass1_positionYstart = 200; this.dataDoc.spring_constant = 0.5; @@ -792,14 +735,9 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent )} -
+
)}
-
+
{(this.simulationType == 'One Weight' || this.simulationType == 'Inclined Plane') && this.wallPositions?.map((element, index) => )}
@@ -1708,24 +1636,8 @@ export class PhysicsSimulationBox extends ViewBoxAnnotatableComponent { @computed get draggable() { return !['Inclined Plane', 'Pendulum'].includes(this.props.simulationType) && this.props.simulationMode === 'Freeform'; } + @computed get panelHeight() { + return Math.max(800, this.props.panelHeight()) + 'px'; + } + @computed get panelWidth() { + return Math.max(1000, this.props.panelWidth()) + 'px'; + } epsilon = 0.0001; labelBackgroundColor = `rgba(255,255,255,0.5)`; @@ -782,7 +788,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - + {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(val => { const count = 10; const xPos1 = this.state.xPosition + this.props.radius + (val % 2 === 0 ? -20 : 20); @@ -804,7 +810,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - +
@@ -818,7 +824,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - +
@@ -832,7 +838,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - + { left: 0, top: 0, }}> - + { left: 0, top: 0, }}> - + { left: 0, top: 0, }}> - + {!this.state.dragging && ( @@ -949,7 +955,7 @@ export default class Weight extends React.Component { {this.props.simulationType == 'Inclined Plane' && (
- +
@@ -973,7 +979,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - + @@ -1014,7 +1020,7 @@ export default class Weight extends React.Component { left: 0, top: 0, }}> - + @@ -1082,7 +1088,7 @@ export default class Weight extends React.Component { left: this.props.xMin, top: this.props.yMin, }}> - + @@ -1147,7 +1153,7 @@ export default class Weight extends React.Component { left: this.props.xMin, top: this.props.yMin, }}> - + -- cgit v1.2.3-70-g09d2