#1260 - darkcloud - Thu Jan 16, 2003 12:37 am
Any one have a function that I could use that could fill an array with points on a circle? I want to make a circle path that my sprites could follow. I was thinking of using two arrays, one for x, and one for y. Is this a good way to do paths or is there a better way?
_________________
Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind.
#1265 - shadow_gg - Thu Jan 16, 2003 1:30 am
I'm not sure I will ask your question but :
for me the easier way ,is getting the actual angle between the sprite and the center of your rotation like atanf(deltay/deltax) (if you don't already know it) and in function of the way you want him to rotate increment or decrement the angle.
So the next position of the sprite with this new angle is Xnextpos = distance * cosf(angle) + Xcenter.
Ynextpos = distance * sinf(angle) + Ycenter.
So,I think if you want to use arrays for optimizing speed,you only have to build a precalculated table of cos(fixed point)->1 mul + 1 add
I think this is the better way because you don't have const values!
#1300 - Lord Graga - Thu Jan 16, 2003 3:47 pm
Code: |
void Mode4_Circle(u16 xCenter, u16 yCenter, u16 radius, u8 color)
{
//Local variables
s16 x = 0;
s16 y = radius;
s16 p = 3 - 2 * radius;
while (x <= y)
{
Mode4_PlotPixel(xCenter + x, yCenter + y, color);
Mode4_PlotPixel(xCenter - x, yCenter + y, color);
Mode4_PlotPixel(xCenter + x, yCenter - y, color);
Mode4_PlotPixel(xCenter - x, yCenter - y, color);
Mode4_PlotPixel(xCenter + y, yCenter + x, color);
Mode4_PlotPixel(xCenter - y, yCenter + x, color);
Mode4_PlotPixel(xCenter + y, yCenter - x, color);
Mode4_PlotPixel(xCenter - y, yCenter - x, color);
if (p < 0)
{
p += 4 * x++ + 6;
}
else
{
p += 4 * (x++ - y--) + 10;
}
}
} |
#1648 - AnthC - Tue Jan 21, 2003 12:58 am
From the top of my head, here's an age old poor mans rotator :-
(You could use this to fill your circle buffer with a bit of tinkering)
int r=64; // radius
int x=r*65536; // ~cos(angle)*r
int y=0*65536; // ~sin(angle)*r
int cx=LCD_WIDTH/2; //
int cy=LCD_HEIGHT/2; // centre position
for (;;)
{
PlotPixel(cx+x/65536,cy+y/65536);
x- =y*1/16; //
y+=x*1/16; // (*1/16 defines the rate at which the points rotate)
}