05-12-2012, 11:53 PM
|
#1
|
|
Registered User
Join Date: Apr 2012
Posts: 40
|
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
|
|
|
05-13-2012, 01:32 AM
|
#2
|
|
Registered User
Join Date: May 2011
Posts: 33
|
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.
|
|
|
05-13-2012, 03:52 PM
|
#3
|
|
Registered User
Join Date: Mar 2012
Posts: 42
|
im interested how would you do this im working on a similar thing
|
|
|
05-13-2012, 03:53 PM
|
#4
|
|
Registered User
Join Date: Apr 2012
Posts: 40
|
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
|
|
|
05-14-2012, 05:28 PM
|
#5
|
|
Registered User
Join Date: Apr 2012
Posts: 40
|
Anyone?
|
|
|
05-14-2012, 05:46 PM
|
#6
|
|
Senior Member
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,682
|
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/
Last edited by abeall; 05-14-2012 at 07:15 PM.
|
|
|
05-15-2012, 02:59 PM
|
#7
|
|
Registered User
Join Date: Apr 2012
Posts: 40
|
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!
|
|
|
05-15-2012, 03:14 PM
|
#8
|
|
Senior Member
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,682
|
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.
|
|
|
05-15-2012, 04:11 PM
|
#9
|
|
Registered User
Join Date: Apr 2012
Posts: 40
|
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.
|
|
|
05-15-2012, 05:18 PM
|
#10
|
|
Senior Member
Join Date: Feb 2006
Location: Washington, DC
Posts: 2,682
|
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);
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 07:26 PM.
///
|
|