A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Line of sight/raycasting calculation problem

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    9

    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!

  2. #2
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    I simplified the code to this

    public function Range_draw():Sprite
    {

    range.graphics.clear();
    range.graphics.beginFill(0xffffff,1);
    range.graphics.lineStyle(0,0x000000);
    range.graphics.moveTo(startx,starty);
    rot = 105;
    for (var o:int = 0; o<30; o++)
    {
    rot -= 1;
    thisAngle = rot * Math.PI / 180;
    pt0.x = startx;
    pt0.y = starty;

    while (!obstacle.hitTestPoint(pt0.x,pt0.y,true) && pt0.y < 500 && pt0.y > -100 && pt0.x > -100 && pt0.x < 500)
    {


    pt0.x = pt0.x + 1 * Math.cos(thisAngle);
    pt0.y = pt0.y + 1 * Math.sin(thisAngle);

    }//while

    range.graphics.lineTo(pt0.x,pt0.y);


    }

    range.graphics.endFill();
    range.graphics.lineTo(startx,starty);
    return range;
    }
    now it doesn't use the array, only one point which it resets and sends off in a different direction every time, with an interval of 10 milisecs for this function, still get the same results, very slow program run, so I think I need to change my method completely

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    923
    the fastest would be to approximate your movie clips by polygons and find intersections of sight lines with their edges.
    who is this? a word of friendly advice: FFS stop using AS2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center