apscience
apscience

Reputation: 7243

Manual bounding box collision detection in AS3

I need to do bounding box collision detection manually. My current implementation sees bullets passing right by enemies. Not only that, sometimes it seems they get hit later..

For the objects that I want to hittest, I have getters like this:

    public function get left():Number{
        return x - width / 2;
    }

(The 'crosshairs' are at the center of the movieclips)

When I check for collisions, I use this:

if(this.leftX >= Main.player.leftX && this.rightX <= Main.player.rightX && this.topX >= Main.player.topX && this.downX <= Main.player.downX){

The bullet has a x of 4, a y of 13, and the player is a 20 by 20 square.

Are ther better ways to do this or should I fudge the numbers a bit?

Upvotes: 1

Views: 897

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

You are checking "this square is inside player's square", you want to check if any of corners are inside the other one. Distance between centers will be easier to write and may work as well for such a small objects.

Upvotes: 1

Related Questions