最后这一节记录一下cloudBomb中冲击力影响的实现和碰撞过滤。利用chipmunk中的冲击力函数可以在导弹爆炸时弹飞附近的刚体,达到最真实的爆炸所产生的物理影响,其实在游戏中也可以用来做释放技能之类的影响区域,下面是代码:
首先需要拿到导弹爆炸后,在影响范围内的刚体坐标:
cpSpaceNearestPointQuery(space,shape->body->p,100,12,CP_NO_GROUP,actionNearestPoint,NULL);
//获取爆炸点附近坐标后的回调函数 static void actionNearestPoint(cpShape *shape, cpFloat distance, cpVect point, void *data);
//获取爆炸点附近坐标后的回调函数 void HelloWorld::actionNearestPoint(cpShape *shape, cpFloat distance, cpVect point, void *data){ const cpFloat splash = 100.0; const cpFloat strength = 400.0; cpBody *dbody = shape->body; // Use the nearest point to calculate the impulse to apply. // This makes for a pretty good approximation. //强度设定 cpFloat intensity = 1.0 - cpvdist(dbody->p, point)/splash; //冲击力计算 cpVect impulse = cpvmult(cpvnormalize(cpvsub(dbody->p, point)), strength*intensity); //应用冲击到对应的刚体 cpBodyApplyImpulse(dbody,impulse,cpvsub(point, dbody->p)); // Play with the boxes' emotions. Box *dbox = (Box*)dbody->data; //这里只做了个表情的输出日志,有兴趣的可以自己做着玩 dbox->makeUpset(); }
最后看看导弹在碰撞墙体爆炸后箱子被弹飞的效果图:
源代码下载:http://download.csdn.net/detail/cyistudio/5558209