Programming Question

Command a squadron of spaceships
Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Programming Question

Post by Sketchy » Fri Apr 10, 2009 1:24 am

Ok, I realise this is probably pretty optimistic, but after a *lot* of searching and asking elsewhere, I haven't been able to find an answer yet...

Can anyone tell me how the steering part of Critical Mass works?

What I mean is, any idea how can you calculate the required rate of acceleration and turning required to reach the closest possible point to the mouse cursor?

If there are formulae, they must be quite complicated, as there are a lot of variables: Start X, Start Y, Start Speed, Start Angle, Target X, Target Y...

It can't just use "trial & error" - many combinations of acceleration and turning - as it wouldn't be possible to achieve the pixel-perfect results.
Image

User avatar
Legacy
Posts: 664
Joined: Thu Feb 17, 2005 4:34 pm
Location: Wellsboro, PA, USA
Contact:

Re: Programming Question

Post by Legacy » Fri Apr 10, 2009 1:57 am

Sketchy wrote:Ok, I realise this is probably pretty optimistic, but after a *lot* of searching and asking elsewhere, I haven't been able to find an answer yet...

Can anyone tell me how the steering part of Critical Mass works?

What I mean is, any idea how can you calculate the required rate of acceleration and turning required to reach the closest possible point to the mouse cursor?

If there are formulae, they must be quite complicated, as there are a lot of variables: Start X, Start Y, Start Speed, Start Angle, Target X, Target Y...

It can't just use "trial & error" - many combinations of acceleration and turning - as it wouldn't be possible to achieve the pixel-perfect results.


I'm willing to bet that Sean is using a fairly standard set of functions to draw the arc and calculate the end point.
"Every man is my superior in that I may learn from him."

Don't take life too seriously, you won't make it out alive!

umeboshi110
Posts: 139
Joined: Tue Oct 11, 2005 1:37 pm
Location: NY
Contact:

Post by umeboshi110 » Fri Apr 10, 2009 2:52 am

from what i can see, the acceleration is based on distance from the center of the ship , i.e. distance formula with the square root of the sum of the squares. the ship will have some set velocity, including the "speed" or the distance the ship will travel (which i suspect is measured in pixels), and direction. so the next turn's speed will be set to the new distance or as close as possible with a max difference equal to the thrust.
the direction is gotten from by calculating the angle from the direction the ship is facing. then i think this angle is multiplied by two to get how much the ship actually turns. of course i have no clue how critical mass calculates it, but the basic principles for different methods should be (nearly) the same. basically, it's polar coordinate stuff with x=r * cos(θ) and y=r * sin(θ), or θ = arccos(x/r) = arsin(y/r), where x is the difference in x-coordinates and y the difference in y-coordinates of the ship and mouse (mouse minus ship), r the distance from ship to mouse, and θ the angle between the ray moving to the right of the ship and the line from the ship to the mouse. this can be compared to the direction the ship is facing, or its current angle, to find the difference and then how much to turn by. the turn value shows the max number of degrees the ship can change direction its direction by each turn.
so with these, i imagine the total range a ship can fly into is like one of those slices of a slice of a pineapple. i hope that was understandable and that i'm as sane as i think i am. i'm not quite sure how to make the arc the ship travels though and too lazy to figure it out :p

i have another question though. the radar range of a ship. is it a rectangle or a nice ellipse or something done in polar coordinates using the distance to each side?

EDIT: it doesn't really matter, but now that i think about it, the "positive x-axis," or the ray pointing to the right, that i talked about could actually be pointing up because that's what it is in the mission editor and it may simplify some game mechanics, though it's easier to think in terms of the standard coordinate system they teach us with.

oh, oh! question two! so when you move, does the "radar range" move with the ship "realtime" or not, so can you sweep an area with strong radar on one side of the ship?

ah, yes, i just realized the arc can be made using the endpoints and the tangents.
(\ /)
(O.o)
(> <)

Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Post by Sketchy » Fri Apr 10, 2009 3:40 am

Thanks for the help - I'm going to take some time to digest it, and see what I can come up with.

As for the radar...

I'm pretty sure the range calculation uses the straight-line "euclidean" distance, but the radar components on each side cover just a 45 degree arc.

Should result in something like this:
Image

And yes, the radar coverage is continuously updated in realtime.
However, the first time you spot an enemy ship, if you then lose contact with it again by the end of the turn, the siren won't sound - so early in a mission be sure to check your radar at the end of every turn.
Image

User avatar
Fusion_power
Posts: 323
Joined: Fri Sep 03, 2004 2:34 pm
Contact:

Post by Fusion_power » Fri Apr 10, 2009 5:35 am

http://en.wikipedia.org/wiki/B%C3%A9zier_curve

You will find a few answers there.

DarJones

Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Post by Sketchy » Fri Apr 10, 2009 12:13 pm

Thanks for the suggestion guys :)

I understand how to move the ships, once I know the thrust and turning.
The problem is trying to figure out how much thrust and turning are actually needed.

Critical Mass seems to be the only game that calculates thrust & turning from a destination - other similar games, such as FlightCommander2, OverTheReich, and AchtungSpitfire, all require you to enter them directly (using an onscreen joystick & throttle) and then show you the resulting flight path.

It's no use finding the distance between the ship and the mouse - the actual distance travelled will vary depending on the rate of turning, as a straight line is going to be shorter than a curve. The same applies to the ships angle - they can't be treated as if they are independent of one another.

Also, it isn't enough to just pass through the destination - it must be reached after a precise amount of time (ie. the end of the turn).

Again, I can see how Bezier curves are useful for plotting specific points along the ships flightpath, but I don't see how you could extract thrust & turning from one.

If P0 = start, and P2 = target, how do you know P1?
Presumably, it would need to be a very high-order curve too, as a quadratic curve couldn't account for acceleration?
Image

User avatar
Fusion_power
Posts: 323
Joined: Fri Sep 03, 2004 2:34 pm
Contact:

Post by Fusion_power » Sat Apr 11, 2009 4:03 am

Bezier curves account for the full curve expression including turning, acceleration, and distance traveled. It is a lot easier to explain in polar coordinates. Go back and read the wiki and hone your math skills.

DarJones

Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Post by Sketchy » Sat Apr 11, 2009 12:41 pm

I've read that page (and others on B?zier Curves) many times, and given the 3 or more points, I understand how to draw a curve.

The problem is that I only know 2 points - P0 and P2. How do I find P1?
Image

Is P1 just the point you'd end up at without any rotation or acceleration? If so, I already got as far as drawing that curve.
Even having got a curve though, I don't understand how you can find acceleration & turning from it.

I thought maybe (if I have 50 frames per turn):
* P0 = start point
* P2 = end point (mouse)
* P1 = end point if turning & accel. = 0
* Q0 = 1/50th of the way from P0 to P1
* Q1 = 1/50th of the way from P1 to P2
* B = 1/50th of the way from Q0 to Q1
* Accel. = distance from P0 to B, minus original speed
* Turning = angle from P0 to B, minus original angle

Where have I gone wrong (or is just all completely wrong)?
Image

the space predator
Posts: 442
Joined: Fri Feb 06, 2009 11:09 pm
Location: Montr?al

Post by the space predator » Sat Apr 11, 2009 3:19 pm

It appears to be good

User avatar
Legacy
Posts: 664
Joined: Thu Feb 17, 2005 4:34 pm
Location: Wellsboro, PA, USA
Contact:

Post by Legacy » Sat Apr 11, 2009 6:16 pm

A little bit of fairly basic calculus should suffice...
"Every man is my superior in that I may learn from him."

Don't take life too seriously, you won't make it out alive!

Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Post by Sketchy » Sat Apr 11, 2009 9:09 pm

I'm pretty sure there's no such thing as "fairly basic calculus" :(
Image

User avatar
Legacy
Posts: 664
Joined: Thu Feb 17, 2005 4:34 pm
Location: Wellsboro, PA, USA
Contact:

Post by Legacy » Sun Apr 12, 2009 2:15 am

It's been a while, but taking derivatives isn't too bad.
"Every man is my superior in that I may learn from him."

Don't take life too seriously, you won't make it out alive!

umeboshi110
Posts: 139
Joined: Tue Oct 11, 2005 1:37 pm
Location: NY
Contact:

Post by umeboshi110 » Sun Apr 12, 2009 5:41 am

wow, so i was completely off (i just checked using a ruler on my screen too lol). however, i still believe that the turn is double the angle between the "no turn" line and the line between mouse and ship. just in case, i will mention that i'm pretty sure acceleration works instantaneously at the start of each turn and you will move each turn with a constant speed.

well, since you already know how to draw the curve figuring out the rest shouldn't be too hard. i can see that in the quadratic example there, P1 is most definitely not end point if turn/accel=0. it is basically the intersection between the line you would travel if turn=0 and the line you would be traveling in after the turn (extending backwards).
now, i am assuming you will want to approximate a circular arc as this is what it's like in CM as far as I can see. after some geometry/trig, the point P1 is a distance d=(r/2)/cos(θ) along the line of travel if turn=0, where θ = angle between line of travel if turn=0 and line between ship and mouse and r is distance from ship to mouse.

i'll add more stuff if i can figure it out
(\ /)
(O.o)
(> <)

sorare31
Posts: 9
Joined: Tue Apr 07, 2009 6:00 am

Post by sorare31 » Sun Apr 12, 2009 7:27 am

how is it that a ship with a tighter turn radius than, say, an orik still manage to get hit by one if fired from behind?

Sketchy
Posts: 73
Joined: Thu Nov 06, 2008 4:39 pm

Post by Sketchy » Sun Apr 12, 2009 12:31 pm

Sorare31:
Missiles have a much higher top speed than ships, and rather than trying to follow the path the ship takes, they can "cut corners" (although they can't "lead their target"). I'm pretty sure a modern AAM couldn't out-turn something like an F-16, but it can fly at Mach4+ so it doesn't need to.
Oriks fired from close range are easy to avoid because they haven't had time to get up to speed yet. Once they've been in the air for a couple of turns though, they become really very annoying.

Umeboshi110:
Thankyou so much - this is just the kind of detailed help I was hoping for :)

You're absolutely right about acceleration being instant, and speed constant. I was convinced that wasn't the case before, but I did some tests, and there's no doubt about it. It seems like that ought to make things easier?

I'm going to put that formula of yours into my program and see how it looks.

I have another question though:
Any thoughts on the best way to make a ship reverse, rather than trying to just turn really tightly?
Image

Post Reply