Circular bodies
The next type of body we can use to detect collisions is a circle. For that we are going to consider the diameter of the circle to be the largest of the dimensions of the sprite. We have to add a member variable to ScreenGameObject
named mRadius
and this code to the constructor of the sprite:
mRadius = Math.max(mHeight, mWidth)/2;
Note that other elements that inherit from ScreenGameObject
may want to initialize the radius in a different way.
The calculation of a circular collision is fairly simple: we just have to measure the distance between the centers of the two circles and check if it is smaller than the sum of the radius.
Get Android Game Programming: A Developer’s Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.