Jellezilla
Jellezilla

Reputation: 4180

Collision Detection of Sprites in Actionscript 3.0 Flash

I am making an achtung die kurve-like game in AS3.0. So far I've done the movements of the 4 different players, and it works alright.

I am now to make collision detection, in order to test if a 'worm'-so to speak, is colliding with eachother or its own tail.

As I understand it, if I use hitTestObject(); it will use the registration area of the whole object, which would be a huge problem, seeing since this registration makes a 4-sided registration that contains all of the object. So if this is used, it will 'collide' just by entering this rectangle instead of hitting the actual worm. Is this correctly understood?

I've been looking through different methods of collision detection, and can't seem to find an optimal one for my project.

My thought were to check if the 'worms' are drawing their new sprites on a white background. if they aren't, then it must have hit something.

You can see how I used my code here: code in .as format linked to an .fla file

Sorry for my ill-formulated question, hope it makes somewhat sense. Any help is greatly appreciated!!

Best regards - Jesper

Upvotes: 1

Views: 4045

Answers (3)

Torious
Torious

Reputation: 3424

Looking at the screenshots of that game, I think the best model would be to describe each worm as a chain of circles. Then divide the world/level in a grid with cells somewhat larger than the circle radii.

The collision check would then be:

  1. clear grid
  2. place each circle into the 1 or more grid cells it falls in
  3. iterate over all cells, for each cell:
    • for each pair of circles (partially) in this cell, check if they intersect. If they do; collision. Note that this may result in more than 1 collision occurrence between circle A and B, so you'd also need to check that to avoid duplicates.

Step 1 and 2 can be optimized by not clearing the grid, and instead of step 2, updating each circle's cell after it moves. If you size your cells like 5x the size of a circle, a circle can stay in the same cell for a few frames, avoiding excessive add/remove operations.

I'm doing something similar in a project of mine right now, except with space ships! My grid cells are currently 256x256 (too big for your project I think) and my units have radii of about 20.

Upvotes: 0

user985358
user985358

Reputation:

Try this function if you want a Pixel Perfect Collision Detection with efficient CPU usage:

trace("Collided: " + (areaOfCollision(mc1, mc2) != null));
trace("Where: " + areaOfCollision(mc1, mc2));

function areaOfCollision(object1:DisplayObject, object2:DisplayObject, tolerance:int = 255):Rectangle {
    if (object1.hitTestObject(object2)) {
        var limits1:Rectangle = object1.getBounds(object1.parent);
        var limits2:Rectangle = object2.getBounds(object2.parent);
        var limits:Rectangle = limits1.intersection(limits2);
        limits.x = Math.floor(limits.x);
        limits.y = Math.floor(limits.y);
        limits.width = Math.ceil(limits.width);
        limits.height = Math.ceil(limits.height);
        if (limits.width < 1 || limits.height < 1) return null;

        var image:BitmapData = new BitmapData(limits.width, limits.height, false);
        var matrix:Matrix = object1.transform.concatenatedMatrix;
        matrix.translate(-limits.left, -limits.top);
        image.draw(object1, matrix, new ColorTransform(1, 1, 1, 1, 255, -255, -255, tolerance));
        matrix = object2.transform.concatenatedMatrix;
        matrix.translate(-limits.left, -limits.top);
        image.draw(object2, matrix, new ColorTransform(1, 1, 1, 1, 255, 255, 255, tolerance), BlendMode.DIFFERENCE);

        var intersection:Rectangle = image.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);
        if (intersection.width == 0) return null;
        intersection.offset(limits.left, limits.top);
        return intersection;
    }
    return null;
}

After a successful preliminary hitTestObject(), this function backgroundly takes a snapshot from the shapes of both objects painted with different colors each, then overlays them intersecting the colors on a new one, returning the Rectangle of the resulting shape. So cool.

To learn more about Pixel Perfect Collision Detection you can google Collision Detection followed by one of these names: "The ActionScript Man", "Troy Gilbert", "Boulevart (wim)", "Grant Skinner (gSkinner)" or "Senocular". Those guys are awesome AS3 references by the way.

Upvotes: 5

dennis
dennis

Reputation: 2020

The problem you discribe is a very common problem for collission detection because the object has a set width and height and therefor defines a rectangle as the object.

There is a solution however to make a colission detection system on pixel level I have found this on the official site and this made me able to make collission detection for bitmaps on pixel level.

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d55.html

hope it helps you out in the same way.

Upvotes: 0

Related Questions