- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
import {Vector} from "./Vector";
/**
* A physical interaction is a force causes object positions to be re-evaulated each frame based on
* force's magnitude.
*/
export const PhysicalInteraction = (() => {
class PhysicalInteraction {
constructor(objects) {
objects[0].interactions.push(this);
objects[1].interactions.push(this);
this.objects = objects;
}
/**
* This method should be overriden to calculate the force at the given instant.
*
* @return {Vector<3>}
*/
force() {
return Vector.for3D().ZERO;
}
}
const EuclideanSpace = {};
/**
* @static
* @member {number}
*/
PhysicalInteraction.GRAVITATIONAL_CONSTANT = 1;
// TODO: webdoc shouldn't document this class as it isn't returned by the initializer function
/** This class shouldn't be documented! */
class Gravity extends PhysicalInteraction {
force() {
const massProduct = (this.objects[0].mass * this.objects[1]);
const radialDistance = EuclideanSpace.calculateDistance(
this.objects[0].position * this.objects[1].position);
return PhysicalInteraction.GRAVITATIONAL_CONSTANT *
massProduct / (radialDistance * radialDistance);
}
}
/**
* Creates a graviational force
*
* @static
* @method
* @param {Tuple<PhysicalObject, 2>} objects
* @return {PhysicalInteraction}
*/
PhysicalInteraction.createGravity = function(objects) {
return new Gravity(objects);
};
return PhysicalInteraction;
})();