🎉 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++?

Started by
15 comments, last by JoeJ 3 years, 2 months ago

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!

Advertisement

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 tone posse, ready for action.

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?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Yes, should I check after every update?

@Kookies_N_Kareem You check during every update. That's what updates are for.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Sorry, I'm new to this, it's my first game without an engine. Thanks for your help!

@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.

http://gameprogrammingpatterns.com/game-loop.html

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

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

JoeJ said:
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).

This is what I'm currently doing, however, I agree that it's overly taxing, but I don't understand the alternative. Could you expand?

This topic is closed to new replies.

Advertisement