What equation is used to calculate a Spaceship's desired angle of engine burn to reach a stationary point?

Started by
12 comments, last by taby 2 months, 2 weeks ago

What equation should be used to calculate the required angle, Theta, that a spaceship should use to burn it's engine in order to reach a stationary point, P, in space. Gravity and drag can be ignored. Note, burning the engine in the direction of the point is not always the most efficient angle to use as the Spaceship may already have some velocity.

A spaceship is moving in the XY-plane at some velocity and would like to alter it's trajectory in order to reach a stationary point. The following information is known:

  • Location of the spaceship, e.g. Rs = <10, 10>
  • Velocity of the spaceship, e.g. Vs = <35, 2>
  • Location of the point, e.g. Rp = <300, 200>
  • Acceleration of the spaceship (when firing in the desired angle to reach Rp), e.g. A = 42 m/s
  • Maximum speed of the spaceship, e.g. Ss = 1000 m/s

I think the equations of motion should be used to figure out the desired equation to calculate Theta, but that is where it all becomes Greek to me. Using the information above, what is the formula for calculating the most efficient angle to burn the Spaceship's main engine in order to reach the desired point?

Advertisement

gazumpt said:
Gravity and drag can be ignored.

Then you don't have any external force, and thus the most efficient path towards the goal is simply the straight line towards the goal.

If there would be (constant) gravity, we could use the same formula used to calculate the launch angle of a projectile to hit a goal under gravity. Although this formula assumes the projectile has no thrusters to control itself, that's still good enough for a counter example. The point is, the constant external force turns a the straight line defined by initial launch velocity into a parabola, and we calculate launch angle so the resulting parabola intersects the goal.

But if there is no external force, there is nothing to affect the shortest path of a straight line, so you could use just that.

I think you need to add other objectives to formulate a optimization problem.
For example minimizing the need of fuel required for turning the spaceship towards the goal.
Say we can generate a generate a angular acceleration of x while keeping the requirement on fuel ‘low enough’.
Then you could define:

Current angular velocity.
Max angular acceleration of x.
Current error angle, which is the angle from current forwards direction to the desired direction towards the goal.

Then you could calculate the time when you need to switch thrusters from generating a counter clockwise rotation while accelerating towards the desired direction,
to a clockwise acceleration to decelerate the rotation so the ship stops at the desired angle with a velocity of zero.

At this point the ship is aligned and can use it's main thrusters to generate linear acceleration to start moving.
And we can do the same here: Introduce a max linear acceleration we can apply while ‘reducing fuel well enough’.
Than calculate the time where we switch from accelerating to decelerating, so we finally stop at the goal with a linear velocity of zero (or some given target non zero velocity at the goal).

This problem has an analytical solution. I have worked it out, but the code might be ugly and hard to read. But i can post if you want it.

But ofc. there is no ‘ideal low amount of fuel’ given from that. The answer to that would be simply ‘use an amount of zero fuel, and never start moving at all’. That's why you need to introduce some given constants to define the maximum allowed acceleration and deceleration.

I would actually solve both of those problems each frame, because you can start with linear movement instantly, and you do not need to wait until the ship has the desired angle from rotating. This would give a curved path then, since the ship is both rotating and moving at the same time. IT should look natural and the behavior would also be predictable to the player.

@joej thank you for the detailed response! I think I'm understanding what you're suggesting. I've drawn a quick picture to help illustrate my thoughts a bit better. I hope 🙂. My objective would be to burn the spaceship's engine at an angle that would most quickly align the trajectory of the spaceship with the target point.

JoeJ said:
But if there is no external force, there is nothing to affect the shortest path of a straight line, so you could use just that.

Agreed, there is no external force, but shouldn't the initial velocity of the spaceship be taken into consideration in order to cancel it out as quickly as possible, rather than pointing directly at the target point without considering the current trajectory?

The spaceship is travelling at some initial constant velocity with zero acceleration. The engine is off. The spaceship can then rotate in any direction before turning the engine on. Is rotating the spaceship to face the target point really the most efficient angle to use to minimize the amount of time to reach the point? What I'm thinking is that in order to cancel out the current velocity and align with the direction of the target as quick as possible, an angle, theta in the image below, greater than directly pointing to the point would be best. The spaceship would be trying to cancel the bad velocity (the current velocity in the wrong direction compared to the point) as quick as possible while aligning with the point. Once the spaceship's trajectory is inline with the target point, the engine would turn off.

gazumpt said:
Agreed, there is no external force, but shouldn't the initial velocity of the spaceship be taken into consideration in order to cancel it out as quickly as possible, rather than pointing directly at the target point without considering the current trajectory?

Yes. I have assumed the spaceship is initially ‘stationary’ from reading the thread title i guess. But this does not affect my proposal.

gazumpt said:
The spaceship can then rotate in any direction before turning the engine on. Is rotating the spaceship to face the target point really the most efficient angle to use to minimize the amount of time to reach the point?

With my proposal you would not have to worry about that. The ship would just constantly adjust (or optimize) it's ‘strategy’ to reach the goal, treating angular and linear movements independently from each other. And it does so while respecting your given allowed acceleration. Personally i use the method to model natural behavior in ragdoll simulations, where bounding acceleration is important to remain balanced, but minimizing energy is not important - the goal rather is to come to the goal as fast as possible.

So my point about minimizing fuel was somewhat wrong. It's more about fast predictable motion needed for an action game to be fun, i would say.

I'm very optimistic it's fine to control space ships, and i'm sure the guys at NASA use it too for a lot of things.
A very useful property is that it also gives the duration until hitting the target, which is helpful to predict the future, as often needed in control problems.

However, since linear and angular properties change independently, i guess there is no analytical prediction about trajectory anymore. Maybe there is a good guess though. But if you need trajectories without having to integrate a mini simulation, i would rather propose some method based on splines. Might depend on if your game is an action game or some kind of RTS.

Otherwise, both players and AI should do fine with controlling spaceships that way.
Your further thoughts seemingly go into the same direction.
But i'll do a simple example, to point out what actually matters:

Just the angular part. It looks up but it wants to look at the right arrow. Initial and target velocities are zero.

The green arrows show the acceleration phase, the red arrows the deceleration phase. Giving both the same value, the turning point will be in the middle for this simple example. But doing the math, it always works even if initial and target velocities are not zero, and if acc. / dec. have different values.
It's quadratic equations of motion, as often appearing when planing about gravity.

But still quite complicated, and not super easy to use.
Sometimes i use ‘critical damping' instead, which is super simple and gives similar results in many cases.

As far as I know, with no gravity, and no friction (because you are in space), how do you turn?

if it is a spaceship I assume it should be similar to gas thrusters? if that is the case, (I mean, you are not using real gas but you are modeling it via a mathematical vector and a visual/sound effect) then you have one vector (gas thrusters) and you have your original velocity vector, and you have your goal direction vector. If those three vectors are on a plane, then it is as easy as solving a 7th grade equation:

S + tR  = P
3 vectors, S is the spaceship current velocity vector, P is the ideal trajectory and tR is a multiple of your rotating vector.

S is your spaceship velocity vector, R is your thruster vector, P is your target point, and you have to solve for t.
if in case your R vector is not part of S-P plane, then you can use your thrusters (with primitive linear algebra) to first go into that plane, and then use the equation above.

There are few special cases that worth handling, but first I wanted to be sure I understand the problem correctly.

BTW, if you are looking strictly for angles. then this should show you how to calculate them, this is your ideal angle (in your picture).

and this is your angle directly towards the point:

Basic trigonometry should show suffice to calculate it. (I still prefer the vectors method, with no angles at all).

None

@AliAbdulKareem yes, this is what I think I'm after! Thank you! Do you know what the equation is named or the name of the general concept of these types of calculations? I'd love to be able to research more about them to get a better understanding. It's been over 20 years since I learned these basic concepts.

In the S + tR = P equation, how do I calculate R and t? Do I need another equation? Two unknowns, two equations. I am trying to calculate the R required (or the angle at which the thruster acceleration should be applied).

Your assumptions about being on the same plane and using thrusters is correct. That is the scenario I attempted to describe.

In the S + tR = P equation, how do I calculate R and t? Do I need another equation? Two unknowns, two equations. I am trying to calculate the R required (or the angle at which the thruster acceleration should be applied).

No you don't any other equation, I assumed you are familiar with vectors. In the simplest case to calculate this, assume t = 1 and then your equation becomes:

R = P - S;

(of course, R, P and S all are vectors, not scalar values, so this addition/subtraction is actually component wise)

Now, the reason I introduced t was not to confuse you, but rather the fact, adding R directly, will make your spaceship jumps to the point P immediately (this should be obvious since what we calculated was the exact vector that goes from S to P). You usually want to move in that direction a bit by bit, as if you were rotating, that is where t comes into play, by specifying a small value, it acts as if you are rotating, here is a picture showcasing how changing the value from t = 0 to t = 1 Affects the spaceship:

With S being what your spaceship was originally pointing towards. So to summarize, you calculate R then you select a small number t = 0.1 for example and then you add 0.1*R to your Position (technically you should add that to your Velocity Vector, and not the position but I guess it will also work in this scenario). Now, I should mention, you will face some surprising behavior if this the first time you are working with vectors (but I am sure you will figure them out easily). But one particular situation I should note is this:
I am assuming you are doing something the following in your code:

Spaceship.Position += Velocity * DeltaTime; //updating the position every frame

That means if you add a multiple of R using t = 0.1 for example, by the next frame, you would have changed your position by moving in the original spaceship Velocity direction, and you have to recalculate your R, this problem is easily solved with a bit of extra math, but if you are unfamiliar with vector arithmetic and normalization, I would suggest you just calculate the angles as in the previous answer from trigonometry:

//No warranty this will work, I coded it at 1am, but the idea should be correct. 
vector2d R = P - S;
float ThetaR = arctan(R.y, R.x); 
float ThetaS = arctan(S.y, S.y); 
float IdealTheta = ThetaS - ThetaR; // (this is clockwise, for anti-clockwise, flip the sign of the angle)

One reason I hate this (and hate working with angles in general) is that if you are moving in the up direction S.x = 0 then that division by zero is a bit annoying, but if you are using a robust math library, you should be fine, I heard (though, I never tested it myself) in C++ atan2 does handle this scenario (at least if you believe this stackoverflow). Then you simply interpolate the this angle to smoothly rotate towards your target.

An alternative way to calculate the angle, can be obtained from dot product, by using arccosine function. Provided that, there are a few more ways to calculate the angle in a 2D-plane such as here and here.

Another possible scenario you will run into if you where going to use Vectors, is that if the target point was exactly in opposite direction of your spaceship movement direction, it will fail. because you will not rotate, the vector equation will gives
R = -S while in angle method, you will get angle = 180.0f which would work (there are few other small traps with vectors but again I assume you will find them easy to fix).

gazumpt said:
I'd love to be able to research more about them to get a better understanding. It's been over 20 years since I learned these basic concepts.

If you would like to read about it, it is just basic vector arithmetic, any linear algebra book/course should cover these in the very first few lectures. Personally I would recommend the following if you want a refresher:

The first few lecture of professor Norman Wildberger discusses vectors in a very intuitive/nice way, and honestly the best intro in my opinion (If I remember correctly, the first 3 lectures covers those things very well).
One thing to note is that he does discuss the dot product till the end of course (he has a good reason for doing so though, but it is impractical in your case).

Another quick primer for vectors, is given in chapter one of the book Introduction to linear algebra 5th edition by Gilbert Strang (I have personally used the 5th edition, its a great, but pretty sure the 3rd edition will cover it as well, and it is available online for free) and in the second chapter of this good free book online (which is geared towards game developers).
I don't want to overwhelm you, so my recommendation is start with Norman, and if you feel you need more, use the other two.

None

When you say “reach” a point, does that mean arrive with velocity close to 0, or is it okay to zoom by the point at high speed?

@alvaro absolutely fine to zoom by at speed. I'm working on a waypoint system and would like the ship to find the best angle (or vector) to minimize time to the point, taking into consideration it's current velocity.

I think I figured it out, at least if there is no maximum speed. We are going to always accelerate at full throttle in the chosen direction. In order to solve for the direction, we will first figure out the time t it will take us to get to the target.

The key to solving the problem is to realize that we can decompose how much we move as the sum of the acceleration part and how much we would move if we didn't accelerate (Vs*t). This means that, at time t, the point Rs+Vs*t will be at a distance 0.5*A*t^2 from Rp. We can use the dot product of a vector with itself to compute the square of the distance, and then we get this equation in t:

dot(Rs+Vs*t-Rp,Rs+Vs*t-Rp) = (A/4)*t^4

I will take Rp to be the origin, to make things a bit easier to expand:

dot(Rs+Vs*t,Rs+Vs*t) = dot(Rs,Rs) + 2*dot(Rs,Vs)*t + dot(Vs,Vs)*t^2 = (A/4)*t^4

So we get a 4th-degree polynomial equation in t:

(A/4)*t^4 - dot(Vs,Vs)*t^2 - 2*dot(Rs,Vt)*t - dot(Rs,Rs) = 0

Find the smallest positive solution, and now you can compute the direction Rp-Rs+Vs*t, which is the direction in which you should accelerate.

I can walk you through an example if any of this doesn't make sense.

Oh, and I don't quite know how to incorporate a maximum speed.

This topic is closed to new replies.

Advertisement