arjun wrote: ↑Thu Dec 23, 2021 2:46 am
I'd like to know more about how you get away with just using 2 AABBs for collisions. Since the car is squished along one axis more than the other (i.e rectangular) don't you need 3 AABBs at minimum? One for vertical, one for horizontal and one for diagonal?
The first reason for choosing 2 AABBs was that my initial tests with 1 just felt rubbish. Far too much overlap between the colliding sprites! I had anticipated this, however we have to start somewhere
Tests with 2 were much more satisfying, and there was much less "catching" of protruding corners on each other than I had anticipated. There's still a little visible overlap between the colliding sprites of course, but this does help make the cars seem more malleable rather than being rock-solid.
Speed was the most crucial factor, however. When two cars collide with 2 AABBs per car, you have to consider collisions between:
Code: Select all
a_outer --- b_outer
if collision:
a_front --- b_front
a_front --- b_rear
a_rear --- b_front
a_rear --- b_rear
Upping this to 3 AABBs, this becomes:
Code: Select all
a_outer --- b_outer
if collision:
a_front --- b_front
a_front --- b_middle
a_front --- b_rear
a_middle --- b_front
a_middle --- b_middle
a_middle --- b_rear
a_rear --- b_front
a_rear --- b_middle
a_rear --- b_rear
So the code size and computation time for resolving collisions both rise exponentially (power 2) as we add more AABBs.
It would also mean that the AABBs would have to be variable size - at present, they are
all 8x8 pixels for cars - which would require more memory reads when updating the AABBs. At present, I only need to look up 4 values per AABB (top-left offset of each AABB from the car's origin). I would need a further 4 lookups (width & height of each AABB) to implement variable AABB dimensions on a 2 AABB car. For 3 AABBs, this rises to a total of 12 lookups vs the current 4.
Note, even 2 AABBs is somewhat wasteful, as when the car is facing N/E/S/W the car can be modelled accurately with 1... but the logic to figure this out on a case-by-case basis is likely to nearly as long as just getting on with it and processing both, and would chew up more memory with the extra code too.
So yeah, 3 AABBs would be nice - but was deemed not feasible for such a small boost in accuracy.
Hope that answer your question