I am currently creating a personal 2D tile map game in Java. This is the first time I have attempted to make any sort of game, so I don't have a lot of techniques known by experienced developers.
Currently, I am working on the collision detection: I have a small tank (that is rotatable) and walls surround it. My detection of collisions is fine, but it isn't great. The tank has a certain speed which correlates to how much pixels should the tank be redrawn every time the user presses say W or S keys. Right now, the tank has a speed of 2 so it will be redrawn 2 pixels ahead in the desired direction. However, this can create an undesired "bounce" effect where it, even though miniscule, skips over 2 pixels, which means it can skip over walls sometimes and a collision is not detected where it should have been.
Each wall has 4 line segments that construct it. To account for this "bounce" of the tank, I made the wall segments have a max offset of 2 so if the tank is present between a segment of the wall and said segment + 2 pixels, then the tank will be detected.
I thought this was the solution, however, the tank can sometimes stop further away than desired from a segment of a wall, like so (the tank I am talking about is the one to the right. It is rotated. In the picture, the tank won't move any further because a collision is detected, despite pressing down the W key to move it forward (towards the wall on its left, since the tank is rotated leftwards). This is being caused by that wall segment offset):
Summary: the tank is being redrawn every 2 pixels in the desired location. This redrawing skips all the values between those 2 pixels, causing those values to be undetected for collisions. To account for this, the walls are thicker than they seem with an offset of 2 pixels. However, the tank can sometimes stop sooner than expected, creating unprecise collisions.
My question is, are there any techniques to handle this type of situation? Especially techniques regarding the first problem, where there are skipped values.
Some clarification:
- There is not anything wrong with the tank image itself (meaning that the tank picture and its vertices are correctly in place)
- The rotation math is working correctly
- As with the tank picture, the wall picture is placed correctly with its vertices
Here is an instance where the tank is correctly colliding (Rightmost tank):

