Line of sight/raycasting calculation problem
Hey, i'm working on something in which I need a light of sight of an AI to be permanently updated(or at least when given a time to start working), I wrote a small class called Ranged which creates an array with 20 points, and sends them off in different directions until they hit a MovieClip, and then draw lines between them and fill. Its built out of several functions:
Range_create_points, just creates new points and adds them to an array
Code:
public function Range_create_points():void
{
for (var i:int = 0; i<20; i++)
{
var pt_temp:Point = new Point(startx,starty);
lightarr.push(pt_temp);
}//for
}//Range_create_points
Range_draw, which draws the range, this is being done over and over in the constructor function with an interval of 10 milisecs
Code:
public function Range_draw():void
{
Range_calc();
range.graphics.clear();
range.graphics.beginFill(0xffffff,1);
range.graphics.moveTo(startx,starty);
for (var o:int = 0; o<lightarr.length; o++)
{
range.graphics.lineTo(lightarr[o].x,lightarr[o].y);
}//for
range.graphics.endFill();
range.graphics.lineTo(startx,starty);
}//Range_draw
this is Range_calc, the function Range_draw is calling:
Code:
public function Range_calc():void
{
rot = 105;
for (var i:int = 0; i<20; i++)
{
rot -= 2;
thisAngle = rot * Math.PI / 180;
lightarr[i].x = startx;
lightarr[i].y = starty;
while (!obstacle.hitTestPoint(lightarr[i].x,lightarr[i].y,true) && lightarr[i].y < 500 && lightarr[i].y > -100 && lightarr[i].x > -100 && lightarr[i].x < 500)
{
lightarr[i].x = lightarr[i].x + 1 * Math.cos(thisAngle);
lightarr[i].y = lightarr[i].y + 1 * Math.sin(thisAngle);
}//while
}//for
}//Range_calc
lightarr includes an array of points with a starter x and y properties(startx,starty)
In the constructor I have var rayInterval:uint = setInterval(Range_draw,10);
however on runtime this is very slow even with just 20 points. Is there any way I can utilize this to work fast and still update constantly?
thanks!:domo: