Trouble creating Ellipses/Ovals [SOLVED]

Started by Charity, Sun 01/07/2007 09:43:58

Previous topic - Next topic

Charity

Two (related) questions.  I am trying to confine a character inside of a circle on the ground.  The circle needs to be visible to the player and easily resizable and movable.  My first idea was to represent the "trap" using the RawDrawCircle function, but when I did, I realised two things = first, the circle is automatically filled, which looks strange, and second, a literal circle doesn't take perspective into account, since my rooms and character aren't drawn from a straight overhead viewpoint.  So question one: is it possible to RawDraw an ellipse without using an imported image of some sort?  And how?

Then there is the problem of keeping the character inside the "circle".  Since the size and location are not static, I can't use walkable areas.  My best idea so far is to anchor the character to the ellipse's foci, and somehow stop him or pull him back if he goes too far from them, but I can't figure out how to check the sum distance from the two foci.  Will I need to use floats?  (Bear in mind that my geometry is extremely sketchy at best.  Not bad at math so much as waay behind.)

If anyone could point me in the right direction it would be greatly appreciated.

Gilbert

To draw an unfilled ellipse try something quick like this:
Code: ags

function RawDrawEllipse(int x, int y, int width, int height){
  float yscale=IntToFloat(height)/IntToFloat(width);
  int ii=width;
  int ytemp;
  while (ii>=0){
    ytemp=FloatToInt(Maths.Sqrt(IntToFloat((width*width)-(ii*ii)))*yscale);
    RawDrawLine(x-ii, y-ytemp, x-ii, y-ytemp);
    RawDrawLine(x-ii, y+ytemp, x-ii, y+ytemp);
    RawDrawLine(x+ii, y-ytemp, x+ii, y-ytemp);
    RawDrawLine(x+ii, y+ytemp, x+ii, y+ytemp);
    ii--;
  }
}

I don't have time to look at the restrict character problem yet, but maybe you can have some hint by looking at the drawing function above.

Charity

Thank you so much!  It works like a charm.  Though for the record, the way that code works , if a column y has more than two pixels (such as at the ends of the ellipse), it will only draw the top and bottom ones.  I found a couple ways to fix it, but the one I've settled on for now is
Code: ags
function RawDrawEllipse(int x, int y, int width, int height){
  float yscale=IntToFloat(height)/IntToFloat(width);
  int ii=width;
  int ytemp;
  int lastx=ii;
  int lasty=FloatToInt(Maths.Sqrt(IntToFloat((width*width)-(ii*ii)))*yscale,eRoundNearest);
  while (ii>=0){
    ytemp=FloatToInt(Maths.Sqrt(IntToFloat((width*width)-(ii*ii)))*yscale,eRoundNearest);
    RawDrawLine(x-lastx, y-lasty, x-ii, y-ytemp);
    RawDrawLine(x-lastx, y+lasty, x-ii, y+ytemp);
    RawDrawLine(x+lastx, y-lasty, x+ii, y-ytemp);
    RawDrawLine(x+lastx, y+lasty, x+ii, y+ytemp);
    lastx=ii;
    lasty=ytemp;
    ii--;
  }
}

which seems the cleanest, scriptingwise, and creates the smoothest ellipses I have been able to manage so far.

I was also able to use those yscale and ytemp codes to check if the character was at the edge of the circle and stop him if he was, but it is still very buggy, and crashes if he manages to step out of range by a pixel or two.  I should be able to clean it up from here, so consider this case solved.  Thanks again.  ^_^

SMF spam blocked by CleanTalk