Showing posts with label Cocos3D. Show all posts
Showing posts with label Cocos3D. Show all posts

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.




Tuesday, 24 July 2012

Bullet Physics - Cocos3d - Collisions using contact-added callback and masks

Within my current game I want my truck to be able to come into contact with a power up.
When this happens I don't want it to influence the truck, just register the impact.

There is quite a straight forward way of dealing with collisions using bullet and this is using the contact added callback.

You will need to create one method that handles what happens when a collision occurs and then set a bullet variable to be equal to this method.

Then all you will have to do is register each object that uses the callback for collisions.

Create a function with the following signature


bool contact_callback( btManifoldPoint &btmanifoldpoint,
                            const btCollisionObject *btcollisionobject0,
                            int part_0, int index_0,
                            const btCollisionObject *btcollisionobject1,
                            int part_1, int index_1 ) 


Within this method you will be returned a pair of objects that have collided, it is up to you what you do with them.

I use the

rigidBody->setUserPointer(physicsObject);

method of the rigidBody to set a pointer to my own object.

This allows me to cast the pointer from the collisions to my own objects.

CC3PhysicsObject3D *phyObject0 = ( CC3PhysicsObject3D * )( ( btRigidBody * )btcollisionobject0 )->getUserPointer();
    

Note even if you remove an object that comes into contact immediatly from your physics world it will still call this call back a few times, I have an isBeingRemoved property on my own object which I set as a further check.

Then all you have to do is register your callback with the world

 gContactAddedCallback = contact_callback;

I do this at the bottom of my world initialisation code.

Finally I register any rigid bodies that you want to call this callback method when a collision has occurred.


pObject.rigidBody->setCollisionFlags(pObject.rigidBody->getCollisionFlags() |  btRigidBody::CF_NO_CONTACT_RESPONSE |btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK );


Here pObject is my physics. I use two flags here

btRigidBody::CF_NO_CONTACT_RESPONSE 

Which register the rigid body as not having an affect on the physics world

and

btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK 

Which registers the object to call the gContactAddedCallback related call back function.

I hope the above is of some help!

Duncan.