myPhysicsLab Documentation

Getting Collision Data

There are several myPhysicsLab simulations that include collisions between objects. To get collision data use the setCollisionFunction method to specify a function which prints the data to the Terminal. (Instead of printing, you could collect the data into a Javascript array for later processing).

Each collision simulation has its own particular collsion class. Here are steps for the CollideBlocks simulation, other collision simulations are described below.

Open the terminal window by clicking the “terminal” checkbox. (This screenshot is from the Double Pendulum simulation, but CollideBlocks is similar).

 

Type help in the command box (and hit return of course) to see available Terminal commands.

Set your desired initial conditions on the simulation.

Paste into the Terminal command box this script

sim.setCollisionFunction(function(c,t) {
  const s = c.getDetectedTime().toFixed(2)+"\t"
    +c.getImpulse().toFixed(2)+"\t"
    +c.rightBlock_.getPosition().getX().toFixed(2)+"\t"
    +c.leftBlock_.getName()+"\t"
    +c.rightBlock_.getName();
  t.println(s);
})

To see it working try this link.

Hit the “play” button to start the simulation running. You should see output like this

0.44	5.19	3.00	BLOCK1	BLOCK2
1.51	5.19	7.20	BLOCK2	WALLRIGHT
2.81	3.73	2.20	BLOCK1	BLOCK2
3.34	4.16	1.69	BLOCK1	BLOCK2
3.92	3.19	2.73	BLOCK1	BLOCK2
4.95	5.89	7.20	BLOCK2	WALLRIGHT
5.93	3.37	2.76	BLOCK1	BLOCK2
6.54	4.37	1.74	BLOCK1	BLOCK2
7.11	3.72	2.45	BLOCK1	BLOCK2
8.26	5.57	7.20	BLOCK2	WALLRIGHT

As you can tell from the code the order is:

There are tab characters between each field "\t", which makes it easy to paste into a spreadsheet. You can of course change the formatting as you want by modifying the code.

Collision Types and Collision Simulations

There are various collision simulations and each has it’s particular collision type with different information available. Check the documentation or source code to find how to get the data you want from a particular collision type.

CollideBlocks

See above for example code for the CollideBlocks simulation. The collisions are instances of BlockCollision which contain block objects that are instances of PointMass.

RollerFlight

For the RollerFlight simulation here is some sample code

sim.setCollisionFunction(function(c,t) {
  const s = c.getDetectedTime().toFixed(2)+"\t"
    +c.getImpulse().toFixed(2)+"\t"
    +c.getPathPoint().getX().toFixed(2)+"\t"
    +c.getPathPoint().getY().toFixed(2);
  t.println(s);
})

To see it working try this link.

The collisions are instances of RollerCollision which contain instances of PathPoint.

Engine2D

Many myPhysicsLab simulations use the Rigid Body Physics Engine. Some examples are BilliardsApp, FastBallApp, PileApp and others. When you type sim in the Terminal command box, you will usually see that the sim is a ContactSim or less often an ImpulseSim. Here is some sample code that works with these simulations

sim.setCollisionFunction(function(c,t) {
const s = c.getDetectedTime().toFixed(2)+"\t"
  +c.getDistance().toFixed(5)+"\t"
  +c.getImpulse().toFixed(2)+"\t"
  +c.getPrimaryBody().getName()+"\t"
  +c.getNormalBody().getName();
t.println(s);
})

To see it working in the FastBall simulation try this link

The collisions are instances of RigidBodyCollision which contain instances of RigidBody.

Molecule Apps

For the Molecule2 and Molecule3 simulations here is some sample code

sim.setCollisionFunction(function(c,t) {
  const s = c.getDetectedTime().toFixed(2)+"\t"
    +c.getImpulse().toFixed(2)+"\t"
    +c.atom.getPosition().getX().toFixed(2)+"\t"
    +c.atom.getPosition().getY().toFixed(2)+"\t"
    +c.atom.getName()+"\t"+c.side;
  t.println(s);
})

To see it working in the Molecule2 simulation try this link

The collisions are instances of MoleculeCollision which contain instances of PointMass.