Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-12-2012, 11:53 PM   #1
lordoftherock
Registered User
 
Join Date: Apr 2012
Posts: 40
Default Collision Testing

hi,
I am able to test if my objects are colliding with eachother but how would i go about testing if it is a topside, bottomside, leftside or rightside. I am using bitmaps to get pixel perfect collision.
thanks
lordoftherock is offline  
Old 05-13-2012, 01:32 AM   #2
backshesh
Registered User
 
Join Date: May 2011
Posts: 33
Default

you have to make a collision sensor for the top bottom left and right side. And when that sensor is activated it activates a code saying the top bottom left and right side where just activated.

Just wondering but what is your method for the collision detection. I am working on a project right now and I am working on a bitmapdata collision detection thing. Just curious.
backshesh is offline  
Old 05-13-2012, 03:52 PM   #3
mushroom6969
Registered User
 
Join Date: Mar 2012
Posts: 42
Default

im interested how would you do this im working on a similar thing
mushroom6969 is offline  
Old 05-13-2012, 03:53 PM   #4
lordoftherock
Registered User
 
Join Date: Apr 2012
Posts: 40
Default

how exactly would i make a collision sensor. When doing collisions for rectangular objects i have just drawn out 4 separate rectangles for top, left, bottom, right and tested for collisions with those, but now for bitmaps that wouldn't work.

My method is essentially what is done here: mikechambers(dot)com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection
lordoftherock is offline  
Old 05-14-2012, 05:28 PM   #5
lordoftherock
Registered User
 
Join Date: Apr 2012
Posts: 40
Default

Anyone?
lordoftherock is offline  
Old 05-14-2012, 05:46 PM   #6
abeall
Senior Member
 
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,812
Send a message via AIM to abeall
Default

Mike Chambers example is great, but BitmapData/hitTest is not a good method to determine angle of collision, since it only tells you whether two shapes overlap and doesn't give you any additional information.

One way to determine the angle of a collision is to do a series of hitTestPoint checks in a circle, then take the average vector of all hits in that circle. For instance:

ActionScript Code:
/**  * Test for a hit against a shape based on a point and radius,  * returns the angle of the hit or NaN if no hit.  */ function hitTestPointAngle(object:Sprite, xPos:Number, yPos:Number, radius:Number, samples:uint = 90):Number {     const PI2:Number = Math.PI * 2, SAMPLE:Number = 1 / samples;     var dx:Number, dy:Number, a:Number, tx:Number = 0, ty:Number = 0, hits:int = 0;     var i:int = samples;     while(i--){         a = PI2 * (i * SAMPLE);         dx = radius * Math.cos(a);         dy = radius * Math.sin(a);         if(object.hitTestPoint(xPos + dx, yPos + dy, true)){             hits++;             tx += dx;             ty += dy;         }     }     if(!hits)         return NaN;         return Math.atan2(ty, tx) * (180 / Math.PI); }

ActionScript Code:
// Example: var hitAngle:Number = hitTestPointAngle(walls, player.x, player.y, 15); if(!isNaN(hitAngle)){     // move player 5px away from walls     player.x -= Math.cos(hitAngle * (Math.PI / 180)) * 5;     player.y -= Math.sin(hitAngle * (Math.PI / 180)) * 5; }

You could also check out the open source "collision detection kit":
http://code.google.com/p/collisiondetectionkit/
__________________
Aaron Beall | Flash portfolio | Fireworks extensions | Twitter

Last edited by abeall; 05-14-2012 at 07:15 PM.
abeall is offline  
Old 05-15-2012, 02:59 PM   #7
lordoftherock
Registered User
 
Join Date: Apr 2012
Posts: 40
Default

hi thanks so much for that! I don't think i could have done it on my own!
However, the collisions seem to be innacurate:
ActionScript Code:
public function movePlayer(event:Event):void { //other stuff.. collisions(); } public function collisions():void {   for(var i:int=0;i<collisionObj.length;i++) { //loop through objects                         var hitAngle:Number = hitTestPointAngle(collisionObj[i].mc, player.x, player.y, 15);             if(!isNaN(hitAngle)){                 // move player 5px away from walls                 player.x -= Math.cos(hitAngle * (Math.PI / 180)) * player.speed*timeDiff;                 player.y -= Math.sin(hitAngle * (Math.PI / 180)) * player.speed*timeDiff;             } } }
it is identifying the collision when the player is no where near the object?
thanks very much!
lordoftherock is offline  
Old 05-15-2012, 03:14 PM   #8
abeall
Senior Member
 
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,812
Send a message via AIM to abeall
Default

You would need to set the radius argument to the radius of your player.

Also looping through all your collision objects is not the way to do this. Put all your collision objects in a container and just check once against the container.
__________________
Aaron Beall | Flash portfolio | Fireworks extensions | Twitter
abeall is offline  
Old 05-15-2012, 04:11 PM   #9
lordoftherock
Registered User
 
Join Date: Apr 2012
Posts: 40
Default

ok thanks,
the radius of my player is 7.5px, yet this hasn't solved the problem. Turning off the scrolling in my game seems to make it work slightly better, yet the collisions are still inaccurate. The collisions (which still are being detected far away from the object) seem to make the player move in an orbital motion.
lordoftherock is offline  
Old 05-15-2012, 05:18 PM   #10
abeall
Senior Member
 
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,812
Send a message via AIM to abeall
Default

hitTestPoint requires the x and y coordinate to be in stage coordinates. Since your player object is in a container that is being scrolled, the player x and y needs to be convert to stage coordinates:

ActionScript Code:
var point:Point = localToGlobal(new Point(player.x, player.y)); var hitAngle:Number = hitTestPointAngle(collisionObjContainer, point.x, point.y, 15);
__________________
Aaron Beall | Flash portfolio | Fireworks extensions | Twitter
abeall is offline  
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Jump


All times are GMT. The time now is 11:39 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.