DEV Community

fakelaboratory
fakelaboratory

Posted on • Updated on

Physics: Newton's Laws of motion

Physics

  1. Force of gravity = mass * gravity acceleration
  2. Force of slide = force(gravity)*cos(plane angle)
  3. Force of friction(opposite to slide) = friction coefficient *force(gravity) * sin(plane angle)

Image description

Additional force(custom push) from top of view:
Image description

//horizontal application(from top of view) of setted in ui the force
cube.force_x = force.magnitude*Math.cos(Math.PI*force.angle/180+Math.PI)*-1;
cube.force_result = cube.force_slide + cube.force_friction + cube.force_x; 
cube.acceleration_x = cube.force_result/ cube.mass; // from F = m*a
cube.init_velocity_x = 0; // set start point

//vertical(from top of view) application of setted in ui the force
cube.force_y = force.magnitude*Math.sin(Math.PI*force.angle/180);
cube.acceleration_y = cube.force_y/ cube.mass; // from F = m*a
cube.init_velocity_y = 0; // set start point
Enter fullscreen mode Exit fullscreen mode

Start the simuluation by moving with the initial speed equal to zero, then with Δd=V(init)*time + (1/2)*acceleration*time^2 move points to delta and increase the init speed through acceleration.
The time is frame iteration, 1000/24, for example.


Code

Idea:
Image description
Sketch of UI:
Image description
Real screenshot:
Image description
Structure:
Image description

  • objects_render is for buffers, program, shaders of WebGL
  • glmjs are helping files, glmjs library(matrix operations) and helper file, where code for rotate object, move by glmjs.
  • objects_logic files of plane, cube, time, force...
plane {
   vertices; //array positions, colors
   angle; //degree
   inclane(angle) {
      //inclane plane and cube
      cube.inclane()
   } 
}
cube {
   vertices;
   mass; //kg
   build_physics(); //calculate forces and accelerations 
   inclane(); //also
}
force {
   vertices; //vizualiation, blue line
   angle; //degree
   magnitude; //newton
   inclane();
}  
time {
   //some coefficients and iter of time
}
Enter fullscreen mode Exit fullscreen mode

for example plane object:
Image description
As possible to see in the screenshot this simulation is using additional arrays for vertices, in example of the plane it is just a "backup" of original vertices, but for the cube: more easy just move cube in x,y(top of view),store this position and render it in another array with x,y + inclane...

Tried to test with real physics(I mean in the room with paper and matches) looks the similar. Also I added acceleration coefficient for "slow motion" because all happening so fast. But it is my philosophy interpretations...

Code here:
https://github.com/fakelaboratory/publish/tree/main/Newton's%20laws%20of%20motion
Test through browser:
https://fakelaboratory.github.io/publish/Newton's laws of motion/

Top comments (1)

Collapse
 
fakelaboratory profile image
fakelaboratory • Edited

Also have some idea about using the data in experiments:
play only one "preexperiment" with storing the data(time, what changing by time in each frame, frame like a matrices), then replay experiments with this stored data but with correction by coefficients...

For example we have a data about the angle 30 and the force 4 - without recalc of physics(by formulas), achieve new experiment but with "training" "restore" from data

px, py - positions
ax, ay - accelerations
*store them in the data(frames)

1 frame: px, py, ax, ay
2 frame: px, py, ax, ay
...
n   frame: px,    py,    ax,    ay
           k1*px, k2*py, k3*ax, k4*ay
n+1 frame: k1*px, k2*py, k3*ax, k4*ay
n+2 frame: k1*px, k2*py, k3*ax, k4*ay
...
m   frame: l1*k1*px, l1*k2*py, l1*k3*ax, l1*k4*ay
m+1 frame: l1*k1*px, l1*k2*py, l1*k3*ax, l1*k4*ay
...
Enter fullscreen mode Exit fullscreen mode

Add to n frame k1, k2, k3, k4(and for all next frames);
Add to m frame l1, l2, l3, l4(and for all next frames);
Then replay from data...

I mean maybe someone interested to connect AI to physics.