Skip to content

Commit

Permalink
Add planet and rocket code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowserinator committed Jul 9, 2018
1 parent 3bdd1d2 commit de16afc
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 33 deletions.
6 changes: 6 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Fix rotation along the center

Optimize planets
split int osections or something

Set current rocket as controllable
rocket.control = true;

Expand Down Expand Up @@ -25,6 +30,7 @@ Apply forces
drag
gravity

rotate camera to align down with planet

Properly set masses and densities from the rocket parts
Remove fuel only from attached fuel tanks
Expand Down
1 change: 1 addition & 0 deletions src/components/physical-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PhysicalSprite extends ImageSprite {
constructor(image_path, width, height, body) {
super(image_path, width, height);
this.body = body;
this.skip_add_body = false; // Should it skip adding the body (Ie it's part of a compound object)
}

/**
Expand Down
31 changes: 30 additions & 1 deletion src/game-components/planet.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';

const config = require('../game/config.js');

/**
* This file acts like a template for a planet/moon. Extend
* this class and override all variables
*/
class Planet {
constructor() {
constructor(x, y) {
this.position = {x: x, y: y};

this.orbital_e = 0; // Eccentricity
this.orbital_distance = 0;
this.rotation_speed = 0;
Expand Down Expand Up @@ -35,4 +39,29 @@ class Planet {
getHeight: angle => 1
};
}

createBody() {
this.body = Matter.Bodies.circle(this.position.x, this.position.y, this.radius);
Matter.Body.setStatic(this.body, true); // Planets are static bodies
}

applyGravity(rocket) {
// Calculate force magnitude to apply
let x1 = rocket.position.x;
let y1 = rocket.position.y;
let x2 = this.position.x;
let y2 = this.position.y;

let f_mag = config.G_CONSTANT * 1 * this.mass / ((x1 - x2) ** 2 + (y1 - y2) ** 2);

// Calculate force direction to apply
let angle = Math.atan2(y2 - y1, x2 - x1);
console.log(angle, angle * 180 / Math.PI)
console.log(f_mag)
console.log({x: f_mag * Math.cos(angle), y: f_mag * Math.sin(angle)})

rocket.applyForceToAll({x: f_mag * Math.cos(angle), y: f_mag * Math.sin(angle)});
}
}

module.exports = Planet;
4 changes: 3 additions & 1 deletion src/game-components/rocket-part.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class RocketPart extends PhysicalSprite {
let body = Matter.Bodies.rectangle(x, y, width, height);
super(image_path, width, height, body);

this.skip_add_body = true;

/* Data that should be overrided by another
* class that extends this class */
this.id = null;
Expand All @@ -41,7 +43,7 @@ class RocketPart extends PhysicalSprite {
* },
* volume: <number>, Volume in m^3
* density: <number>, Density in kg/m^3
* description: <string>
* description: <string>
* }
*/
this.data = data;
Expand Down
25 changes: 23 additions & 2 deletions src/game/bodies/earth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@
const Planet = require('../../game-components/planet.js');

class Earth extends Planet {
constructor() {

constructor(x, y) {
super(x, y);

this.radius = 1000;
this.mass = 100000;

this.createBody();
}

addToStage(PIXI, stage) {
let graphics = new PIXI.Graphics();

graphics.beginFill(0xe74c3c); // Red
graphics.drawCircle(this.position.x, this.position.y, this.radius);
graphics.endFill();

stage.addChild(graphics);
}

update() {
//Pretend to be a physicalsprite
}
}

module.exports = Earth;
5 changes: 4 additions & 1 deletion src/game/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

module.exports = {
// Build grid
build_grid_size: 50
build_grid_size: 50,

// Physics
G_CONSTANT: 20
};
33 changes: 22 additions & 11 deletions src/game/rocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ class Rocket {
constructor(parts, Matter) {
this.parts = parts;
this.control = false; // Can it be controlled?
this.center_pos = {};
this.position = {};

this.comp = Matter.Composite.create({});

this.body = Matter.Composite.create({});
for (let part of this.parts) {
part.rocket = this;
Matter.Composite.add(this.body, part.body);
Matter.Composite.add(this.comp, part.body);
}

Matter.Events.on(this.body, 'afterAdd', this.updateCenterPos);
Matter.Events.on(this.body, 'afterRemove', this.updateCenterPos);
this.body = Matter.Body.create({parts: this.parts.map(x => x.body)});

Matter.Events.on(this.comp, 'afterAdd', this.updateCenterPos);
Matter.Events.on(this.comp, 'afterRemove', this.updateCenterPos);
this.updateCenterPos();
}

Expand All @@ -24,27 +27,35 @@ class Rocket {
* @param {Vector} vector Force vector, in format {x: num, y: num}
*/
applyForceToAll(vector) {
for (let body of this.body.bodies) {
Matter.Body.applyForce(this.body, this.body.position, vector);
/* for (let body of this.comp.bodies) {
Matter.Body.applyForce(body, body.position, vector);
}
} */
}

getPos() {
this.updateCenterPos();
return this.center_pos;
return this.position;
}

updateCenterPos() {
let avg_x = 0;
let avg_y = 0;

for (let body of this.body.bodies) {
for (let body of this.comp.bodies) {
avg_x += body.position.x;
avg_y += body.position.y;
}

this.center_pos.x = avg_x / this.body.bodies.length;
this.center_pos.y = avg_y / this.body.bodies.length;
this.position.x = avg_x / this.comp.bodies.length;
this.position.y = avg_y / this.comp.bodies.length;
}

update() {
// Make sure rotation is consistent
for (let body of this.comp.bodies) {
body.angle = this.body.angle;
}
}
}

Expand Down
33 changes: 20 additions & 13 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Scene = require(path.resolve(appPath, './src/scene.js'));
const Camera = require(path.resolve(appPath, './src/ui/camera.js'));



const Earth = require(path.resolve(appPath, './src/game/bodies/earth.js'));
const PhysicalSprite = require(path.resolve(appPath, './src/components/physical-sprite.js'));

const Rocket = require(path.resolve(appPath, './src/game/rocket.js'));
Expand All @@ -29,19 +29,13 @@ let blocks = [
new FuelTank(90, 400),
new FuelTank(90, 350),
new FuelTank(90, 300),
new Thruster(90, 450)
//new Thruster(90, 450)
];


let current_scene = new Scene(
blocks,
[],
null,
'test-level'
);

let earth = new Earth(0, -1000);
let rocket = new Rocket(blocks, Matter);


const camera = new Camera();

/* Alias for matter.js */
Expand All @@ -57,21 +51,34 @@ let stage, renderer;
let engine = Engine.create();


let current_scene = new Scene(
blocks,
[],
[rocket.body, earth.body],
[rocket],
null,
'test-level'
);




let far;

function init(){
resetAll();

current_scene.load(stage, World, engine);

World.add(engine.world, []);
engine.world.gravity.y = 0; // Disable gravity
current_scene.load(stage, World, engine); // Load the current scene (Add all objects)
World.add(engine.world, []); // Init the current world

// Start the scene
Engine.run(engine);
renderer.render(stage);
requestAnimationFrame(update);

// Add all the planets
earth.addToStage(PIXI, stage);
}


Expand Down
19 changes: 16 additions & 3 deletions src/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class Scene {
* @param {null} background Currently unused
* @param {null} name Currently unused
*/
constructor(physical_sprites, sprites, background, name) {
constructor(physical_sprites, sprites, bodies, updatable, background, name) {
this.name = name;
this.physical_sprites = physical_sprites;
this.sprites = sprites;
this.bodies = bodies;
this.updatable = updatable;
}

/**
Expand All @@ -36,14 +38,21 @@ class Scene {
// Add physical sprites
for(let physical_sprite of this.physical_sprites) {
stage.addChild(physical_sprite.sprite);
bodies.push(physical_sprite.body);
if (!physical_sprite.skip_add_body) {
bodies.push(physical_sprite.body);
}
}

// Add non-physical sprites
for(let sprite of this.sprites) {
stage.addChild(sprite.sprite);
}

// Add matterjs bodies
for(let body of this.bodies) {
bodies.push(body);
}

// Add all physical objects
world.add(engine.world, bodies);
}
Expand All @@ -54,9 +63,13 @@ class Scene {
* will be run.
*/
update() {
for(let physical_sprite of this.physical_sprites) {
for (let physical_sprite of this.physical_sprites) {
physical_sprite.update();
}

for (let updatable of this.updatable) {
updatable.update();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Camera {
constructor() {
this.x = 0;
this.y = 0;
this.scale = 1;
this.scale = 0.2;
this.filter = null;
}

Expand Down

0 comments on commit de16afc

Please sign in to comment.