Wednesday 1 August 2012

Cocos3d, Explosions in Monster Truck Trials

When a coin is collected initially we initially had a simple fade out.  This was to boring.



How would we create an explosion in cocos3d?  One concept would be to create clones of the original object and fire them off in various directions.

As we have the physics engine available, the following process was chosen.

  • Spawn 10 new copies of the current one.
  • Create a collision mask so that only explosion object can influence each other.
  • Randomly change thier direction.
  • Apply a little impulse.
  • Create a fade out event that on completion removes the objects from the 3d and physics worlds.
The impulse alone would not be enough to make the objects go all over the place but as they are being spawned in the same locations the physics engine will make them react with each other as long as they have the same collision mask.

And here is the code.

float previous = node.rotation.y;
    for (int x = 0; x < 10; x++) {
    
        int random = [Helper getRandomNumberBetweenMin:1 andMax:10];
        CC3MeshNode* newCoin = [node copyAutoreleased];
        newCoin.uniformScale = 0.05 * random;
        newCoin.location = node.location;
        newCoin.quaternion = node.quaternion;
   
        [_activeWorld addChild:newCoin];
        [newCoin rotateBy:cc3v( previous + (80 * random), previous + (80 *  random), previous + (20 * random))];
        previous = newCoin.rotation.y;
        CC3PhysicsObject3D* explosionObject = [_physicsWorld createMyPhysicsObject:newCoin shape:boxShape2 mass:100 restitution:0.1 position:newCoin.location isStatic: NO : COL_PARTICLE : COL_PARTICLE];
        explosionObject.rigidBody->applyCentralImpulse(btVector3(5,5,0));
        CCFadeOut* fadeOut2 = [CCFadeOut actionWithDuration: random * 0.2];
        CCSequence *seq = [CCSequence actions:fadeOut2,   [CCCallFuncN actionWithTarget:explosionObject selector:@selector(remove)], nil];
        [explosionObject.node runAction:seq];    
       
    }    


I hope its of some use.


Duncan.




No comments:

Post a Comment