Programming Games for Android

Programming Games for Android 19: detect Collisions

42 videos

238 minutes

Let's talk about collisions because I obviously interested to know that my player has been hit by one of the spikes. The first thing I do is look for on one thing that has the Stage class to get the boundaries of objects, and debug mode. I'll show how to enter debug mode to see in detail the actors we have on stage, and you will understand why. First I call setDebugAll method to indicate that when we run

show the edges of objects. If we pass true, the edge of each object in a line that will border the actor so we know your limits will appear. Need to work to give the actors a few sizes, and we have not done, I've only given positions. The first thing I will do is give the dimensions of the actor. And I can do that directly from the manufacturer, because the textures and TextureRegions have methods for

the width and height of the texture, and it is perfect for me to tell my actor, 'Hey, your new size (with setSize) is pinchos.getRegionWidth () for the width of the region, and pinchos.getRegionHeight (), to obtain high '. Thus we know how tall the ActorPinchos: which measure the texture. With ActorJugador is the same. This you what you know because I did it in the first videos: getWidth () and getHeight ().

This combined with this other, because we need its dimensions, it allows us to directly obtain this green edge going to see now around our character. With this more or less what we can do is to determine what the limits are, and will help us to see how we can make is detected when there has been a collision. I will return to my Skitch to understand how I will manage collisions. There are better ways to manage them, and soon

let's see how we can start introducing Box2D, a very interesting technology that will allow us to add realistic physics to the game, and how to better detect a collision is obtained. For the moment we will study how to obtain a simple algorithm to open mouth for later. At this point we can reduce our actor to a square. All actors, in fact. We have a player and have a skewer.

What interests us is to detect when this has occurred: ie when the spikes have clashed with the actor. Apparently there is a simple way to see when we hit; and here we have this vertical line, which would be corresponding to the X position of the player. When we say setX () it we indicate is where it gets this line because the origin of coordinate indicated on the left side. We know what wide is the actor,

so it is easy to determine the right edge making a sum of X + WIDTH. Similarly, we can get the left edge of our skewers using X in the case of the skewers. When a collision occurs, an interesting thing happens: the collisions occur because the left edge our ActorPinchos is more to the left, ie, the X worth less, than the value of X + Width of spikes. If we make a comparison between pinchos.x

and (jugador.x + jugador.width), what happens is that there is a collision is because the former is smaller than the second. If this condition is true, there is a collision, because the spikes are literally inside the player. This is the first strategy for detecting collisions. Using the render method before calling draw () a method call called comprobarColisiones () that will create now. There is some discrepancy against

when to update and drawn. Do not be fooled: always Actualizad before drawing. Because if you do not, if you do it the other way around, ie, meteis the draw () first, you have a problem, because the collisions will be checked after making the act (), and the act () will not be the the player is watching. As the draw () was done before the player does not perceive. And in games that have a lot of movement this is a problem,

so first would do the updates and checks and finally when everything is ready dibujadlo. Especially with Box2D, this will be very important. The last thing I will do is write the comprobarColisiones () method to check what I have said. If the right edge of my player is greater than the left edge of my spikes, is because there has been a collision. Good time to kill the player, basically. Le I can put a condition

it is alive, and by a getter and a setter I can modify or check if my player is alive or not. Now what I do, finally is that if the player is alive and he has collided, collision printed. I want to do this, because if not, the message collision while moving the spikes are followed it will appear dozens of times. To finish being well is the setAlive is made false to not again true worth. This is to avoid printing

often the message collision. This is a somewhat crude and dirty way but allows checking by a mathematical model ... - good. I'll see why it does not work. Maybe because you forgot to put this. Yeah true. Pardon this detail: when you start the game, the live start. Yes now. He said that by basic geometric models such as rectangles we can see when there has been a collision. This technique is called bounding boxes.

It is not the best technology to check collisions but basic 2D games is. However, with Box2D we have a better system to integrate physical.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.