-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysObject.js
executable file
·50 lines (48 loc) · 1.79 KB
/
PhysObject.js
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
import { Vector2 } from './Vector2.js';
/**
* 2 dimensional physics object that can be affected by forces.
*/
export class PhysObject {
/**
* Create PhysObject
* @param {Vector2} position Position of the object in pixels. Defaults to (0.0, 0.0).
* @param {Vector2} velocity Velocity of the object. Defaults to (0.0, 0.0).
* @param {number} gravity Acceleration of the object due to gravity. Defaults to 0.0.
* @param {Vector2} acceleration Acceleration of the object. Defaults to (0, 0).
* @param {number} mass Mass of the object. Defaults to 1.0.
* @param {boolean} collidesWithWalls Whether the object will collide with the canvas edges.
* @param {boolean} collidesWithObjects Whether the object will collide other objects.
* @param {boolean} isStatic Whether the object is static, i.e., doesn't move.
*/
constructor(
position = new Vector2(0, 0),
velocity = new Vector2(0, 0),
acceleration = new Vector2(0, 0),
gravity = 0,
mass = 1,
drag = 0,
collidesWithObjects = true,
collidesWithWalls = true,
isStatic = false
) {
this.position = position;
this.gravity = gravity;
this.velocity = velocity;
this.acceleration = acceleration;
this.mass = mass;
this.drag = drag;
this.collidesWithObjects = collidesWithObjects;
this.collidesWithWalls = collidesWithWalls;
this.onObjectCollison = function () {};
this.onWallCollision = function () {};
this.isStatic = isStatic;
}
/**
* Applies a force to the object. The "force" will remain until an equal and opposite
* force is applied or the acceleration is manually changed.
* @param {Vector2} force Vector2 representing the force to be applied.
*/
applyForce(force) {
this.acceleration = this.acceleration.add(force.scalarDiv(this.mass));
}
}