Beginning Android Tablet Games Programming by Jeremy Kerfs
Author:Jeremy Kerfs
Language: eng
Format: epub, pdf
Publisher: Apress®
Published: 2011-11-22T16:00:00+00:00
Detecting Collisions Between Sprites
The next big change requires an entirely new function in the SpriteObject class to deal with collisions. If you have done 2D collision detection before, the solution will look familiar. The function tests two rectangles for a collision. Recall that because the screens in Android have their origin in the upper-left corner, if the bottom of the first sprite is less than the top of the other sprite, then there is no collision because the first sprite is above the second one on the screen.
If there is a collision between the two sprites, then the new method returns true. Intriguingly, when you search for collisions, you use the bitmap to gather the width. Your sprite class doesn’t store the width or height directly because it’s already contained in the bitmap. You use this approach to get the dimensions of a sprite later for collisions with walls.
As with any function that requires a series of if statements, your collision detection is moderately expensive in terms of processing. You want to eliminate needless collision routines if possible. This is, however, much better than doing pixel-by-pixel detection that can cause games to reach a near standstill.
Add the function in Listing 5-2 to the SpriteObject class.
Listing 5-2. The collision detection function in SpriteObject class
public boolean collide(SpriteObject entity){
double left, entity_left;
double right, entity_right;
double top, entity_top;
double bottom, entity_bottom;
Download
Beginning Android Tablet Games Programming by Jeremy Kerfs.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(14778)
The Mikado Method by Ola Ellnestam Daniel Brolund(12038)
Hello! Python by Anthony Briggs(11949)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(11368)
Dependency Injection in .NET by Mark Seemann(11134)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10528)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(9966)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(9573)
Grails in Action by Glen Smith Peter Ledbrook(9292)
Hit Refresh by Satya Nadella(9039)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(8893)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(8759)
The Kubernetes Operator Framework Book by Michael Dame(8473)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8360)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8294)
Robo-Advisor with Python by Aki Ranin(8247)
Practical Computer Architecture with Python and ARM by Alan Clements(8223)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8192)
Building Low Latency Applications with C++ by Sourav Ghosh(8096)