![detecting collision java lwjgl detecting collision java lwjgl](https://bs-uploads.toptal.io/blackfish-uploads/components/blog_post_page/content/cover_image_file/cover_image/686197/retina_1708x683_cover-0907_VideoGamePhysicsTutorial_Razvan_Newsletter-40bbf9fce5c754d1f3687c3bd8b05eb3.png)
The way to determine the triangle associated to the current position, is by checking if the z z z coordinate is above or below that diagonal. You can see that the cell is divided by a diagonal that separates the two triangles. How we can do this ? Let’s examine the triangles that form a single cell. With all that information we are able to calculate the positions of the vertices of the triangles contained in the cell. The following picture shows all the variables described above for a sample terrain block. private Box2D getBoundingBox (GameItem terrainBlock) r o w = b o u n d i n g B o x. We will create a new method that calculates the bounding box of a terrain block, named getBoundingBox. By doing this way we access them later at any time without repeating those operations in each game loop cycle. Since the terrain will not be displaced or scaled at runtime, we can perform those calculations in the Terrain class constructor. In order to do that, we will calculate the bounding box of each terrain block taking into consideration the displacement and the scaling.
![detecting collision java lwjgl detecting collision java lwjgl](https://i.ytimg.com/vi/JIxV-LXqa1g/maxresdefault.jpg)
So, what we need to do first is determine in which terrain block the current position, the camera, is in. So we already have one of the components of the comparison, thus, the next thing to calculate is terrain height at current position.Īs it's been said before, the terrain is composed by a grid of terrain blocks as shown in the next figure.Įach terrain block is constructed from the same height map mesh, but is scaled and displaced precisely to form a terrain grid that looks like a continuous landscape. Since we do not have yet a player concept the answer is easy, the current position will be the camera position. The first thing we need to define is what we understand for the term "current position". Simple concept, does it ? Indeed it is but we need to perform several calculations before we are able to do that comparison. If we are above terrain’s y y y value there’s no collision, if not, we need to get back. In order to detect a collision we must compare current position y y y value with the y y y value of the point of the terrain we are currently in. The height map is used to set the height of the vertices that compose the triangles that form the terrain. If you recall from previous chapter, a terrain is composed by blocks, and each of those blocks is constructed from a height map. Once we have created a terrain the next step is to detect collisions to avoid traversing through it.