Sign in to follow this  
dries123

Explosion problem

Recommended Posts

dries123    115
Hi all, I've got a problem creating the explosion when a bullet hits an enemy. (Game is in 2D). I'll draw out the problem: Current explosion: Type: Circular Description: Particles fly in the 6 directions around the enemy. Wanted explosion: When the enemy is hit he explodes, but the particles fly in the 3 directions that go away from the area of impact. Any ideas on how to do this?

Share this post


Link to post
Share on other sites
jyk    2094
If the six direction are fixed (e.g. they correspond to the vertices of a hexagon), you could do it like this:

1. Create a unit-length vector for each of the six potential directions

2. Normalize the projectile velocity vector

3. Take the dot product of each of the six direction vectors and the velocity vector

4. Sort the direction vectors according to the value of the computed dot products

5. Use the three direction vectors with the highest dot product values

Share this post


Link to post
Share on other sites
Thaumaturge    3826
Hold on - I've just realised that you said "away from the area of impact", not "in the direction of the bullet". In that case, if you're comfortable with a circular representation (that is, you're happy with the particles moving away from the point of impact as though they had impacted with a circle):


  • Take the vector between the impacted object and the impact point; we will call this "normal".

  • If you are using six set directions:

    • Use the vector "normal" as jyk suggested you use the projectile velocity vector, or

    • Use an "if" statement to pick the appropriate angles (if angle > 330 or angle < 30, ..., else if angle < 90, ..., etc.).


  • Otherwise, if you are using velocity-based particles (this, admittedly, seems unlikely, given your description, I believe):


    • Add the vector "normal" to the velocity of the created particles.


If you want a more accurate representation, then I would suggest calculating the normal of the impacted object at the point of impact, and using that as I suggested above.

Share this post


Link to post
Share on other sites
dries123    115
Sorry for being that unclear. I'll try to give a better sketch of the problem.

We now use circular explosions: particles fly in random directions away from the object.

We want backward explosions: particles fly away from the area of impact being the point were the bullet hits the enemy.

sry for the inconvenience and thanks for your time

Share this post


Link to post
Share on other sites
Thaumaturge    3826
In that case, I'd suggest either going for my last suggestion above (calculate the normal vector, and add that to the velocity of the created particles), or perhaps this, probably better but also a little more intensive, method:

  • Again, calculate the normal at the point of impact (again selecting from either a circular representation or a more complex one). Normalise this vector if it is not already normalised.

  • Generate your particles as normal, with circularly-distributed velocities.

  • For each particle:
    • Find the scalar projection of the particle's velocity.

    • (Presuming that the normal points out of the impacted object) If thte projection is negative, either:
      • Zero the particle's velocity in this direction, by adding to that velocity -scalar_projection*normal_vector.

      • Reflect the particle's velocity, by adding to that velocity -scalar_projection*2.0*normal_vector


I believe that that should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this