Post #161,982
6/28/04 5:56:00 PM
|
What I'm up to.
I'm rewriting "Asteroids" as part of a school project, forthehellofit.
While vector math is plain simple for most of the critters, figuring in ship thrust and getting it right is tricky. As a bonus, I'm implementing a "turtle" to do all the animation for the various objects, so the rocks will lazily spin as they work their way across the screen - no prerendered graphics at all.
Very simple graphics, mind you...
Tired of lying in the sunshine staying home to watch the rain. You are young and life is long and there is time to kill today. And then one day you find ten years have got behind you. No one told you when to run, you missed the starting gun.
|
Post #161,983
6/28/04 6:22:11 PM
|
Simple optimization:
Which happens more, new position calculation or thrusting?
If it's thrusting, use the method you've currently come up with.
If it's position calculation, then model your speed/direction vector as an X/Y tuple instead of direction/speed, and add/subtract to X and Y when thrust is applied. This way you're only doing the sin and cos calcs when thrust is applied (probably infrequently) instead of every movement tick.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #161,986
6/28/04 6:37:47 PM
|
That's exactly what I'm doing.
Vectors are an x/y pair, complete with math. I just wanted a function to construct a vector out of a speed/angle pair.
In fact, what I'll probably end up doing to save comp time is to save the last used vector calc, check to see if the angle has changed, and if it hasn't, just reuse the last used vector. Not that I'm terribly concerned about performance, as this will be running on some seriously meaty computers.
Tired of lying in the sunshine staying home to watch the rain. You are young and life is long and there is time to kill today. And then one day you find ten years have got behind you. No one told you when to run, you missed the starting gun.
|
Post #161,987
6/28/04 6:38:36 PM
|
And now I'm thinking of...
...just building a lookup table with all 360 angles.
Tired of lying in the sunshine staying home to watch the rain. You are young and life is long and there is time to kill today. And then one day you find ten years have got behind you. No one told you when to run, you missed the starting gun.
|
Post #161,988
6/28/04 6:43:47 PM
|
360?
You only need 90... ;-)
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #161,998
6/28/04 7:38:05 PM
|
Probably worth it
instead of modulo-ing angles - plus you get an unambiguous arctan without fuss.
-drl
|
Post #162,008
6/28/04 10:01:03 PM
|
A computational alternative.
There is no reason to have a more precise result than a human can notice.
Use the sine expansion:
sin( x ) = x - ( x3/3! ) + ( x5/5! ) - ...
and the cosine expansion:
cos ( x ) = 1 - (x2/2!) + (x4/4!) - ...
Of course the angle x has to be in radians (360 degrees is 2 PI radians) and you need to keep x small (i.e. no more than say PI/2 and THIS IS IMPORTANT!). That just means you have to know what quadrant you're in and supply the proper sign to the values. If you want to be less than 1 percent off, take each series to 4 terms (i.e sine term with 7! divisor and cosine term with 6! divisor.
The factorials can be precomputed constants. Save and compute each power from the prior power of x:
x = ? x2 = x * x x3 = x2 * x
etc.
Obviously, all floating point variables.
What this approach does is probably the same as what the library function does, but it quits much earlier and gives a less accurate but acceptable result.
Alex
Honor has not to be won; it must only not be lost. -- Arthur Schopenhauer (1788-1860), German philosopher
|
Post #162,019
6/29/04 12:17:20 AM
|
This suddenly got much easier.
I, at most, want a person to take a full second to rotate the ship a full 360 degrees. Since there are sixty heartbeats per second in this system (FPS maxes out at 60 for this project) I'll only need 60 "positions" out of the 360 that are actually there - at 0, 6, 12, etc.
Building the table will take less than a second at startup, even on slower computers. And it will help slower computers run it.
Tired of lying in the sunshine staying home to watch the rain. You are young and life is long and there is time to kill today. And then one day you find ten years have got behind you. No one told you when to run, you missed the starting gun.
|
Post #162,030
6/29/04 1:12:25 AM
|
That might not be how the library does it...
I've heard that at least some libraries find that it is faster and more efficient to have a partial table of sin's and cos's, then use the addition identities to fill in any one that they want:
sin(x + y) = sin(x)cos(y) + sin(y)cos(x) cos(x + y) = cos(x)cos(y) - sin(x)sin(y)
Cheers, Ben
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act - [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
|