Path: chuka.playstation.co.uk!news From: James Shaughnessy Newsgroups: scee.yaroze.programming.2d_graphics Subject: Re: trigonometric speed question Date: Wed, 22 Jul 1998 13:18:41 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 43 Message-ID: <35B5D8A1.1DC7@manc.u-net.com> References: <01bdb502$e7b60630$0100fc9d@skynet> NNTP-Posting-Host: manc.u-net.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.0 (Win95; I) Antony Arciuolo wrote: > You know Starcraft? The characters move the same speed no matter > which direction they face? Well what is a good method of achieving > this? I know the trig, but in 320x240 mode, I have a maximum speed > of 4, which is way too fast already. > This means the only speed pairs (speedX, speedY) are: > (4,0); (3,1); (2,2); (1,3); (0,4)... and so on for the other > quadrants. > So how do I get a better angular resolution? without increasing the > maximum speed? Hi there, It seems you are yet to discover the delights of the twelve step bitshift. It is a way of getting floating point without using floats or doubles. All you do is treat all your velocity variables (or whatever) as 4096 times the size they really are. So in your case instead of a speed pair being (4,0) it would be ((4<<12),0) that is (16384, 0) [Note how you use a quick bitshift to times by 4096 (2^12=4096)] When it comes to using the value as its actual number just bitshift the other way: speed_x = (16384>>12) You can do normal trig. to get very accurate angular resolution. Eg. if your speed is 4, and you want the direction 30 degrees NE, then this would be your speed pair: (8192, 14189) (equvalent to (2.0, 3.46) Never use floating point numbers if you are after fast execution - golden rule with the PSX it seems.) The basic formula (of course :)) is this: vx = (speed_of_ship * SIN[angle])>>12); vy = (speed_of_ship * COS[angle])>>12); where angle is an integer 0 < angle < 359 using a LUT If you want to use sin() and cos() then again you're adding a spanner, so use lookup tables for very fast trig maths (there is a sin/cos lookup table header file builder on my page -- see SCBUILD.ZIP if you don't want to take a day off to type in your own. NEVER resort to run-time calculation of sin/cos LUTs!) Cheers, Jim -- ----------------------------------------- James Shaughnessy james@manc.u-net.com http://www.netyaroze-europe.com/~shaughnj -----------------------------------------