I’m currently working on remaking a game I made a few years ago, and it didn’t occur to me until recently that the new code is very framerate dependent, in that the speeds of various game entities vary depending on what framerate you’re getting. I’m making the new version of the game using Slick2D.
I modified the calculations that “move” the projectile to take deltaTime into account, and this has got me some better results with various frame rates, but now I’m running into another issue. Specifically, the accuracy of my projectile collision detection varies depending on the frame rate. The faster the frame rate, the more accurate it is. I can understand why this is, as with a slower frame rate, the projectiles will be moving further along their path, meaning they could skip right past the enemy collision radius; so I’m trying to think of how I could modify the existing system to give me better results that would be consistent regardless of frame rate.
I’ve thought about using raycasting to decide whether the projectile hits. I was about to start writing the code so that the raycasting would be done when the weapon fires, so it would determine then if there is a collision, and then just fire the projectile really fast so it gets there before the enemy can move away from it.
I realize that this is not a good approach, because this means the enemy will take damage before the projectile actually arrives, which would result in some… interesting visuals. So would it be feasible to have the projectile simply raycast along its own trajectory and if it’s within the enemy’s collision radius, then it hits?
Currently, the collision detection is:
But as mentioned before, the varying frame rate can cause particles to completely miss just because of lag. So my new idea is…
I feel like I’m confusing myself every time I try to reason this out. Can anyone offer advice?
My current code base is available at: https://github.com/packetpirate/Generic-Zombie-Shooter-Redux
Relevant Files:
I’m also open to any suggestions about the rest of my code, specifically any design patterns I could use to make things easier to modify or anything that I could do differently.