Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com AABB math based collision not working so well

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 06-01-2012, 06:21 AM PM User | #1 aleczander 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..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:38 PM.