Skip to content

Commit 4786f26

Browse files
committed
Add hitbox intersect checks
1 parent c73bf60 commit 4786f26

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

js/modules/Celestial.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export default class Celestial {
1818
* @arg {Vector} [options.velocity] Velocity vector in meters/second.
1919
* @arg {Vector} [options.acceleration] Acceleration vector in meters/second^2
2020
* @arg {number} [options.mass=0] Mass in kilograms.
21+
* @arg {string} [options.hitBox="CIRCLE"] Shape of the hit box
2122
* @arg {number} [options.size=0] Width or diameter in meters.
22-
* @arg {ImageData} [options.texture] The texture for rendering.
23+
* @arg {string} [options.texture] The path of a texture for rendering.
2324
*/
2425
constructor(options) {
2526
/*----- Metadata ---------------------------------------------------------*/
@@ -35,13 +36,13 @@ export default class Celestial {
3536
this.velocity = options.velocity || new Vector(0, 0);
3637
/** @var {number} mass Mass in kilograms. */
3738
this.mass = options.mass || 0;
38-
/** @TODO Add HitBox class */
39-
this.hitBox = null;
39+
/** @var {string} hitBox Shape of the hit box */
40+
this.hitBox = options.hitBox || "CIRCLE";
4041

4142
/*----- Rendering --------------------------------------------------------*/
4243
/** @var {number} size Width or diameter in meters. */
4344
this.size = options.size || 0;
44-
/** @var {ImageData} texture The texture for rendering. */
45+
/** @var {string} texture The texture for rendering. */
4546
this.texture = options.texture || null;
4647
}
4748
}

js/modules/Physics.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,36 @@ export default class Physics {
2626
/*----- Constants ----------------------------------------------------------*/
2727
/** @const {number} G Gravitational constant. */
2828
static G = 6.67e-11;
29-
/** @const {number} timeScale Time scale (seconds per loop). */
30-
static timeScale = 0.5e3;
31-
/** @const {number} interval Interval time for physics loop. */
32-
static interval = 1e3 / 120;
29+
/** @const {number} TIME_SCALE Time scale (seconds per loop). */
30+
static TIME_SCALE = 0.5e3;
31+
/** @const {number} INTERVAL Interval time for physics loop. */
32+
static INTERVAL = 1e3 / 120;
3333

34-
/*----- Calculation Methods ------------------------------------------------*/
35-
/** Update `Celestial#position`s based on `Celestial#velocity` */
36-
updatePositions() {
37-
this.model.scene.forEach(
38-
/** @arg obj {Celestial} */
39-
(obj) => obj.position.add(obj.velocity.copy().scale(Physics.timeScale))
40-
);
34+
/*----- Calculation Functions ----------------------------------------------*/
35+
/** @const {Object} INTERSECT_CHECKS Intersect function store. */
36+
static INTERSECT_CHECKS = {
37+
CIRCLE: {
38+
CIRCLE:
39+
/**
40+
* @arg {Celestial} a
41+
* @arg {Celestial} b
42+
* @returns {boolean}
43+
*/
44+
(a, b) => a.position.vectorTo(b.position).magnitude > a.size + b.size,
45+
},
46+
};
47+
/**
48+
* Check for intersections between two Celestial instances
49+
* @arg {Celestial} a
50+
* @arg {Celestial} b
51+
*/
52+
checkIntersection(a, b) {
53+
return Physics.INTERSECT_CHECKS[a.hitBox][b.hitBox](a, b);
4154
}
4255
/**
4356
* Calculate gravitational acceleration between two Celestial instances
44-
* @param {Celestial} m_1 Celestial to apply acceleration to.
45-
* @param {Celestial} m_2 Celestial applying the acceleration.
57+
* @arg {Celestial} m_1 Celestial to apply acceleration to.
58+
* @arg {Celestial} m_2 Celestial applying the acceleration.
4659
* @returns {Vector} Gravitational acceleration Vector.
4760
*/
4861
gravitate(m_1, m_2) {
@@ -54,6 +67,15 @@ export default class Physics {
5467
between.magnitude = 1;
5568
return between.scale((Physics.G * m_2.mass) / r ** 2);
5669
}
70+
71+
/*----- Calculation Methods ------------------------------------------------*/
72+
/** Update `Celestial#position`s based on `Celestial#velocity` */
73+
updatePositions() {
74+
this.model.scene.forEach(
75+
/** @arg obj {Celestial} */
76+
(obj) => obj.position.add(obj.velocity.copy().scale(Physics.TIME_SCALE))
77+
);
78+
}
5779
/** Update `Celestial#velocity`s based on gravitational acceleration */
5880
updateVelocities() {
5981
this.model.scene.forEach(
@@ -75,7 +97,7 @@ export default class Physics {
7597
new Vector(0, 0)
7698
)
7799
// Velocity from acceleration
78-
.scale(Physics.timeScale)
100+
.scale(Physics.TIME_SCALE)
79101
)
80102
);
81103
}
@@ -85,7 +107,7 @@ export default class Physics {
85107
loop() {
86108
this._intervalId = window.setInterval(
87109
this.step.bind(this),
88-
Physics.interval
110+
Physics.INTERVAL
89111
);
90112
console.log(`Physics running on loop ${this._intervalId}`);
91113
}

0 commit comments

Comments
 (0)