In a sport I’m growing I needed to have some life like explosion results. At first, I believed I’d simply apply an impulse to all objects inside vary of the explosion. Nevertheless, this created unrealistic motion within the affected objects and the objects didn’t rotate. I attempted utilizing ray casts to set factors to use the forces to create rotation. This made the explosion look extra life like and labored nicely sufficient till the explosions exploded close to a static physique through which case objects would generally be missed as they had been shielded by the static physique. I made a decision to dive in absolutely and use particles to symbolize the pressure of the blast.
The very first thing I made was an array to retailer the place the explosions would explode and an array to retailer every particle from the explosion.
public Array public Array |
Subsequent, I created the tactic that might be run for every explosion.
non-public void explodeABomb(Vector2 vector){ int numRays = ExplosionParticle.NUMRAYS; for (int i = 0; i numRays; i++) { float angle = (i / (float)numRays) * 360 * MathUtils.degreesToRadians; Vector2 rayDir = new Vector2( (float) Math.sin(angle),(float) Math.cos(angle) ); partys. add(new ExplosionParticle(world, vector, rayDir)); // create the particle } this.explosions.add(vector); // add explosion particle impact at vector } |
I wanted to create a category for the Explosion particles which might comprise a reference to the physique. On this class, I create a physique definition and use it to create a Physique. The physique place is then set to the place the bomb exploded. The linear velocity is then set to offer the particle a route and pressure. A Fixture is then created to offer the physique a form, measurement, weight and so forth, and added to the physique.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
public class ExplosionParticle { public int blastPower = 100; public static last int NUMRAYS = 60; public Physique physique; public ExplosionParticle(World world, Vector2 vector, Vector2 rayDir){ BodyDef bd = new BodyDef(); bd.sort = BodyType.DynamicBody; bd.fixedRotation = true; // rotation not obligatory bd.bullet = true; // stop tunneling at excessive velocity bd.linearDamping = 10; // drag as a consequence of shifting by means of air bd.gravityScale = 0; // ignore gravity bd.place.x = vector.getPosition().x; bd.place.y = vector.getPosition().y;// begin at blast heart rayDir.scl(blastPower); bd.linearVelocity.x = rayDir.x; bd.linearVelocity.y = rayDir.y; physique = world.createBody( bd ); //create a reference to this class within the physique(this permits us to loop by means of the world our bodies and examine if the physique is an Explosion particle) physique.setUserData(this);
CircleShape circleShape = new CircleShape(); circleShape.setRadius(0.05f); // very small
FixtureDef fd = new FixtureDef(); fd.form = circleShape; fd.density = 120 / (float)NUMRAYS; // very excessive – shared throughout all particles fd.friction = 0; // friction not obligatory fd.restitution = 0.99f; // excessive restitution to replicate off obstacles fd.filter.groupIndex = –1; // particles mustn’t collide with one another physique.createFixture( fd ); }
} |
The Box2D world would quickly refill with particles until they’re eliminated as soon as they’ve completed expelling all their power.
for(ExplosionParticle social gathering: partys){ if(Math.abs(social gathering.physique.getLinearVelocity().x) 5f && Math.abs(social gathering.physique.getLinearVelocity().y) 5f){ world.destroyBody(social gathering.physique); partys.removeValue(social gathering, true); } } |
In my contact listener I’ve this code to inform the sport mannequin to make an explosion
guardian.createBlast(bomb); |
which runs this methodology in my Recreation Mannequin
public void createBlast(Bomb bomb) { bombsToExplode.add(bomb.physique.getPosition()); bomb.isDead = true; // units the bomb to useless so it may be eliminated exterior of the box2d physics calculations } |
That is how the explosions look when utilizing the box2D debug renderer.
[embedyt] http://www.youtube.com/watch?v=iN6Faut9g0A[/embedyt]
Additional Studying:
Views: 7,036