2
\$\begingroup\$

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):

Tank not doing precise collision (Rightmost tank)

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:

  1. There is not anything wrong with the tank image itself (meaning that the tank picture and its vertices are correctly in place)
  2. The rotation math is working correctly
  3. 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): Tank correctly colliding (Rightmost tank)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ An option to consider is to "push back" the tank out of the wall once you detect a collision. e.g. if the tank goes from A to B in one frame, once it's moved to B, check if it collides with anything. If it does, define an "imaginary line" using something like Bresenham's line algorithm between B and A, and push the tank back one pixel at a time, checking for collision each time. Once the tank does not collide with anything, you got your tank out of the wall. \$\endgroup\$
    – Vaillancourt
    Jul 12, 2021 at 14:56
  • \$\begingroup\$ (This may be subject to tunneling if your tank goes "too fast".) \$\endgroup\$
    – Vaillancourt
    Jul 12, 2021 at 14:57
  • \$\begingroup\$ @Vaillancourt ♦ Thanks for the comments and links. I think I’m going to take an idea of your push back method: however far away the tank is to the segment of a wall when colliding is how much I push it towards the wall in ofc the right direction. And thanks for those links again, it’s nice to learn new things that I could utilize in the future :) \$\endgroup\$ Jul 12, 2021 at 15:53
  • \$\begingroup\$ If you need to research more on this topic, the terms you likely need to search for are "pixel perfect collision". Another thing I may suggest is to think a bit "forward", e.g. what if you realize that your tanks appear too slow and you need to move them by 10 pixels instead of 2? But, you know, you'll get there eventually :) good luck with your project! \$\endgroup\$
    – Vaillancourt
    Jul 12, 2021 at 16:34
  • \$\begingroup\$ @Vaillancourt ♦ Ah thanks for the advice and link. Thanks \$\endgroup\$ Jul 12, 2021 at 18:17

0

You must log in to answer this question.

Browse other questions tagged .