I'm building a simple, minimal game where the user can launch projectiles at an enemy and the enemy will die if they get hit enough times. I figured out a collision system that works that can detect if an enemy was hit, but only when the collision check is run. However, I want all projectiles that hit the enemy to count, should I use threads to constantly check if the enemy was hit, and if so can someone show how? Or is there another way to do this? Any help is greatly appreciated!
๐ Celebrating 25 Years of GameDev.net! ๐
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
How can I check if an enemy was hit in C++?
Kookies_N_Kareem said:
I figured out a collision system that works that can detect if an enemy was hit, but only when the collision check is run.
How is this a problem? That's the way it usually works.
The problem I'm finding is that the check is only run at certain times, and projectiles have hit the enemy between checks but by the time the next check is run, the projectile isn't colliding anymore
Kookies_N_Kareem said:
I'm finding is that the check is only run at certain times
Do you know how to do object updates in a game loop?
@Kookies_N_Kareem No need for sorry.
Check this page out for info on game loops. It's overly verbose, don't expect to understand everything at once.
If your projectiles are points, you need to do the collision test at high frequency so points move in small steps, which is necessary to prevent missing collisions becasue projectiles tunnel through enemies. To reduce this frequency, you can test against the line between two timesteps of the point shaped projectile instead. With this the projectiles can move large distance per step and you still don't miss collisions. (It's a form of โcontinuous collisionโ detection then.)
To speed up the process of finding collisions itself you want to avoid to test every enemy against every projectile (if you have lots of those). So you need a spatial acceleration structure, like regular grid, multilevel grid (if size of objects differs a lot), or if memory becomes an issue quad/octree, or BVH, etc. This way a projectile only tests enemies which are close to it. (โbroadphase collision detectionโ)