Java Smarter collision detection

I am making a game in java that involves characters moving around a map and having some solid collision objects (i.e. buildings) placed around the map by reading certain data from a text file. There will be several maps in which the locations of these objects will be changed. My question will draw a rectangle in a specific color that indicates a collision behind such structures, or will read the coordinates of the mouse and search for an array of these structures to see if this point is on a building, thereby negating a move or change, be more inventive and / or faster. If drawing a rectangle is better, it is better to leave it behind the structure or remove it after a collision is detected. Thank you for your time!

+3


source to share


2 answers


In my junior year in college, I worked on an algorithm for a collision detection system for a Windows phone. It's hardly perfect, but it was extremely effective and adaptable to most games.

The way it worked was pretty simple. There were two types of objects; Colliding objects (such as enemies or buildings) and objects that you want to check for collisions with these collimated objects.

I had this idea when I was going through the data structures class and we talked about Linked Lists. I thought that if each link is a colliding object, you can insert your game objects that were already created in it. Then, as the game objects moved, you would have an easy way to check your collision spots. Thus, my system was born.

All there really is is a class that fires either in every game loop, or when you decide to check for conflicts. You specify your player's location, or the location of the marker, or what ever was an object that you want to see if it collides with something and it looks for all localizable objects and tests to see if they overlap.



Its real effectiveness comes into play when you add the second element (Locations AND quadrant)

For example, if I break my phone screen apart and I know which quadrant my player or marker is in. I can only choose to view the list of objects that can be collapsed in this quadrant. Thus, you cut your search algorithm by a quarter of its original size. enter image description here

There are many ways to detect collisions. This was a simple example that I used in my class to show how you could find two opposite circles that were actually squares. As you can see, simply by taking the center point coordinates of the circles and the radius, you can calculate the hypotenuse and determine where and when they touch. enter image description here

Good luck! if you have any questions, do not hesitate to ask!

+5


source


The last answer in this post might help you. This is a simple maze. The maze structure is controlled by a data file that simply contains 0, 1 to indicate a path or wall. You navigate the maze using the arrow keys. When the arrow key is pressed, the code checks that the next square is not a wall.



0


source







All Articles