0

I've tried using hitTestObject(), but that seems to require that I make the call using a specific instance, rendering it more or less useless unless I want to have dozens of objects making dozens of collision checks each frame, which just seems wasteful and annoying to implement.

Is there any way to do collision check based on class rather than instance?

Maybe Something equivalent to this: http://docs.yoyogames.com/source/dadiospice/002_reference/movement%20and%20collisions/collisions/place_meeting.html

Alternatively, is there any function that returns whatever a list of objects that share overlapping coordinates with the one I'm checking?

1 Answer 1

1

If I'm understanding your question correctly, you have several objects of that same class that each need to check for collisions against each other?

Yes, you would have to go through each object and perform a collision check against the other objects. I suppose you could write a hitTestClass function yourself, but behind the scenes it would still be the same. As far as implementing it, it's not so bad:

for( var i:int = 0; i < asteroids.length -1; ++i )
{
    var a:Asteroid = asteroids[ i ];
    for( var j:int = i+1; j < asteroids.length; ++j )
    {
        var b:Asteroid = asteroids[ j ];
        var isColliding:Boolean = a.hitTestObject( b );
        //Code here to do whatever in the case of collision
    }
}

If computational speed becomes a concern, then there are broad-phase collision detection techniques to chunk down the time. Quad trees are one example.

Sign up to request clarification or add additional context in comments.

5 Comments

I am confused about the ranges you're iterating over; why does j begin at i + 1? Your code could be simplified to something like this.
@Marty It prevents from unnecessarily checking each pair of objects twice. If there's an array of 3 objects, {a,b,c}, then with the loops in my answer, a-b, a-c, b-c is the checking order. If you do the two foreach loops with your example, a-b, a-c, b-a, b-c is the checking order.
Well the main problems is that it evaluates at O(n^2), which is fine for small numbers of objects, but as I said I'm planning to use dozens of objects, so it's just not a viable solution
en.wikipedia.org/wiki/Quadtree will help you then. Collision detection's a deep topic...
Box2D is a 2D physics library for Flash if you want to go that route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.