Skip to content

Commit c678eb5

Browse files
committed
View to model position scaling
1 parent eb1bf2e commit c678eb5

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

js/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let saturn = new Celestial({
3333
titan = new Celestial({
3434
name: "Titan",
3535
mass: 1.35e23,
36-
position: new Point(0, -389), // not to scale
36+
position: new Point(0, -1.187e9),
3737
velocity: new Vector(15.26e3, 0),
3838
size: 18, // not to scale
3939
});
@@ -42,3 +42,4 @@ model.scene.push(titan);
4242
view.loop();
4343
physics.loop();
4444
console.log(model);
45+
window.setTimeout(physics.stop, 100);

js/modules/DOMRenderer.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,17 @@ export default class DOMRenderer extends Renderer {
2727
* @override
2828
*/
2929
render() {
30-
const scene = this.model.scene,
31-
bounds = this.bounds,
32-
origin = {
33-
x: bounds.width / 2,
34-
y: bounds.height / 2,
35-
};
30+
const scene = this.model.scene;
3631
scene.forEach(
3732
/** @arg obj {Celestial} */
3833
(obj) => {
3934
// Make sure each Celestial has an Element
4035
const elem = obj.element || this.generateElement(obj),
4136
// Avoid repeating elem.style a whole bunch.
4237
style = elem.style,
43-
offset = 0 - obj.size / 2;
44-
style.top = `${origin.y + obj.position.y + offset}px`;
45-
style.left = `${origin.x + obj.position.x + offset}px`;
38+
position = this.getPosition(obj);
39+
style.left = `${position.x}px`;
40+
style.top = `${position.y}px`;
4641
style.width = `${obj.size}px`;
4742
style.height = `${obj.size}px`;
4843
}

js/modules/Physics.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default class Physics {
3131
static interval = 1e3 / 120;
3232

3333
/*----- Calculation Methods ------------------------------------------------*/
34+
/** Update `Celestial#position`s based on `Celestial#velocity` */
3435
updatePositions() {
3536
this.model.scene.forEach(
3637
/** @arg obj {Celestial} */

js/modules/Renderer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/*----- Imports --------------------------------------------------------------*/
88
import GameData from "./GameData.js";
9+
import Celestial from "./Celestial.js";
10+
import { Point } from "./Utils.js";
911

1012
/*----- Classes --------------------------------------------------------------*/
1113
/** @module Renderer Superclass for managing game view. */
@@ -19,6 +21,8 @@ export default class Renderer {
1921
this.model = model;
2022
/** @var {boolean} running Should the renderer continue running? */
2123
this.running = true;
24+
/** @var {number} scale Space scale (meters per pixel). */
25+
this.scale = 0.3e7;
2226
}
2327

2428
/*---- Setters and getters -------------------------------------------------*/
@@ -28,6 +32,27 @@ export default class Renderer {
2832
return { top, left, height, width };
2933
}
3034

35+
/*----- Functions ----------------------------------------------------------*/
36+
/**
37+
* Get the appropriate view position for a Celestial instance.
38+
* @param {Celestial} celestial
39+
* @returns {Point} View position for the Celestial.
40+
*/
41+
getPosition(celestial) {
42+
const bounds = this.bounds,
43+
// View origin point (0, 0)
44+
origin = new Point(
45+
bounds.width / 2,
46+
bounds.height / 2
47+
),
48+
// View position from scaled Celestial position
49+
position = celestial.position.copy().scale(1 / this.scale),
50+
// View offset position from Celestial origin
51+
offset = celestial.size / 2;
52+
// Return offset view position
53+
return origin.add(position).subtract(offset);
54+
}
55+
3156
/*----- Methods ------------------------------------------------------------*/
3257
/**
3358
* Sets the view container

js/modules/Utils.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,33 @@ export class Point {
3232
return this;
3333
}
3434
/**
35-
* Add another point to this point.
36-
* @arg {Point} point A point to add to this point
35+
* Add another point or number to this point.
36+
* @arg {Point|number} addend A point to add to this point
3737
* @returns {Point} The updated point.
3838
*/
39-
add(point) {
40-
this.x += point.x;
41-
this.y += point.y;
39+
add(addend) {
40+
if(typeof addend === "number"){
41+
this.x += addend;
42+
this.y += addend;
43+
} else {
44+
this.x += addend.x;
45+
this.y += addend.y;
46+
}
47+
return this;
48+
}
49+
/**
50+
* Subtract another point or number from this point.
51+
* @arg {Point|number} subtrahend A point to subtract from this point
52+
* @returns {Point} The updated point.
53+
*/
54+
subtract(subtrahend) {
55+
if(typeof subtrahend === "number"){
56+
this.x -= subtrahend;
57+
this.y -= subtrahend;
58+
} else {
59+
this.x -= subtrahend.x;
60+
this.y -= subtrahend.y;
61+
}
4262
return this;
4363
}
4464
/**

0 commit comments

Comments
 (0)