We are working on a 3D game engine with bullet physics. After trying out some tricks we finally ended up making explosion that affects all the nearby objects. The below function is called with the explosion location, power and radius. Remember to call this function from the tick method. Calling it from anywhere else might bring up crashes.
- Code: Select all
- (void) Explode:(btVector3)location Power:(float)power Radius:(float)radius {
for(int i = 0; i < _totalShapes; i++) {
btVector3 shapeLocation = [_shapes[i] getPosition];
float distance = btDistance(location, shapeLocation);
if (distance < radius) {
btVector3 dir = [_shapes[i] getDirectionFrom:location];
[_shapes[i] ApplyForce:(dir*(-power/(distance*distance)))];
}
}
}