pzaj
pzaj

Reputation: 1092

AS3 transparent png collision detection against a sprite

I am here, because I'm currently struggling with collision detection for my AS3 application. I am loading a bitmap, that represents a room - it has boundaries (walls) and the rest of it is transparent (the floor). I then create a sprite with a circle inside.

I would like my sprite to move within those boundaries and stop at the wall, I'm capable of implementing logics behind that, what I'm asking for is a method to detect the collision with a wall, my whole room is a bitmap, so I guessed I'll just check for the collision with this bitmap, but unfortunately it also counts the transparent parts.

I've done a google research on that, but I only found very complex system which won't work anyway. I'm doing that on learning purposes, so I would like to know how to implement it myself.

Therefore I'm asking if anyone could provide me with piece of code that would check for collision only for non-transparent parts of my bitmap? (Or should I load this png as a vector? how to do so?).

I'm also rotating my "circle", so I guess this should be also considered. I assume I should do bitmap to bitmap check rather than sprite to bitmap?

I have no working code for collisions at all, so I won't provide any.

Should I provide more information please tell me.

Thanks in advance!

@EDIT

this is code I have for my function, it belongs to Room Class.

public function detectCollisionWith(obj:Sprite):Boolean
    {
        var _bitmapData:BitmapData = new BitmapData(obj.width, obj.height, true, 0);
        _bitmapData.draw(obj);
        var _bitmap:Bitmap = new Bitmap(_bitmapData);
        if (_bitmapData.hitTest(new Point(_bitmap.x, _bitmap.y), 255, this.bitmap, new Point(this.x, this.y), 255))
            return true;
        return false;
    }

Upvotes: 4

Views: 1392

Answers (3)

Bennett Keller
Bennett Keller

Reputation: 1714

You can very easily check this when they are both bitmaps using the BitmapData hitTest().

Adobe on bitmapData hitTest():

Performs pixel-level hit detection between one bitmap image and a point, rectangle, or other bitmap image. A hit is defined as an overlap of a point or rectangle over an opaque pixel, or two overlapping opaque pixels. No stretching, rotation, or other transformation of either object is considered when the hit test is performed.


Now an example of how to implement it. If you turn your sprite into a bitmap:

var spriteBmd:BitmapData = new BitmapData( mySprite.width, mySprite.height, true, 0 );
spriteBmd.draw( mySprite ); //get the sprite asset
var spriteBitmap:Bitmap = new Bitmap( spriteBmd ); //create the bitmap to use

Then you can run the hitTest() against the two bitmaps disregarding transparent sections:

if ( spriteBitmap.bitmapData.hitTest( new Point( mySprite.x, mySprite.y ), 
     255, levelBitmap, new Point( levelBitmap.x, levelBitmap.y ), 255 ) ) {
    trace("Collision detected");
}

This checks only the opaque sections of the bitmaps for collision. You can adjust the values 255 in the if statement if you want to increase how much transparency is allowed for detection.

Upvotes: 4

Nicolas Siver
Nicolas Siver

Reputation: 2885

Simple room? Is it simple rectangle? So, I would recommend calculate collision by checking if point in the predefined rectangle. Create Rectangle, inset it's size on radius of your circle, and check if center of the circle inside of rectangle

//Some room
var roomBounds:Rectangle = new Rectangle(0, 0, 700, 500);
const circleRadius:int = 10;
var collisionBounds: Rectangle = new Rectangle(circleRadius, circleRadius, roomBounds.width - 2*circleRadius, roomBounds.height - 2*circleRadius);

//Check if center of circle inside of collisionBounds
//For example you have point with name circlePosition
//collisionBounds.containsPoint(circlePosition)
if(!collisionBounds.contains(circleX, circleY)){
    trace("Houston! We have collision here!");
}

Upvotes: 0

Nadia
Nadia

Reputation: 257

did it many years ago... but if i'm not wrong you can solve the problem creating a MC with the vector shape of the wall (place it below the wall) and check the collision with it rather than with the bitmap.

Upvotes: 0

Related Questions