A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [HELP] dynamic Lighting and shadows

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    24

    Question [HELP] dynamic Lighting and shadows

    i want to create a dynamic lighting engine and i'm not 100% sure about how to do it.

    i dont want the answer given to me on a silver platter i just want some tips and tricks and a few hints on how to go about it

    atm the code i have is this
    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.geom.*;
    	import flash.utils.*;
    	public class shapes extends MovieClip {
    		var point1:Array = new Array(30,10)
    		var point2:Array = new Array(100,10)
    		var point3:Array = new Array(100,100)
    		var point4:Array = new Array(50,100)
    		var point5:Array = new Array(50,150)
    		var point6:Array = new Array(300,150)
    		var point7:Array = new Array(300,10)
    		var point8:Array = new Array(400,10)
    		var point9:Array = new Array(400,250)
    		var point10:Array = new Array(450,250)
    		var point11:Array = new Array(450,350)
    		var point12:Array = new Array(250,350)
    		var point13:Array = new Array(250,250)
    		var point14:Array = new Array(100,250)
    		var point15:Array = new Array(100,350)
    		var point16:Array = new Array(30,350)
    
    		
    		var line:Sprite = new Sprite
    		var mouseLine:Sprite = new Sprite()
    
    		var shape:Array = new Array()
    		
    		var colour:int = 990055
    		
    		public function shapes(){
    			shape.push(point1,point2,point3,point4,point5,point6,point7,point8,point9,point10,point11,point12,point13,point14,point15,point16)
    			addEventListener(Event.ENTER_FRAME,eFrame)
    			addChild(line)
    			addChild(mouseLine)
    			
    		}
    		
    		function eFrame(event:Event){
    			drawShape(shape,colour)
    			drawMousetoPoints(shape)
    		}
    		
    		function drawMousetoPoints(theShape:Array){
    			mouseLine.graphics.clear()
    			for(var i in theShape){
    				mouseLine.graphics.moveTo(theShape[i][0],theShape[i][1])
    				mouseLine.graphics.lineStyle(2)
    				var I = theShape[i][0]+(theShape[i][0]-mouseX)
    				var J = theShape[i][1]+(theShape[i][1]-mouseY)
    				mouseLine.graphics.lineTo(I,J);
    			}
    		}
    		
    		function drawShape(theShape:Array,theColour:int){
    			line.graphics.clear()
    			line.graphics.beginFill(theColour,.0)
    			line.graphics.moveTo(theShape[0][0],theShape[0][1])
    			for(var i in theShape){
    				
    				if(i<theShape.length-1){
    					line.graphics.lineStyle(2)
    					line.graphics.lineTo(theShape[i][0],theShape[i][1]);
    				}else{
    					line.graphics.lineStyle(2)
    					line.graphics.lineTo(theShape[i][0],theShape[i][1])
    					line.graphics.lineTo(theShape[0][0],theShape[0][1])
    				}
    			}
    			line.graphics.endFill()
    		}
    	}
    }
    cheers in advance

  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    24

    Exclamation still want help

    kk ppl i still need help but i have progressed a fair way

    dynamicLighting.as
    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.geom.*;
    	import flash.utils.*;
    	public class dynamicLighting extends MovieClip {
    		var building1:Array = new Array(30,10);
    		var building2:Array = new Array(100,10);
    		var building3:Array = new Array(100,100);
    		var building4:Array = new Array(50,100);
    		var building5:Array = new Array(50,150);
    		var building6:Array = new Array(300,150);
    		var building7:Array = new Array(300,10);
    		var building8:Array = new Array(400,10);
    		var building9:Array = new Array(400,250);
    		var building10:Array = new Array(450,250);
    		var building11:Array = new Array(450,350);
    		var building12:Array = new Array(250,350);
    		var building13:Array = new Array(250,250);
    		var building14:Array = new Array(100,250);
    		var building15:Array = new Array(100,350);
    		var building16:Array = new Array(30,350);
    
    		var wallPoints:Array = new Array ();
    		var line:Sprite = new Sprite  ;
    
    		var mouseLine:Sprite = new Sprite  ;
    
    		var buildingPoints:Array = new Array  ;
    		var colour:int = 990055;
    
    		var shapeAr:Array = new Array();
    		var building:shape;
    		var wall:shape;
    
    		//for y = mx + c
    		var m:Number;
    		var c:Number;
    		var circle:Shape = new Shape();
    		public function dynamicLighting() {
    			wallPoints = [[250,175],[250,225],[175,225],[175,175]];
    			buildingPoints.push(building1,building2,building3,building4,building5,building6,building7,building8,building9,building10,building11,building12,building13,building14,building15,building16);
    			building = new shape(buildingPoints);
    			wall = new shape(wallPoints);
    			addEventListener(Event.ENTER_FRAME,eFrame);
    			addChild(building);
    			addChild(wall);
    			addChild(line);
    			addChild(mouseLine);
    			addChild(circle);
    
    			shapeAr.push(building,wall);
    		}
    
    		function eFrame(event:Event) {
    			//building.drawShape()
    			//wall.drawShape()
    			drawMousetoPoints(shapeAr);
    		}
    
    		function drawMousetoPoints(theShape:Array) {
    			mouseLine.graphics.clear();
    			circle.graphics.clear();
    			for (var i in theShape) {
    				for (var k in theShape[i].pointAr) {
    					mouseLine.graphics.moveTo(theShape[i].pointAr[k][0],theShape[i].pointAr[k][1]);
    					mouseLine.graphics.lineStyle(1,0xFF0000);
    
    					//x and y for the mouse, current point being tested and the two surrounding points
    					var x1:Number = mouseX;
    					var y1:Number = mouseY;
    					var x2:Number = theShape[i].pointAr[k][0];
    					var y2:Number = theShape[i].pointAr[k][1];
    					var x3:Number;
    					var y3:Number;
    					var x4:Number;
    					var y4:Number;
    					var v1:Array = new Array();
    					var v2:Array = new Array();
    					var v3:Array = new Array();
    					//used for testing one line at a time i change it to if (k == 5)
                                            if (k >= 0) {
    						getMC([x1,y1],[x2,y2]);
    						if (k == theShape[i].pointAr.length - 1) {
    							x3 = theShape[i].pointAr[0][0];
    							y3 = theShape[i].pointAr[0][1];
    
    							x4 = theShape[i].pointAr[k - 1][0];
    							y4 = theShape[i].pointAr[k - 1][1];
    
    						} else if (k == 0) {
    							x3 = theShape[i].pointAr[k + 1][0];
    							y3 = theShape[i].pointAr[k + 1][1];
    
    							x4 = theShape[i].pointAr[theShape[i].pointAr.length - 1][0];
    							y4 = theShape[i].pointAr[theShape[i].pointAr.length - 1][1];
    
    						} else {
    							x3 = theShape[i].pointAr[k + 1][0];
    							y3 = theShape[i].pointAr[k + 1][1];
    
    							x4 = theShape[i].pointAr[k - 1][0];
    							y4 = theShape[i].pointAr[k - 1][1];
    						}
    
    						//trace("M:\t"+ x1,y1)
    						//trace("P:\t"+ x2,y2)
    						///trace("-:\t"+ x3,y3)
    						//trace("+:\t"+ x4,y4+"\n")
    						//v1 = [(x2 - x1),(y2-y1)];
    						//v2 = [(x3 - x2),(y3-y2)];
    						//v3 = [(x2 - x4),(y2-y4)];
    						//trace("V1:\t"+v1)
    						//trace("V2:\t"+v2)
    						//trace("V3:\t"+v3+"\n")
    						//var c1 = (v1[0]-v2[0])*(v1[1]-v2[1]);
    						//var c2 = (v1[0]-v3[0])*(v1[1]-v3[1]);
    						i//f (c1 <= 0 && c2 <= 0 || c1 >= 0 && c2 >= 0) {
    
    
    
    						//}
    						//mouseLine.graphics.lineTo(theShape[i].pointAr[k][0]+theShape[i].pointAr[k][0]-mouseX,theShape[i].pointAr[k][1]+theShape[i].pointAr[k][1]-mouseY);
    						testIntersection(theShape,i,k,x2,y2);
    					}
    				}
    			}
    		}
    
    		function getMC(xy1:Array,xy2:Array) {
    			m = (xy2[1]-xy1[1])/(xy2[0]-xy1[0]);
    			c = xy1[1] - m * xy1[0];
    		}
    
    		function testIntersection(theShape:Array,j,l,cx,cy) {
    			var pointAr:Array = new Array()
    			for (var i in theShape) {
    				for (var k in theShape[i].pointAr) {
    					if (i == l) {
    						if (k == j || k == j - 1 || k == j + 1) {
    							continue;
    						}
    					}
    					//x and y for the mouse, current point being tested and the two surrounding points
    					var x1:Number = theShape[i].pointAr[k][0];
    					var y1:Number = theShape[i].pointAr[k][1];
    					var x2:Number;
    					var y2:Number;
    					var testX:Number;
    					var testY:Number;
    					var v1:Array = new Array();
    
    					if (k == theShape[i].pointAr.length - 1) {
    						x2 = theShape[i].pointAr[0][0];
    						y2 = theShape[i].pointAr[0][1];
    
    
    					} else {
    						x2 = theShape[i].pointAr[k + 1][0];
    						y2 = theShape[i].pointAr[k + 1][1];
    
    					}
    					prepCircle(x1,x2,testX,y1,y2,testY,cx,cy)
    					//trace();
    				}
    			}
    		}
    
    		function drawCircle(X,Y) {
    			circle.graphics.beginFill(0x00FF00);
    			circle.graphics.lineStyle(1,0x00FF00);
    			circle.graphics.drawCircle(X,Y,1);
    			circle.graphics.endFill();
    			mouseLine.graphics.lineTo(X,Y);
    		}
    
    		function prepCircle(x1,x2,testX,y1,y2,testY,cx,cy) {
    
    			if (y2 == y1) {
    				testX = (y2-c)/m;
    				if (mouseX < cx && testX > cx || mouseX > cx && testX < cx) {
    					if (x2 > x1) {
    						if (testX < x2 && testX > x1) {
    							drawCircle (testX,y2);
    							if(testX != undefined && y2 != undefined){
    								return ([testX,y2]);
    							}
    						}
    					} else {
    						if (testX > x2 && testX < x1) {
    							drawCircle (testX,y2)
    							if(testX != undefined && y2 != undefined){
    								return ([testX,y2]);
    							}
    						}
    					}
    				}
    			}
    
    			if (x2 == x1) {
    				testY = m * x2 + c;
    				if (mouseY < cy && testY > cy || mouseY > cy && testY < cy) {
    					if (y2 > y1) {
    						if (testY < y2 && testY > y1) {
    							drawCircle (x2,testY);
    							if(testY != undefined && x2 != undefined){
    								return ([x2,testY]);
    							}
    						}
    					} else {
    						if (testY > y2 && testY < y1) {
    							drawCircle (x2,testY);
    							if(testY != undefined && x2 != undefined){
    								return ([x2,testY]);
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    }
    and

    shape.as
    Code:
    //this is where the shapes will be held and make them selves
    
    package{
    	import flash.display.*;
    	import flash.events.*;
    	import flash.geom.*;
    	import flash.utils.*;
    	
    	public class shape extends MovieClip{
    		
    		var pointAr:Array = new Array
    		
    		var line:Sprite = new Sprite
    		
    		public function shape(points){
    			
    			pointAr = points
    			addChild(line)
    			drawShape()
    		
    		}
    		
    		function drawShape(){
    			line.graphics.clear();
    			line.graphics.beginFill(0x000000,.0);
    			line.graphics.moveTo(pointAr[0][0],pointAr[0][1]);
    			for (var i in pointAr) {
    
    				if (i<pointAr.length-1) {
    					line.graphics.lineStyle(1,0xFF0000);
    					line.graphics.lineTo(pointAr[i][0],pointAr[i][1]);
    				} else {
    					line.graphics.lineStyle(1,0xFF0000);
    					line.graphics.lineTo(pointAr[i][0],pointAr[i][1]);
    					line.graphics.lineTo(pointAr[0][0],pointAr[0][1]);
    				}
    			}
    			line.graphics.endFill();
    		}
    		
    	}
    }
    shape.as just holds the room and box.

    that is all that is needed so just new flash with those two files and it will show you what i'm up to so please a helping hand.

    and sorry bout lack of comments in code,

Tags for this Thread

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