Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411500 Posts in 69373 Topics- by 58429 Members - Latest Member: Alternalo

April 25, 2024, 02:31:16 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Applying Thrust to an object with Inertia
Pages: [1]
Print
Author Topic: Applying Thrust to an object with Inertia  (Read 721 times)
quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« on: August 20, 2016, 09:49:27 AM »

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
 
Code:
   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:

Code:
	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 .
« Last Edit: August 20, 2016, 09:59:20 AM by quantumpotato » Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: August 20, 2016, 02:16:41 PM »

Don't use magnitude and heading for storing velocity. Use x_velocity and y_velocity instead (noahVX/Y). That format is much easier to work with. For instance, you can apply thrust simply by adding the x_thrust to x_velocity, and similarly for y.

Many other compuations are vastly easier as well, though you may need to study some more vector maths before they all become apparent.
Logged
quantumpotato
Quantum Potato
Level 10
*****



View Profile WWW
« Reply #2 on: August 20, 2016, 03:52:12 PM »

Don't use magnitude and heading for storing velocity. Use x_velocity and y_velocity instead (noahVX/Y). That format is much easier to work with. For instance, you can apply thrust simply by adding the x_thrust to x_velocity, and similarly for y.

Many other compuations are vastly easier as well, though you may need to study some more vector maths before they all become apparent.

Thanks for the tip! I was doing this before but moved to magnitude & heading as I thought it would be cleaner.. I have some experimenting to do!
Logged

Cheezmeister
Level 3
***



View Profile
« Reply #3 on: August 22, 2016, 11:20:41 PM »

Mmmm I wouldn't say storing vel as theta/magnitude is a *bad* thing per se, but it will certainly be harder to reason about...and you'll probably find yourself converting to cartesian a fair bit, anyway. You can't go wrong with simple x/y. I believe for Vec[0] I stored a rotation value in addition to position and velocity.

Writing (or finding/incorporating from a library) some basic vector arithmetic and conversions will make your life easier. I find that with a proliferation of pairs of x/y operations like you have above, typos are inevitable (not to mention typing the same thing twice gets old).

Have fun Smiley

Logged

෴Me෴ @chzmstr | www.luchenlabs.com ቒMadeቓ RA | Nextris | Chromathud   ᙍMakingᙌCheezus II (Devlog)
jasontomlee
Level 0
**



View Profile WWW
« Reply #4 on: August 29, 2016, 06:49:27 PM »

 Hmmmm Interesting indeed! Good ole physics & math Tongue
Logged

==== Creating simple Gamemaker assets & games ====



TWITTER: https://twitter.com/jasontomlee
ITCH:    https://jasontomlee.itch.io/
WEBSITE: http://jasontomlee.com/
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic