Tuesday, January 10, 2012

Efficient bounding box collision

I made a bounding box collision that uses bit-shifting.
By multiplying the bounds of each object and then rounding the result,
I get 2 numbers between 0-31 for the min/max on the Y and the min/max on the X.

I then create two unsigned integers that have bits set in a pattern to represent
the space the object takes up on the X and Y axis. After that, I sort the object
on both the X and Y axis by 32 different rough positions.

After that, I do collision tests between the first sorted object in the sorted
X array. If X is NOT colliding with the next sorted object on the X-axis, I know that object collides with nothing. If it DOES collide on the X, I check the Y to
see if it is also colliding on the Y.

If it is NOT colliding on the Y, rather than go in a pattern of:
1. Look at next x. Then look at that x's y if they are colliding...

I go back and forth between going through the sorted x's and going through the
sorted y's. I do this to avoid a worst case scenario where ALL objects are colliding
on the X axis, yet very few are colliding on the Y. Or vise versa.

Also, if "A" is colliding with "B", we do NOT test "B" against "A".

Something is wrong with calculating the bounding box after scaling objects in AS3.
At least, with how I am doing it. After I figure that out, I will use the bounding
box to create an inscribed circle to turn my bounding-box collision into circle-circle collision.


2 comments:

  1. TODO:
    Jaron says explosions should STOP moving.
    Sounds good.

    ReplyDelete
  2. Jaron says ship is to big.
    Size reduction 20 to 30%.

    ReplyDelete