# How do I predict an orbiting object's trajectory and draw it

Get help using Construct 2

### » Sat Jan 10, 2015 12:14 pm

(Not enough space for a ? )

I'm working on a game where you fly around space in a ship, and I would like to draw a path showing your orbit. Do you have any tips?
B
3
Posts: 9
Reputation: 193

### » Sat Jan 10, 2015 12:49 pm

Do a search on the forums, there have been plenty of examlpes of similar requests.
B
52
S
16
G
9
Posts: 1,098
Reputation: 11,267

### » Sat Jan 10, 2015 2:35 pm

I searched around and didn't find anything similar to my question. Could you give me a link to one?
B
3
Posts: 9
Reputation: 193

### » Sat Jan 10, 2015 2:44 pm

It kinda depends on what your "orbit" is like - is it limited to actually orbiting around things? Is it a straight line, a curve?

If it's an actual orbit you could use this neat tutorial to see how to place other objects, say, dots around whatever it is you are orbiting around to show a trajectory: https://www.scirra.com/tutorials/1050/b ... lar-motion
B
19
S
6
G
7
Posts: 1,101
Reputation: 6,146

### » Sat Jan 10, 2015 2:54 pm

I should've explained more - there is a star system, with a central star and two planets orbiting it in more or less circular orbits, and you can freely fly around it, while the star and planets constantly affect your trajectory. I'll take a look at this tutorial, thanks!

E: Well, it didn't help much, as I use cos and sin to calculate my trajectory.
B
3
Posts: 9
Reputation: 193

### » Sat Jan 10, 2015 3:35 pm

Hey @McDonald,

One way you might approach it, is to run a loop every tick, to simulate the next several frames of motion updates on a "ghost" copy of your ship, dropping dots or particles as it goes, to draw the predicted orbital path.

How it would work
Suppose you have your "real" ship, and you have event code that describes how its position and velocity will be updated each tick (C2 frame). I'll call this event code the "update formula".

So, on a tick, you update the "real" ship position and velocity by running the update formula on it, once.

Now, suppose you also have a "ghost" ship. (The ghost ship will be invisible to the player.)

Right after you update the "real" ship's position and velocity, you set the ghost ship to the same position and velocity.

Now you run a loop with 100 iterations,
and in that loop, on each iteration you update the "ghost" ship, by running the update formula on it, and then you place a dot object at the ghost's current location.

Result
Each time you go through a loop iteration, you step the ghost forwards in time, exactly as if the real ship were continuing frame-by-frame on its trajectory.

At the end of the loop's 100 iterations, you have 100 dots tracing a predicted trajectory 100 frames into the future.

Pros & Cons
As a possible concern, depending on the complexity of the update formula, and the specs of the platform you're targeting with this game, this loop could be a rather intensive spot in your code, though on a typical computer I think it shouldn't be a problem at all.

That said, this method has the advantage of using the same update code you've already created for the "real" ship, and as a result, it will be an accurate simulation of the real ship's future motion, not including player control input, of course. :)

Optimizations
If you need to use fewer objects, you can always place a dot every other loop iteration, or every 5th iteration, etc.

Or you could use a canvas object and paste dots (or trace lines) directly into it, which has the advantage of using 1 dot object no matter how many iterations you do.
B
28
S
18
G
8
Posts: 333
Reputation: 6,193

### » Sat Jan 10, 2015 4:19 pm

I did what you suggested and the ghost doesn't update and follows the real ship instead. This is probably because the tick ends, ghost starts going forwards, but the next tick comes and resets the ghost back to ship's position. I added the loop as a sub-event if that matters.

The "Repeat 100 times" loop causes the ghost ship to just spawn a 100 times more dots in one place, so it seems to be a problem with updating the ghost.

Although now that I think of it, I need player actions to be accounted for instantly, so this isn't the best solution.
B
3
Posts: 9
Reputation: 193

### » Sat Jan 10, 2015 7:34 pm

B
32
S
7
G
3
Posts: 22
Reputation: 4,813

### » Sat Jan 10, 2015 8:08 pm

Here's an example of fisholith's idea with some random control scheme. It works quite well.
You do not have the required permissions to view the files attached to this post.
B
95
S
34
G
128
Posts: 5,493
Reputation: 81,674

### » Sat Jan 10, 2015 9:02 pm

Eh, I tried adding it to my game (then just straight up copy-pasting it), but it didn't help. Thanks for help, anyway.
B
3
Posts: 9
Reputation: 193

Next