Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the blast area of a bomb?

I'm developing a game, let's say player a put a bomb in location x=100,y=100 and the explosion radius is 100 units... It is pretty easy for me to find all the "items" in the game that were hit by the blast of the bomb (just need to check that their distance from the bomb is lower then 100).

But now i want to take in consideration the obstacles i have in the game, the obstacles are squares, always 64*64 pixels, always aligned to the axis (not rotated).. i want to know if an item was "hidden" behind an obstacle to know he wasn't hit ...

something like this :

enter image description here

The dude on the right wasn't hit, but the dude on the bottom was hit, i filled in gray the hit area, and in green the area which is hidden...

My idea is : 1. find all the items in the scene that their distance from the bomb is lower then 100. 2. find all the obstacles in the scene that their distance from the bomb is lower then 100. 3. calculate the lines from the items to the center of the bomb .. then check if the lines intersect any obstacle , if no .. you were hit.

Finaly, the questions 1. Does any one has a better idea ? 2. Are there free opensource c# compatible engines that can help me ? Box2d can help me here ?

Thanks

like image 329
OopsUser Avatar asked Nov 24 '13 19:11

OopsUser


Video Answer


1 Answers

It is quite simple, and as Jongware mention in the comments, you should use two lines of visibility.

You should compute the lines of visibility from each "side" of the items in the picture. The origin of each visibility line can be approximated by computing the line from the center of the bomb and get the direction normal to that vector. Your two visibility points are then located one radius out from the center of the item in the normal and negative normal direction. This circle approximation might not represent all possible shapes very well, but is generally a good enough approximation for simple games (and your items look circular in the drawing).

Java-isch pseudocode using 2D-vectors:

// bombCenter and itemCenter are 2D-vectors
bombDirectionVector = bombCenter.minus(itemCenter);
normal = bombDirectionVector.getNormal()    // normal vector of length 1
viewPoint1 = itemCenter.plus(normal.times(itemRadius));
viewPoint2 = itemCenter.minus(normal.times(itemRadius));
// Check obstacle intersection with the lines from viewPoint{1,2} to bombCenter
// ...

The visibility lines will then go from the points on the sides of each item to the bomb center. So, for each item, you check if the two visibility lines intersect either the same obstacle or two connected obstacles.

There is no free open source C#-compatible engines I know of that does this, but the only part that can be a bit tricky is the obstacle intersection check. So if you just find something to help you with the intersection check, then the rest should be very straight forward to implement.

I hope this helps, and let me know if anything is unclear and I'll clarify the answer accordingly.

like image 57
mags Avatar answered Oct 12 '22 08:10

mags