Explosions?

so, some of you probably saw this question on BSE http://blender.stackexchange.com/questions/36780/creating-an-explosion-in-blender-game-engine, but i coudln’t figure it out.
@Monster recommended that the OP increase his speed that he played his animations at. Unfortunately, this caused physics errors that he was unable to resolve with higher physics steps due to obsolete hardware.

^situation^

now i’m wondering, id there a way that i could do an explosion adding an empty at the event point and then measuring with a near sensor
[Near; character]>[python] with the python being along the lines of

Near.hitObject setVelocity[awayFromEventCenter]

from that line you can see that i don’t python. but that^ is kinda what i’m looking for. is this possible?

So you want an explosion that launches objects (in its near-sensor detection range) away from it?


import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner

force = 10.0 # controls how powerful the explosion is

near = cont.sensors["near"]

if near.positive:
    for obj in near.hitObjectList: # for every object detected by the near sensor
        vec = obj .getVectTo(own) # gets distance, global vec-to, local vec-to
        vect = vec[1] # get dat global vec
        dist = vec[0] # oooo get dat distance too
        totalForce = force/dist # oh snap divide your force coefficient by distance so stuff further away experiences less force
        obj .applyForce([-totalForce * vect[0], -totalForce * vect[1], -totalForce * vect[2]], False) # APPLY THAT SHtuff.
        # The inputs here are:  [X force, Y force, Z force], True/False (controlling whether the force is applied on global or local axes)

That should just about do it. You might need to add “import mathutils” at the top but I doubt it. Should work alright. For the setup, you want a near sensor named “near” (wow crazy I know) connected to the python controller on the empty object you’re adding at the explosion event. That should be it! :smiley:

I frequently do this with collsion and a sphere, but near is nearly the same thing,

:smiley:

(but you can’t pick collision layers for near)

kaboom.blend (557 KB)

Here is pretty much the code posted above, in action.

Hit spacebar to make the red cube explode.

The top-level script keeps a running list of near objects, and shuffles that off to a property of the grenade object.

When detonation() is triggered (spacebar here), it takes that list, finds the vector, distance and applies force, to each object in the hitList.

Kaboom!

Was just thinking

near, + raycast to each target could make a explosion that respected walls etc,

and ‘throwing yourself down on the grenade’ really would block everything around from the hit, maybe even a second or third raycast depending what strength of armor? (armored units block explosions better then unarmored units?)

like

if damage multiplyer remainig >quit threshold:
cast rays again after target and remove value based on armor level of last hit.

I wonder how expensive(cpu cost) it would be
to cast 32 rays 6 units long? and use vector reflect :, apply impulse at hit point, and armor etc?

Casting rays from the blast point would be a good idea!

In theory, you could get behind someone else and use them as a human shield.

I don’t see it getting too expensive, until you have multiple explosions affecting a lot of rigid-body objects.

Applying impulse would also make things look more convincing (straight force looks a little ‘feathery’?)

It would be worth experimenting with (I know nothing of impulse force)

Boom,

Space = go boom

Attachments

BoomBoom.blend (898 KB)

this has up too 2 reflections


edit: this is useful in animation as well as game


)

Attachments

BoomBoomBoom.blend (1.03 MB)

Wow, guys, thanks so much!

Maintain a list of objects that can be hit by an explosion, then do:


explosion = cont.owner
for obj in damagable:
    dist = obj.getDistanceTo(explosion)
    amount = 1/dist
    dir = obj.getVectTo(explosion)
    obj['Health'] -= amount
    obj.applyForce(dir*amount)

It will apply damage and push them away. The closer they were to the center, the more the damage and the harder they are pushed.

Note: Untested code.

@BoomBoomBoom.blend: That is f’ing sweet! Makes me want to make a game around rocket launchers, LOL!