0

I decided I want to learn about ray casting and give it a go. Although I am by no means a good programmer I just thought it would be good to try to expand my knowledge, plus this may be useful at some point :P I have been trying to follow this guide: http://www.permadi.com/tutorial/raycast/ and it is actually a really good guide but I am afraid I have slipped up somewhere and can't figure out where. My code:

package rayCast{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;

    public class Main extends MovieClip{
        public var charPosX:Number = new Number(192);
        public var charPosY:Number = new Number(192);
        public static var charFov:Number = new Number(60);
        public var charPov:Number = new Number(45);
        public static var charHeight:Number = new Number(32);
        public static var projCharRel:Number = new Number(476);
        public static var rayAngle:Number = new Number(60/200);
        public var gridMap:Array = new Array();
        public var screen:Sprite = new Sprite();

        public function Main(){
            gridMap = [[1,1,1,1,1,1,1,1],[1,0,0,0,0,0,0,1],[1,0,0,1,0,0,0,1],[1,0,0,0,0,0,0,1],[1,0,0,0,1,0,0,1],[1,0,0,0,0,0,0,1],[1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1]];
            //addEventListener(Event.ENTER_FRAME, update);
            screen.graphics.lineStyle(1, 0x000000);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownE);
        }

        public function mouseDownE(e:MouseEvent):void{
            for( var i:int =0; i < 200; i++){
                var Ya:Number = new Number();
                var Xb:Number = new Number();
                var oldXb:Number = new Number();
                var oldYa:Number = new Number();
                if(i+charPov-30 > 180){
                    Ya = -64;
                    oldYa = Math.floor(charPosY/64) * (64) -1;
                }
                else{
                    Ya = 64;
                    oldYa = Math.floor(charPosY/64) * (64) + Ya;
                }
                if(charPov > 270 || charPov < 90){
                    Xb = 64;
                    oldXb = Math.floor(charPosX/64) * (64) + Xb;
                }
                else{
                    Xb = -64;
                    oldXb= Math.floor(charPosX/64) * (64) -1;
                }

                var oldXa:Number = charPosX + (charPosY-oldYa)/Math.tan(i+charPov-30);
                var oldYb:Number = charPosX + (charPosX-oldXb)*Math.tan(i+charPov-30);
                var hit:Boolean = false;
                var Xa:Number = 64/Math.tan(i+charPov-30);
                var Yb:Number = 64*Math.tan(i+charPov-30);
                var newYa:Number;
                var newXa:Number;
                var newYb:Number;
                var newXb:Number;
                var aDistance:Number;
                var bDistance:Number;

                while(hit != true){
                    newYa = oldYa + Ya;
                    newXa = oldXa + Xa;
                    if(gridMap[Math.floor(newYa/64)-1][Math.floor(newXa/64)-1] == 1){
                        aDistance = Math.abs(charPosX - newXa)/Math.cos(i+charPov-30);
                        hit=true;
                    }
                    else{
                        oldYa = newYa; oldXa = newXa;
                    }
                }
                hit = false;
                while(hit != true){                 
                    newYb = oldYb + Yb;
                    newXb = oldXb + Xb;
                    if(gridMap[Math.floor(newYb/64)-1][Math.floor(newXb/64)-1] == 1){
                        bDistance = Math.abs(charPosX - newXb)/Math.cos(i+charPov-30);
                        hit=true;
                    }
                    else{
                        oldYb = newYb; oldXb = newXb;
                    }
                }
                if(aDistance >= bDistance){
                    screen.graphics.moveTo(i, 90 - (64 / bDistance * 277)/2)
                    screen.graphics.lineTo(i, 90 + (64 / bDistance * 277)/2)
                }
                else{
                    screen.graphics.moveTo(i, 90 - (64 / aDistance * 277)/2)
                    screen.graphics.lineTo(i, 90 + (64 / aDistance * 277)/2)
                }

            }
        }
    }
}

What this attempts to do is render the screen when the mouse is pressed down. Unfortunately whenever I press the mouse button I get this TypeError: Error #1010: A term is undefined and has no properties. at rayCast::Main/mouseDownE() I thought it might be my gridMap[] was a little off somewhere but when I did a variable table everything seemed to be working fine :/ Maybe I went wrong with the variable table but could someone help me? Thanks in advance, Kyle.

FishBowl
  • 9
  • 4
  • At least you don't need to do `new Number()` as these are primitives, so you either declare them and leave as is, or assign zero. And yes, you don't have range checking in your raycast loop, as if the map is open, your next `gridMap[Math.floor(newYa/64)-1]` might be out of range, thus undefined. – Vesper Oct 25 '13 at 12:47
  • But my grid map is closed, there is a 1 totally surrounding the map. Surely this wouldn't occur, unless I have made a mistake with the positioning of newX and newY relative to gridMap x and y. – FishBowl Oct 25 '13 at 12:58
  • Maybe this will help http://dev.mothteeth.com/2011/11/2d-ray-casting-on-a-grid-in-as3/ – Pier Oct 26 '13 at 23:11
  • And this http://rocketmandevelopment.com/blog/as3-2d-ray-casting-for-collision-detection/ – Pier Oct 26 '13 at 23:12
  • I am still unsure on where my code is wrong :/ Is there just like a simple mistake to fix or did I misinterpret the technique entirely? – FishBowl Oct 28 '13 at 22:41

0 Answers0