Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AABB math based collision not working so well

    Hey Guys, Heres a ton of code. AABB math based Collision.
    Its not working though, Its having tons of problems. I would really like to do it this way, as its so efficient and powerfully precise.
    The issue is the same two problems keep coming up. Either Its solving the collision too quickly and the rabbit is being moved up before it even touches the tile again (usually involving for loops) or its like this, which looks good as theres no unnessary "bouncing" (the irony is the character is a rabbit), but the player often falls through tiles or isn't hitting the the wall at the same time as the floor, or is barely nipping into the floor and finding the corner of the side of it and treating it like a wall and not moving sideways on the ground.

    Help?

    Heres a megaswf link to it. http://megaswf.com/s/2439428

    Code:
    public function loop(e:Event){
    			updatePlayer();
    			updateCollisions();
    		}
    		private function updatePlayer(){
    				if(gravityB)
    					player.ySpd += gravity;
    				if(left)
    					player.xSpd = -walkSpd;
    				if(right)
    					player.xSpd = walkSpd;
    				if(up)
    					player.ySpd = -jumpSpd;
    				if(player.ySpd > maxSpd)
    					player.ySpd = maxSpd;
    				prevSq1X = player.x;
    				player.x += player.xSpd;
    				squareX = player.x;
    				player.xSpd = 0;
    			if(gravityB){
    				prevSq1Y = player.y;
    				player.y += player.ySpd;
    				squareY = player.y;
    			}
    		}
    		private function updateCollisions(){
    			var i:int = 0;
    			
    			while(i < ground_a.length){
    				solveCollision(i);
    				
    				//trace(i);
    				i++;
    			}//end of for loop
    			findTOCY();
    			findTOCX();
    		}
    		public function solveCollision(i) {
    			squareX2 = ground_a[i].x;
    			squareY2 = ground_a[i].y;
    			squareWidth2 = ground_a[i].width;
    			squareHeight2 = ground_a[i].height;
    			var velocityY = squareY - prevSq1Y;
    			var velocityX = squareX - prevSq1X;
    			var distanceX1;
    			var distanceY1;
    			var distanceX2;
    			var distanceY2;
    				if(velocityX < 0){
    					distanceX1 = squareX2 + squareWidth2 - prevSq1X;
    					distanceX2 = squareX2 - prevSq1X - squareWidth;
    					movingLeft = true;
    					//trace("Moving Left!");
    				} else {
    					distanceX1 = squareX2 - prevSq1X - squareWidth;
    					distanceX2 = squareX2 + squareWidth2 - prevSq1X;
    					movingRight = true;
    				}
    				if(velocityY < 0){
    					distanceY1 = squareY2 + squareHeight2 - prevSq1Y;
    					distanceY2 = squareY2 - prevSq1Y - squareHeight;
    					movingUp = true;
    					player.ySpd = 0;
    					//trace("Moving UP!");
    				} else {
    					distanceY1 = squareY2 - prevSq1Y - squareHeight;
    					distanceY2 = squareY2 + squareHeight2 - prevSq1Y;
    					movingDown = true;
    				}
    			var timeX = distanceX1/velocityX;
    			var timeY = distanceY1/velocityY;
    			var actualTime = Math.max(timeX,timeY);
    			var disjointTimeX = distanceX2/velocityX;
    			var disjointTimeY = distanceY2/velocityY;
    			var actualDisjointTime = Math.min(disjointTimeX, disjointTimeY);
    			if (timeX < 0 && timeY < 0){
    				//trace("COLLISION FOUND UNWORTHY");
    				//trace("TimeX < 0 && timeY < 0");
    				return;
    			}
    			if(timeX > 1 || timeY > 1){
    				//trace("COLLISION FOUND UNWORTHY");
    				//trace("timeX > 1 || timeY > 1");
    				return;
    			}
    			if(timeX == timeY){
    				//trace("timeX has the same value as timeY");
    				return;
    			}
    			if(timeX > timeY){
    				//trace("COLLISION HAPPENED ON X AXIS");
    			}
    			if(timeX < timeY){
    				//trace("COLLISION HAPPENED ON Y AXIS");
    			}
    			if(actualTime < actualDisjointTime){
    				//trace("COLLISION FOUND WORTHY~~>");
    				var tOC = getTimer();
    				timeVector.push(tOC);
    				timeXV.push(timeX);
    				timeYV.push(timeY);
    				actualTimeV.push(actualTime);
    				actualDTimeV.push(actualDisjointTime);
    				velocityYV.push(velocityY);
    				velocityXV.push(velocityX);
    			}
    		}
    		
    		
    		public function findTOCY(){
    			if(timeVector.length > 0){
    					minTOC = Math.min.apply(null,timeVector);
    					var collisionMarker = timeVector.indexOf(minTOC);
    					var tx = timeXV[collisionMarker];
    					var ty = timeYV[collisionMarker];
    					var at = actualTimeV[collisionMarker];
    					var adt = actualDTimeV[collisionMarker];
    					var vy = velocityYV[collisionMarker];
    					var vx = velocityXV[collisionMarker];
    					player.y = player.y - ((1-at) * vy);
    					timeXV.splice(collisionMarker, 1);
    					timeYV.splice(collisionMarker, 1);
    					actualTimeV.splice(collisionMarker, 1);
    					velocityYV.splice(collisionMarker, 1);
    					velocityXV.splice(collisionMarker, 1);
    					timeVector.splice(collisionMarker, 1);
    			}
    		}
    		public function findTOCX(){
    			if(timeVector.length > 0){
    					trace(timeVector.length);
    					minTOC = Math.min.apply(null,timeVector);
    					var collisionMarker = timeVector.indexOf(minTOC);
    					var tx = timeXV[collisionMarker];
    					var ty = timeYV[collisionMarker];
    					var at = actualTimeV[collisionMarker];
    					var adt = actualDTimeV[collisionMarker];
    					var vy = velocityYV[collisionMarker];
    					var vx = velocityXV[collisionMarker];
    					player.x = player.x - ((1-at) * vx);
    					timeXV.splice(collisionMarker, 1);
    					timeYV.splice(collisionMarker, 1);
    					actualTimeV.splice(collisionMarker, 1);
    					velocityYV.splice(collisionMarker, 1);
    					velocityXV.splice(collisionMarker, 1);
    					timeVector.splice(collisionMarker, 1);
    			}
    		}
    Last edited by aleczander; 06-01-2012 at 06:25 AM.


 

Posting Permissions

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