Skip to main content
Mathjax
Source Link
Jesse Dorsey
  • 2.9k
  • 4
  • 34
  • 59

|p - c|^2 = r^2$$ |p - c|^2 = r^2 $$

With a line expressed as p = p0 + μv\$p = p0 + \mu v\$ (where v is a vector, http://en.wikipedia.org/wiki/Euclidean_vector), you insert the line into the circle formula and get

|p0 + μv - c|^2 = r^2$$|p0 + \mu v - c|^2 = r^2$$

(p0 + μv - c)•(p0 + μv - c) = r^2$$(p0 + \mu v - c)•(p0 + \mu v - c) = r^2$$

Define a = c - p0\$a = c - p0\$ and rewrite to (μv - a)•(μv - a) = r^2\$(\mu v - a)•(\mu v - a) = r^2\$

Perform the dot product and we get μ^2(v•v) - 2μ(a•v) + a•a = r^2\$\mu ^2(v•v) - 2\mu (a•v) + a•a = r^2\$

Assume that |v| = 1\$|v| = 1\$ and we have

μ^2 - 2μ(a•v) + |a|2 - r^2 = 0$$\mu ^2 - 2\mu (a•v) + |a|2 - r^2 = 0$$

*μ = a • v +- sqrt((a • v)^2 a^2 – r^2)$$\mu = a • v +- sqrt((a • v)^2 *a^2 – r^2)$$

If μ < 0\$\mu < 0\$, the line of the ship in your case does not intersect with the planets orbit.

If μ = 0\$\mu = 0\$, the line of the ship will simply touch the circle in one point.

Otherwise, this gives us two μ\$\mu \$-values that corresponds to two points on the orbit!

So we can define a line for the ship, and out of that we get either 0, 1 or 2 μ\$\mu \$-values. If we get 1 value, use that one. If we get 2, simply choose one of them.

p = p0 + μv\$p = p0 + \mu v\$ gives us the coordinate, and the μv\$\mu v\$-component gives us how far is will have to travel. Simply divide this last component with the speed of your ship to get how much time it will take for it to get there!

x = c + r*cos(θ)$$x = c + r*cos(θ)$$

y = c + r*sin(θ)$$y = c + r*sin(θ)$$

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*angularVelocity\$t*angularVelocity\$ degrees in it's orbit, and we are done!

|p - c|^2 = r^2

With a line expressed as p = p0 + μv (where v is a vector, http://en.wikipedia.org/wiki/Euclidean_vector), you insert the line into the circle formula and get

|p0 + μv - c|^2 = r^2

(p0 + μv - c)•(p0 + μv - c) = r^2

Define a = c - p0 and rewrite to (μv - a)•(μv - a) = r^2

Perform the dot product and we get μ^2(v•v) - 2μ(a•v) + a•a = r^2

Assume that |v| = 1 and we have

μ^2 - 2μ(a•v) + |a|2 - r^2 = 0

*μ = a • v +- sqrt((a • v)^2 a^2 – r^2)

If μ < 0, the line of the ship in your case does not intersect with the planets orbit.

If μ = 0, the line of the ship will simply touch the circle in one point.

Otherwise, this gives us two μ-values that corresponds to two points on the orbit!

So we can define a line for the ship, and out of that we get either 0, 1 or 2 μ-values. If we get 1 value, use that one. If we get 2, simply choose one of them.

p = p0 + μv gives us the coordinate, and the μv-component gives us how far is will have to travel. Simply divide this last component with the speed of your ship to get how much time it will take for it to get there!

x = c + r*cos(θ)

y = c + r*sin(θ)

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*angularVelocity degrees in it's orbit, and we are done!

$$ |p - c|^2 = r^2 $$

With a line expressed as \$p = p0 + \mu v\$ (where v is a vector, http://en.wikipedia.org/wiki/Euclidean_vector), you insert the line into the circle formula and get

$$|p0 + \mu v - c|^2 = r^2$$

$$(p0 + \mu v - c)•(p0 + \mu v - c) = r^2$$

Define \$a = c - p0\$ and rewrite to \$(\mu v - a)•(\mu v - a) = r^2\$

Perform the dot product and we get \$\mu ^2(v•v) - 2\mu (a•v) + a•a = r^2\$

Assume that \$|v| = 1\$ and we have

$$\mu ^2 - 2\mu (a•v) + |a|2 - r^2 = 0$$

$$\mu = a • v +- sqrt((a • v)^2 *a^2 – r^2)$$

If \$\mu < 0\$, the line of the ship in your case does not intersect with the planets orbit.

If \$\mu = 0\$, the line of the ship will simply touch the circle in one point.

Otherwise, this gives us two \$\mu \$-values that corresponds to two points on the orbit!

So we can define a line for the ship, and out of that we get either 0, 1 or 2 \$\mu \$-values. If we get 1 value, use that one. If we get 2, simply choose one of them.

\$p = p0 + \mu v\$ gives us the coordinate, and the \$\mu v\$-component gives us how far is will have to travel. Simply divide this last component with the speed of your ship to get how much time it will take for it to get there!

$$x = c + r*cos(θ)$$

$$y = c + r*sin(θ)$$

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back \$t*angularVelocity\$ degrees in it's orbit, and we are done!

added 14 characters in body
Source Link
Tholle
  • 200
  • 7

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*θt*angularVelocity degrees in it's orbit, and we are done!

Choose a line for your ship, and run the math to see if it collides with the planets orbit. If it does, calculate the time it will take to get to that point. Use this time to go "backback in time"orbit from this point with the planet to calculate where the planet should be when the ship starts moving.

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*θ degrees in it's orbit, and we are done!

Choose a line for your ship, and run the math to see if it collides with the planets orbit. If it does, calculate the time it will take to get to that point. Use this time to go "back in time" with the planet to calculate where the planet should be when the ship starts moving.

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*angularVelocity degrees in it's orbit, and we are done!

Choose a line for your ship, and run the math to see if it collides with the planets orbit. If it does, calculate the time it will take to get to that point. Use this time to go back in orbit from this point with the planet to calculate where the planet should be when the ship starts moving.

Source Link
Tholle
  • 200
  • 7

Let's start off by taking a look at the math behind the problem.

Step 1:

Finding the intersection between a line and a shape is just a matter of inserting the equation of the line in the equation of the shape, which is a circle in this case.

Line intersecting with circle

Take a circle with center c and radius r. A point p is on the circle if

|p - c|^2 = r^2

With a line expressed as p = p0 + μv (where v is a vector, http://en.wikipedia.org/wiki/Euclidean_vector), you insert the line into the circle formula and get

|p0 + μv - c|^2 = r^2

The squared distance can be rewritten as a dot product (http://en.wikipedia.org/wiki/Dot_product).

(p0 + μv - c)•(p0 + μv - c) = r^2

Define a = c - p0 and rewrite to (μv - a)•(μv - a) = r^2

Perform the dot product and we get μ^2(v•v) - 2μ(a•v) + a•a = r^2

Assume that |v| = 1 and we have

μ^2 - 2μ(a•v) + |a|2 - r^2 = 0

which is a simple quadratic equation, and we arrive at the solution

*μ = a • v +- sqrt((a • v)^2 a^2 – r^2)

If μ < 0, the line of the ship in your case does not intersect with the planets orbit.

If μ = 0, the line of the ship will simply touch the circle in one point.

Otherwise, this gives us two μ-values that corresponds to two points on the orbit!

Step 2:

So we can define a line for the ship, and out of that we get either 0, 1 or 2 μ-values. If we get 1 value, use that one. If we get 2, simply choose one of them.

What can we do with this? Well, we now know the distance the ship has to travel and what point it will end up in!

p = p0 + μv gives us the coordinate, and the μv-component gives us how far is will have to travel. Simply divide this last component with the speed of your ship to get how much time it will take for it to get there!

Now, all that is left to do is to calculate where the planet should be when the ship begins coming towards it's orbit. This is easily calculated with so called Polar coodinates (http://mathworld.wolfram.com/PolarCoordinates.html)

x = c + r*cos(θ)

y = c + r*sin(θ)

And since you had the speed of you ship, and we have the time it will take for the ship to reach the orbit, and where it will collide, we simply move the planet back t*θ degrees in it's orbit, and we are done!

Summary

Choose a line for your ship, and run the math to see if it collides with the planets orbit. If it does, calculate the time it will take to get to that point. Use this time to go "back in time" with the planet to calculate where the planet should be when the ship starts moving.