Question for Math and/or Physics People

Fishlore

Tank
Joined
Jan 21, 2004
Messages
1,270
Reaction score
1
I'm writing a computer program. Let's say I have a two dimensional spaceship in a two dimensional (X,Y) grid. It is probably moving and a pilot is probably trying to control it.

Two questions... What spaceship variables do I need to keep track of in order to predict its future position? Using these variables, what equations do I need to solve to predict the spacecraft's position in 1/32 of a second in the future?

For question 1, I figure I'll need at least the following variables...

current (X,Y)
mass
velocity
angle of velocity
thrust
angle of thrust

anything else? maybe length of time thrusting?

For question 2, what equations do I plug those values into in order to get the new (X,Y), or the change in current (X,Y)?


Is there a place online to learn about this stuff? Can anyone here help me? Any help would be appreciated.

Thanks in advance.
 
use protoniun based engines,everyone uses the admantiun but protoniuns based engines work for that stuff

to plug the protoniun engines just ask your ship's chief of engineering
 
F=ma ???

It's been a few years lol.

But if the "thrust" is expressed as a force, then I would go with F = ma, plug in for F and m, to get acceleration. Velocity is like... integral of acceleration with respect to time. Distance is double integral of acceleration w.r.t. time. So to get position (X,Y), I'd suggest taking a definite (from t_initial=0 to t_final=1/32 sec) double integral of the acceleration a=F/m (assuming thrust is a force) over time. Maybe????

[edit] lol, rjmc

[edit2] Wait no it's a little more complicated, if you already have an initial velocity in some direction, you need to account for that also. Can't be bothered to think the equations through but it shouldn't be ultra-difficult. Just add on some crap for the motion accrued in whatever x-y direction the ship was initially moving in due to initial velocity and whatnot :p. G'luck.

[edit3] Also up there I assumed the same thrust was applied over the whole 1/32 sec. If not, change accordingly. :p

p.s. i hated physics.
 
It's probably easiest to use vectors for velocity and acceleration.

Basically you need two pieces of information. The initial velocity and current force applied to the object. As dfc05 said, if you have an initial value and you know the force values you can solve for acceleration, velocity and position.
 
You know...I could probably explain this to you if I had a digital classroom pretty easily, but I don't. So go to my physics teacher's website. His notes are pretty good at making physics understandable. This is probably the closest any of his notes come to directly addressing your issue. I could calculate what you're asking, and show you my work. You have to give me all the parameters in actual numbers, though.
 
This is my proposed situation:
Phil the monkey is being taken by space ship pilot Slim Jim to a black hole to be destroyed. If the ship is moving at 300 m/s at 43* from position 0,0. If Slim Jim fires his thrusters (F of thrust is 60,000N) on his 1000kg space ship at 312*, what is Slim Jim and Phil's final position after 1/32 of thrust?

If that sounds about right, I have some physics calculations that I worked out that should give you a pretty good idea what's going on. They're too big to post up here, though. I need an email or something.
 
Is that an instantanous thrust? If not you can use the ideal rocket equation to model the velocity increase as fuel is burned.

Other than that it looks like a momentum and impulse problem.
 
hey man just find the mass convert it into energy its all cool
 
angular momentum and initial angular orientation.

because without the orientation of you space ship, angle of thrust is meaningless.

And perhaps local gravitational field.

F = m dv/dt ; t = I dw/dt ; r = dv/dt ; w = d(theta)/dt

four equations, done.
 
I'd look into 2D vector math. Doing this stuff with angles is a pain, vectors make it much easier. You'll still need them for rendering, but for the calculations you should definitely stick to vectors. Applying thrust to an object from another direction than it's already moving is simply adding the thrust vector to the current movement vector. All you need is the ship's position (as a 2D point, but you'll end up using the 2D vector data structure for this as well, for convenience), its mass and its current movement vector (this contains both velocity, which is the length of the vector, as well as it's direction). Making the ship move then is just a matter of adjusting that vector and then adding that vector to the ship's position to actually move it. To adjust that movement vector, you'll mostly be adding other vectors to it. Like the acceleration vector when you press a button, and determining that can indeed be done with F=ma (or rather a = F/m), which will give you the length of the vector. The direction of that vector is the current rotation of the thrusters, which can also be expressed as a vector (with length 1, so you can just multiply it with the length of the acceleration vector to get a vector that you can add to the ship's movement vector).

Anyway, keyword: vectors.
 
I'm serious when I say I have the exact equations he needs written on a piece of paper and scanned into a couple jpgs.
 
In the spoiler is some working Javascript for a "ship" that flies towards your mouse to get you started:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script type="text/javascript">

    var Vector = function (x, y) {
      this.x = x;
      this.y = y;
    };

    Vector.add = function (v1, v2) {
      return new Vector(v1.x + v2.x, v1.y + v2.y);
    };

    Vector.subtract = function (v1, v2) {
      return new Vector(v1.x - v2.x, v1.y - v2.y);
    };

    Vector.multiply = function (v, scalar) {
      return new Vector(v.x * scalar, v.y * scalar);
    };

    Vector.prototype.normalize = function () {
      var num2 = (this.x * this.x) + (this.y * this.y);
      var num = 1 / Math.sqrt(num2);
      this.x *= num;
      this.y *= num;
    };

    var Ship = function (pos) {
      this.position = pos;
      this.movement = new Vector(0, 0);
      this.thrustDirection = new Vector(0, 0);
    };

    Ship.accelerationForce = 2;

    Ship.mass = 1000;

    Ship.prototype.applyThrust = function () {
      var a = (Ship.accelerationForce / Ship.mass);
      this.movement = Vector.add(this.movement, Vector.multiply(this.thrustDirection, a));
      this.position = Vector.add(this.position, this.movement);
    };

    Ship.prototype.draw = function (ctx) {

      ctx.strokeStyle = "black";
      ctx.lineWidth = 1;
      ctx.beginPath();
      ctx.moveTo(this.position.x + 5, this.position.y + 5);
      ctx.lineTo(this.position.x + 5 + (this.thrustDirection.x * 25), this.position.y + 5 + (this.thrustDirection.y * 25));
      ctx.stroke();
      ctx.fillStyle = "rgb(0, 150, 225)";
      ctx.fillRect(this.position.x, this.position.y, 10, 10);
    };

    var mousePos = new Vector(0, 0);

    var Game = function () {

      this.ship = new Ship(new Vector(100, 100));
      this.ctx = do[FONT="Courier New"]cument.g[/FONT]etElementById('canvas').getContext('2d');
      var self = this;
      setInterval(function () {

        var dir = Vector.subtract(mousePos, self.ship.position);

        dir.normalize();

        self.ship.thrustDirection = dir;

        self.ship.applyThrust();

        self.draw();
      }, 16.667);
    }

    Game.prototype.draw = function () {
      this.ctx.fillStyle = "rgb(220, 220, 220)";
      this.ctx.fillRect(0, 0, 500, 500);
      this.ship.draw(this.ctx);
    };

    document.onmousemove = function (event) {
      mousePos = new Vector(event.clientX, event.clientY);
    };

    window.onload = function () {
      var game = new Game();
    };
  
  </script>
</head>
<body>
  <canvas id="canvas"  ></canvas>
</body>
</html>
 
I REALLY, REALLY appreciate the replies you guys have left. I'm going to spend some time going through the replies. Maestro some of those links are really helpful. PvtRyan the code you left is also really helpful. Fantastic starting points.

All in all this is a fantastic help. I'll be back later with some questions. Thanks again, I appreciate it so much.
 
I'm a physics person! :D

I don't know much about programming but I can do my best to provide all the vector equations.

I'll go through the stuff you already have just for completeness

You obviously need the X,Y position to start off. This vector I will call R.
You also need the mass of the spaceship, M.

Then we have the linear motion functions. The first thing we have is velocity, V. Like position velocity will have a separate X and Y component. You say that you're calculating the changes once ever 1/32 seconds so I'll just call that tick T.
So if your position is R and your velocity is V then then your position at the next tick (I'll call this R') will be
R' = R + VT

The velocity for the next tick will be determined by the acceleration, A, by
V' = V + AT

The acceleration for for ship will be determined by the force, F
F = MA

Where I guess F will be determined by player controls.



Now we have the angular functions. So the current angle of the spaceship I'll call S. The angular stuff works much like the linear stuff (except that they're scalars, not vectors) so using angular velocity W we get:
S' = S + WT

And angular acceleration B:
W' = W + BT

And the angular acceleration is determined by force F
F = IB

Where I is the moment of inertia of the ship which acts like the angular mass.


Note that the angular equations will only apply when the force is not through the centre of mass of the ship, so should play no part when you're using the main thruster (which I assume is at the back of the ship). On the other hand if you want realistic motion the angling thrusters should affect both the linear and angular velocity, although for gameplay purposes it would probably be better if they affected the angular much more than the linear.

M and I are also pretty arbitrary so just set them as fits gameplay.
 
These replies are great. I'm learning a lot.

My first question, hope it makes sense and isn't stupid..

I'm tracking the direction my spaceship is pointed (rotation) in degrees. So if it's pointing straight up it's rotation is 0, down 180, etc. How can I turn this into a thrust direction vector?


Not sure if this is needed/relevant or not, but let's say for testing purposes accelerationForce = 2 and mass = 1000...

a = accelerationForce / mass

.. oh and let's say I'm at (0, 0) and rotation is 30 degrees.

Thanks everyone!!!
 
x component is cos(angle)
y component is sin(angle)
 
Careful where you set 0, mathematics and physics both set 0 on the positive X axis, and count anticlockwise from there.
 
x component is cos(angle)
y component is sin(angle)

How would I calculate this vector if I wasn't at (0,0)? I need to take into account my current location right?

So if I was at (5,5), rotated 30 degrees and I wanted to find my thrust vector what would I do?

Sorry if I sound dense. It's been decades since I took a math class.
 
You can just calculate whatever movement you get as if you were at (0,0), e.g. let's say you determine it moves +2 in x and +3 in y. Then just add that with your initial position so you have (5,5) + (2,3) = (7,8).
 
Okay I'm looking for my thrust vector, so if I'm at (5,5) and my rotation is 30 degrees...

cos(30) = .86
sin(30) = .5

Are these the right values? I get an X,Y of (.86, .5). Then I add to my current vector of (5, 5)...

to get...
(5.86, 5.5)

And this would be my thrust vector?


This doesn't seem right to me because if I had a rotation of 90 degress at (0,0)

cos(90) = 0
sin(90) = 1

I get (0,1) which isn't at 90 degrees. Wouldn't that be 180 degrees? Do I have my units screwed up or something?
 
Your thrust vector is relative to your spaceship, not absolute space. A 30 deg thrust vector is the same no matter what your position is. Only your position vector can be shown as a position. Your velocity vector and thrust (acceleration) vector are not on the same coordinate grid. You don't measure them in metres or units of distance. If they were shown on the coordinate map for some reason, they would be plotted relative to your spaceship, with your spaceship at (0,0).

If you are at (5,5), that is your position vector. Your velocity vector is the rate of change of your position vector with respect to time. You can't add velocity to position. It's like adding 50 miles per hour to 100 miles and saying that you have now travelled 150 miles.

To figure out the math, start with only 1 dimension. It makes it much simpler. Assume that mass is constant, so you can ignore it. Now you just need to keep track of position and velocity as properties of the space ship. Acceleration or thrust is an instantaneous property, so you don't have to store it necessarily. Your thrust now is independent of your thrust 5 second ago.

All of the one dimensional equations of motion are easily found on wikipedia or any number of websites. Learn how to use them in one dimension first. For two dimensions, everything is almost exactly the same except that you do it twice. The only thing linking the two dimensions in your case is your thrust vector.

You said that straight up is a rotation of 0 degrees right. So 90 degrees is straight right. In this case x is sin(90) and y is is cos(90). (Standard is typically 0 degrees along the x-axis). This gives you (1,0) which is pointing out straight right.
 
Thanks Dan, sorry for my ignorance in all of this. Another question for you. Your first post said cos(angle) is the X component and sin(angle) is the Y component. In your last post you have X = sin(angle) and Y = cos(angle).

How do I know when to use sin and when to use cos for the X/Y components?

Thanks.
 
Hopefully this image explains sin and cos.

675px-Sin_Cos_Tan_Cot_unit_circle.svg.png
 
Back
Top