I’m trying to use Cory Oneil’s Collision Detection Kit for managing collisions. The problem I am coming across is I will be having a lot of different objects that collide with each other.
Naturally, the CollisionGroup class is the right choice. I want to test collisions of a specific object moving with any number of speeds in the X and Y axis, and then control its movement based on that. So, in the main movement function of the object I am setting the forceX and forceY of the object. I am then checking for collisions and attempting to make the object move accordingly.
private function Walk() : void
{
for ( var i:uint = 0; i < 4; i ++ )
{
if ( key[ i ] == true
&& dir != i
&& ( oldkey[ i ] == false || key[ dir ] == false) )
dir = i;
}
if ( dir == 0 || dir == 2 )
{
forceX = 0;
forceY = ( key[ 2 ] - key[ 0 ] ) * speed;
} else
{
forceX = ( key[ 3 ] - key[ 1 ] ) * speed;
forceY = 0;
}
var collisions:Array = collision.objectCollisions( _block );
if ( collisions.length )
{
var collisionInfo:Object = collisions[ 0 ];
var angle:Number = collisionInfo.angle;
var overlap:int = collisionInfo.overlapping.length;
var sin:Number = Math.sin( angle );
var cos:Number = Math.cos( angle );
var vx0:Number = forceX * cos + forceY * sin;
var vy0:Number = forceY * cos - forceX * sin;
forceX = vx0 * cos - vy0 * sin;
forceY = vy0 * cos + vx0 * sin;
forceX -= cos * overlap / 5;
forceY -= sin * overlap / 5;
}
x += forceX;
y += forceY;
}
Basically, I am drawing a rectangular object defining the size of the object. I am applying the forceX and forceY to the object at the end of the function. The object known as “_block” is a simple rectangular sprite. The problem is that all of the examples I’ve seen so far for CDK use small perfect, falling, side-view spheres. My objects will be moving any direction and their “_block” will be any shape.
This is not working. Instead the objects are jumping a little and sometimes going through objects. It’s really buggy.
Is there anyway to make objects that move in any direction on the X and Y axis in any shape collide with any shape and perfectly stop on pixel-perfect collision? Any good examples with CDK?
Note: To reduce processing, I created a function inside of CollisionGroup class which merely returns an array of collisions to a specific object rather than an array of *all collisions *with all objects, as you see in my code collision.objectCollisions( _block );.