Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 19 Aug 2012
Offline Last Active Aug 23 2012 11:26 AM

Posts I've Made

In Topic: AS3 Collision Issue

23 August 2012 - 11:11 AM

Thanks for the clarification; I don't usually get involved as much in the coding end of things, so this really helps!

over the past few days, I've been researching Separation of Axis Theory as well as some other literature on collision and physics. I managed to create a crude, but workable collision system from scratch based on measuring the distance between the four corners of each _block/tile against the center of the _ball/player object. The system worked pretty well, and I even got some basic jumping and gravity effects going.

Unfortunately, I needed to get the level to rotate. When I made the _Level mC rotate, however, the _ball would only respond to the _Level's non-rotated collision - while the level had the appearance of being rotated, the _ball kept running into "invisible" walls - presumably because the collision information was based on the non-rotated stage, which contains both the _ball and the _Level.

In order to fix this, I put the _ball mC into the _Level mC. This had the effect of causing all of the block_obj's/tiles to become undefined terms and "lose" their properties partway into the script - perhaps because the script couldn't distinguish between the block_obj's and the new _ball?

I'm running out of things to try right now, so any suggestions or advice would be a great help!

public function startTest()
			// constructor code
			collideUp = false;
			collideDown = false;
			collideRight = false;
			collideLeft = false;
			stage.focus = stage;
			if (_Level == null)
				_Level = new level_obj;
				_Level.x = 320;
				_Level.y = 224;
			if (_ball == null)
				_ball = new ball_obj;
				_ball.x = 320;
				_ball.y = 224;
			for(var i:int = _Level.numChildren-1;i>=0;i--)
				//blockArray.visible = false;
					if(_Level.getChildAt(i) is block_obj)
					//trace("more!: " +more);
					//trace("blockArray: " +blockArray);
					} else if (_Level.getChildAt(i) is ball_obj)
						//I thought perhaps that the _Level didn't know what to do with the _ball mC, since it wasn't in an array, so I created another one. No dice.
			//Event Listeners, etc.//

public function collision(event:Event):Array
			for (var i:int = _Level.numChildren-1;i>=0;i--)
				if (blockArray[i] == _ball)
					 trace("ball");//perhaps the _Level was confused by the extra child?
				var theBlock = blockArray[i];
				//trace ("i.x: " +blockArray[i].x +" i.y: " +blockArray[i].y);
				//trace ("ball.x: " +_ball.x +" ball.y: " +_ball.y);
				//trace ("Level.x: " +(_Level.x) +" Level.y: " +(_Level.y));
				//var ballCenterX = (-_Level.x+ballOffsetX);
				//var ballCenterY = (-_Level.y+ballOffsetY);
				var blockCenterX = _Level.theBlock.x; //This is where the "term" - presumably "blockArray[i] "loses" it's definition/properties.
				var blockCenterY = _Level.theBlock.y;
				var ballCenterX = (ballOffsetX); //the 2 ballOffsets's were defined earlier in the script so that the collision funcion wouldn't need to rely directly on the coordinates of the _ball itself.
				var ballCenterY = (ballOffsetY);
				//trace("ballCenterX: " +ballCenterX +" ballCenterY: " +ballCenterY);
				var blockLX = (blockCenterX +(theBlock.width/2))-ballCenterX;
				var blockRX = (blockCenterX -(theBlock.width/2))-ballCenterX;
				var blockUY = (blockCenterY +(theBlock.height/2))-ballCenterY;
				var blockDY = (blockCenterY -(theBlock.height/2))-ballCenterY;
				var DistX = blockCenterX - ballCenterX;
				var DistY = blockCenterY - ballCenterY;
				var Dist:Number = Math.sqrt(DistX*DistX + DistY*DistY);
				var DistUL:Number = Math.sqrt(blockLX*blockLX +blockUY*blockUY);
				var DistUR:Number = Math.sqrt(blockRX*blockRX +blockUY*blockUY);
				var DistDL:Number = Math.sqrt(blockLX*blockLX +blockDY*blockDY);
				var DistDR:Number = Math.sqrt(blockRX*blockRX +blockDY*blockDY);
				//Code for determining collision directions, etc//