chipmunk collision too soft

2019-08-05 13:40发布

问题:

I'm new to physics in cocos2d. I'm using chipmunk, and when two object collide, its just to "soft", like they where made of sponge or rubber.

My code:

cpInitChipmunk();  
space = cpSpaceNew();  
space->gravity = cpv(0, 0);  
schedule(schedule_selector(HelloWorld::step), 1.0f/60.f);
astroBody = cpBodyNew(100, INFINITY);  
astroBody->p = cpv(512,384);
cpSpaceAddBody(space, astroBody);
int num2 = 8;
cpVect vertsAstro[] =  {
    cpv(-17.0f, -44.9f),
    cpv(-29.5f, -33.2f),
    cpv(-32.9f, -13.1f),
    cpv(-24.0f, 11.7f),
    cpv(24.6f, 11.5f),
    cpv(32.9f, -12.9f),
    cpv(29.3f, -33.1f),
    cpv(17.0f, -44.7f)
};
astroShape = cpPolyShapeNew(astroBody, num2, vertsAstro, cpv(0,0));
astroShape->e = 0.0f;
astroShape->u = 0.0f;
astroShape->collision_type = 0; 
astroShape->data = player;
cpSpaceAddShape(space, astroShape);

cpBody *box = cpBodyNew(INFINITY, INFINITY);  
box->p = cpv(z->getPosition().x+32, z->getPosition().y+32); 
int num = 5;
cpVect verts[] = {
    cpv(-32, -32),
    cpv(-32, 32),
    cpv(32, 32),
    cpv(32, -32),
    cpv(-32, -32)
};
cpShape *boxShape = cpPolyShapeNew(box, num, verts, cpv(0,0));
boxShape->e = 0.0f;
boxShape->u = 0.0f;
boxShape->collision_type = 1;  
boxShape->data = z;
cpSpaceAddStaticShape(space, boxShape);

So these objects are colliding, and it is too soft. Can I make it somehow to look like two stones hit each other?

回答1:

You must be using Chipmunk 5. You need to set the fields directly such as shape->e = 1.0.

The getter/setter functions are part of Chipmunk 6. You can still set the fields directly, but it's recommended not to as the setter functions will automatically wake up objects when changing values.



回答2:

From the docs:

void cpShapeSetElasticity(cpShape *shape, cpFloat value)

Elasticity of the shape. A value of 0.0 gives no bounce, while a value of 1.0 will give a “perfect” bounce. However due to inaccuracies in the simulation using 1.0 or greater is not recommended however. The elasticity for a collision is found by multiplying the elasticity of the individual shapes together.

Does this help?