This is a problem I've been trying to figure out, on-and-off for about the last 3 years (I've even posted to ask for help before). Anyway, here goes...
For anyone who's played Sean O'Connors "Critical Mass", I'm trying to replicate the mouse control system from that game.
I have two points - point A, and point B.
I have a spaceship which starts at point A, and has to travel to point B.
The spaceship has an initial heading, turns at a constant rate, and moves at a constant speed.
Because of this, the spaceship will move in a circular arc, as shown in the diagram below.
What I need to be able to find is:
* The length of the arc (drawn in red) - from which I can get the speed to move
* The angle "f" - from which I can get the rate of turn
As you can see, I've tried to do the maths myself, but it's not working out right.
Can anyone spot the flaw(s) in my logic?
I'm also not sure how I'm going to make it work with reverse speeds...
btw: Bezier curves are no good for this, because I will want to limit the maximum turning rate and acceleration.
Any help very much appreciated, as this has been holding me back for ages
Sorry, that was just a diagram to help illustrate the maths.
In the game, the spaceship (point A) moves & rotates, and you drag point B with the mouse.
Taking a quick glance at the drawing:
arc length= R * c (in radians)
the angle f = c
Which is what you wrote next to it I see. I don't see any mistakes, so it's best to check for other errors. Like radians/degree screwups or calcluting the angle in the wrong quadrant.
Thanks.
You were right - after some careful inspection of my code, I noticed there was a "* 2" missing.
I don't use radians at all - that's the only thing stopping me from doing it all in Lua (the trig functions in Lua use radians, and it's too much hassle converting back and forth).
Anyway, that's the easy part done. Now just need to add turn/acceleration limiting, and make it work for reverse movements aswell...
Originally Posted by Sketchy (the trig functions in Lua use radians, and it's too much hassle converting back and forth)
well you could always just redefine math.cos and math.sin to use degrees:
do
local degtorad = math.pi / 180
local oldsin, oldcos = math.sin, math.cos
function math.sin (angle)
return oldsin(angle * degtorad)
end
function math.cos (angle)
return oldcos(angle * degtorad)
end
end
near the top of your code... or you could just define new cos/sin functions. whatever
EDIT: also i think the code tag needs fixed, the line spacing is ridiculous
Dude, use radians. It's a lot easier than degrees. Once you figure out radians, you'll never go back to degrees, it just makes more sense.
2 pi = full circle (360 deg). 1 pi = semi-circle(180 deg). pi/2 = quarter of a circle (90 deg). Don't calculate the value.. just use pi as a constant.
To get angles, think of the starting point as pointing right. The angle is counted anti-clockwise. So, from your starting point pointing up, turn your head so that it points right. Final heading is pointing downwards... it's like three quarters of a circle.
So, f = 3/4 * 2*pi radians [3/4 of circle]
If you want to calculate it clockwise,
f = -1/4 * 2*pi radians [1/4 of circle, clockwise]
For AB:
Circumference = 2 * pi * radius
Part of a circumference = 2 * pi * Section * radius
Length of AB = 2*pi*(c/2*pi)*radius
Length of AB = c*radius [assuming c is in radians]
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
Pshh.. if you're lazy, learn radians. Seriously, it's a lot easier. 360 degrees is some arbitrary number. There's no reason or purpose for it, it's not even an easy range of values to guess. I don't get why you guys put so much effort in avoiding it, it only takes like 10 minutes to learn
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
one big draw of using degrees rather than radians in this case is that radians are floating-point, while in most cases degrees will be integers. integers are nice because they don't suffer precision problems* like floating-point generally does. of course that's not to say you'll never use decimal degrees, but 360 degrees is usually enough for most purposes.
you could get around radian precision issues in lua by making an "exactRadian" object or something that actually stores the numerator and denominator as integers, and when you need the decimal value it will calculate the (n*pi)/d value on the fly. of course this is probably more effort than it's worth since fraction -> float conversion is easy but a (likely inexact) float -> fraction conversion is somewhat more difficult. you'd pretty much need to find/implement a whole fraction-manipulation system to make these more than just slightly usable.
mathematically radians are more elegant but this isn't always the case when trying to work with them in code
* they will if they overflow their range but this isn't likely to happen given how huge a double's integer range is and how small the degree range is
I could teach you in 10 minutes if I had a piece of paper. I'm pretty sure they can teach it, along with wherever you learn angles. Just that the teachers are too used to degrees. It's even easier than Centigrades and meters, because it's completely instinctive Maybe I'll write an article for you guys later with pictures and stuff.
Eh, floating point gets very accurate. MMF uses doubles, which is way more accurate. It's pixel perfect to at least 1000 pixels away, at least with my engine. Way more accurate than you get from just 360 degrees. The code in MMF/Lua should handle overflow and stuff, so it shouldn't happen.
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
Originally Posted by Muz I could teach you in 10 minutes if I had a piece of paper. I'm pretty sure they can teach it, along with wherever you learn angles. Just that the teachers are too used to degrees. It's even easier than Centigrades and meters, because it's completely instinctive Maybe I'll write an article for you guys later with pictures and stuff.
Eh, floating point gets very accurate. MMF uses doubles, which is way more accurate. It's pixel perfect to at least 1000 pixels away, at least with my engine. Way more accurate than you get from just 360 degrees. The code in MMF/Lua should handle overflow and stuff, so it shouldn't happen.
well when you're dealing with floating point, .1 + .2 may or may not equal .3
it's true in many cases that loss of precision probably isn't going to hurt you but when you add == and ~= into the mix, things change