Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning AndEngine

You're reading from   Learning AndEngine Design and create Android games with the simple but powerful tool AndEngine

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher
ISBN-13 9781783985968
Length 286 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Martin Varga Martin Varga
Author Profile Icon Martin Varga
Martin Varga
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Collisions


Collisions are an integral part of the physics engine. Algorithms used in Box2D are nicely optimized and we will take advantage of it. However, we will be adding and removing physics bodies during the game because it's usually a good idea to keep the number of bodies to a minimum. Even the best algorithms will take some time to calculate all collisions and too many bodies might mean a significant slowdown.

While adding bodies is usually easy and can be done without any problem, removing bodies must be done at a specific time and it is one of the most common culprits of strange crashes in games with physics.

Detecting collisions

Collisions are detected automatically by Box2D, and collision events are handled in a contact listener. Here's an example of the simplest contact listener. Let's create a class called MyContactListener in the is.kul.learningandengine.scene package and let it implement the ContactListener interface:

public class MyContactListener implements ContactListener...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime