Multithreading Expensive Math Calculations, Is It Safe?

I’ve been running into performance issues when it comes to calculating the collision of bullets on the screen. I use default BoundingSphere, and after awhile calculations seem to add up. I was able to reduce the load by using the distance formula to check if the bullet was close enough to the player to validate a collision check. But if you have 400+ bullets with some of those bullets having heavier update code to run, the distance formula itself begins to slow down the game, especially since there’s alot of other stuff running in the game code.

I was thinking of handling collision on a separate thread, using a task. Would this help remedy the problem?

My main source of lag is coming from my laser object class, the lasers are stacked with 125 BoundingSpheres that only check for intersections if the player is close to them. I had to do this since there’s no way of rotating the bounding box struct nor do I know how to implement something like that.

Yes u can use another thread but you have to make sure they sync and that can be a whole other problem in it’s self.

Now as for your distance calculations are u using distance squared over distance? It’s substantial faster.

Also you could always create an array of ints. Then do a very large broadphase detection every 5 frames or so and add the bullet index to the new array and then only do the other distance calculation with the index bullets

I’m using a custom distance function I wrote. (which is the usual x2-x1 ^2…) Also, when I said 400 calculations, its more like 20,000… You see, the individual lasers detect the player’s distance, and then each of the hitbox managers check, and if you have about 200 lasers with a 128 hitboxes it gets hectic. However, if distance squared works faster I may try that. But I also want to test using a separate thread first.

To prevent problems like deadlock, should I start them at the same time or start the task a few frames after?

Store your Objects in an octree or similar data structure to make the checks cheaper