gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Ever decreasing circles of hell!

#47802 - lewisrevill - Wed Jul 13, 2005 7:45 pm

Hi people, as you will find out newbie here, but really need your help. First of all could you help me with the code for plotting a circle the manual way with "theta", "sin" and "cos". Also please point out if i will need a header file for any of these functions. Secondly could you help me SOHCAHTOA - namely the TOA = tan(opposite/adjacent). I need to find out the angle of a triangle using the lengths of the opposite and adjacent sides of the triangle?. Please help. Thank you

#47806 - Quirky - Wed Jul 13, 2005 8:00 pm

Look up bresenham circle on google for a much better way of plotting circles. Alternatively, use the old r? = x? + y? and square roots to do the job:

For a filled circle of "size" at x, y...
Code:

  int top = y - size;
  int bot = y + size;
  int ypos;
  for (ypos = top; ypos < bot; ypos++) {
    int w = (size*size) - ((ypos-y)*(ypos-y));
    w = gbasqrt(w);
    int x1 = x-w;
    int x2 = x+w;
    horizontal_line(x1,x2,ypos);
  }


Plug in your own square root and horizontal line code.[/code]

#47807 - lewisrevill - Wed Jul 13, 2005 8:14 pm

Hey thanks, but i dont understand the code, i got taught at uni that we set theta then x=sin(theta) and y=cos(theta), but how do i set the theta? cos it needs to increment. PLease help. I need this because i want a sprite to follow a path of a circle.

#47822 - DekuTree64 - Wed Jul 13, 2005 10:24 pm

Basically what you'll want to do is x = centerX + radius*cos(theta), y = centerY + radius*sin(theta).

Theta is the angle, or the distance around the circle. Units are probably radians, so about 6.28 would be a full rotation. So, if you want your sprite to make a full trip around the circle after 60 frames, add 6.28/60 (about 0.105) to theta every frame.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#47824 - tepples - Wed Jul 13, 2005 10:31 pm

In practice, doing floating point arithmetic on the GBA is slow, let alone sin() or cos(). You'll want to precompute a lookup table using a PC program (written in C, Perl, Scheme, Python, Excel, or whatever) and then compile that into your GBA program.

cos(num_degrees * i / (2*Pi)), for i = 0..num_degrees-1

The num_degrees doesn't even have to be 360; a lot of programs use 512 or some other power of 2 degrees so that they can easily handle cos(theta) and sin(theta) outside the [0, num_degrees) domain.

Try reading the TONC tutorial on lookup tables
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#47834 - Dwedit - Thu Jul 14, 2005 12:00 am

For drawing curvy shapes, it's much faster to use a midpoint algorithm with a decision parameter than doing it the slow manual way. Midpoint algorithms only use addition and subtraction once they enter the inner loop.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."