X-post from gamedev.stackexchange
I'm using
http://www.mathguide.com/lessons2/Vectors.html#combining as a reference. Their magnitude values are too large for my pixels so I'm decreasing by a factor of 100 - so the 30 magnitude becomes 0.3 and the 40 magnitude becomes 0.4.
I have my ship positioned at x = 100, y = 100;
I set its Inertia to equal `theta = 130, magnitude = 0.3`.
Given angle of 0 as being straight up on the screen, the ship moves with inertia towards the bottom right, which is expected.
Everything I have in my `applyThrust` function matches closely with the example code until the very last lines.
I set the new resulting Vector with `theta` & `magnitude` as the Ship's new `Inertia`.
I expect that when the thrust is not active, the Ship continues to move in the bottom right.
Instead, the Ship moves towards the top right - the same angle it moves when thrust is held down.
I think the problem lies in how I'm assigning the new resulting vector to the existing Inertia.
What I'm trying to achieve is:
A ship left alone with no thrust held - continues moving as its `Inertia` dictates.
When Thrust is held, the ship applies thrust towards the theta of its `.r` attribute.
When Thrust is released, the ship continues moving with its new Inertia.
I've uploaded a sample project to Github.
Inertia is defined in engine.js line 257
class Inertia extends Component {
constructor(o) {
super(o);
this.mass = o['mass'];
this.theta = o['theta'];
this.magnitude = o['magnitude'] ? o['magnitude'] : 0;
this.thing = o['thing'];
}
loop() {
var thing = this.thing;
var origin = {'x' : thing.x, 'y': thing.y};
var tp = [thing.x, thing.y - 1];
var np = rotate_point(tp[0], tp[1], origin.x, origin.y, this.theta);
thing.mx = np.x - thing.x;
thing.my = np.y - thing.y;
var p = this.magnitude;
thing.x = thing.x + thing.mx * p;
thing.y = thing.y + thing.my * p;
}
}
Apply Thrust:
applyThrust() {
var t1 = this.ship.r; //rotation, the theta the ship is facing
var m1 = this.ship.grab('thruster').power;
//Without this if statement, the ship will move towards top right
//Even when thrust is not active!
if (m1 > -0.1 && m1 < 0.1) {
return;
}
console.log(m1);
//m1 is 0 when gamepad button is not active.
//m1 is 0.4 when the gamepad thrust button is active.
var inertia = this.ship.grab('inertia');
var t2 = inertia.theta;
var m2 = inertia.magnitude;
// console.log(m2);
//Kim is Inertia
//Noah is Thrust
var kimVX = (m2 * Math.cos(t2 * Math.PI / 180));
var kimVY = (m2 * Math.sin(t2 * Math.PI / 180));
console.log("Inertia Vector.x" + kimVX);
console.log("Inertia Vector.y" + kimVY);
// return;
var noahVX = (m1 * Math.cos(t1 * Math.PI / 180));
var noahVY = (m1 * Math.sin(t1 * Math.PI / 180));
console.log("Thrust Vector.x:" + noahVX);
console.log("Thrust Vector.y:" + noahVY);
// return;
var resultVX = kimVX + noahVX;
var resultVY = kimVY + noahVY;
console.log("Resulting Vector.x" + resultVX);
console.log("Resulting Vector.y" + resultVY);
// return;
//So far the console.logs match within 2 decimal places of the example
//
var c = resultVX * resultVX + resultVY * resultVY;
console.log(c); //Expected to equal ~0.3522 since the example is 3522.25
//and magnitudes are scaled down by a factor of 100
// return;
if (c != 0) {
var resultMagnitude = Math.sqrt(c);
console.log("Resulting vector magnitude" + resultMagnitude);
// return;
var resultTheta = (Math.atan( (resultVY) / (resultVX) )) * 180 / Math.PI ;
console.log(resultTheta);
// return;
inertia.magnitude = resultMagnitude;
inertia.theta = resultTheta;
}
}
The `Ship` is defined in `pilots.js` and is instantiated in `game.js`.
It is represented as a blue circle in `draw.js`.
https://github.com/quantumproducer/sample_vector/The game can be run from game.html .