opengl - Stop particle effect libgdx -
i have game involves lots of collisions , explosions.
basically, enemy-projectiles launched bottom-right corner of screen towards left side. player can intercept them using weapon.
i want whenever "bullet" , projectile collide, code draw pre-made particles @ impact position. problem in real-time, in collision, particles drawn nicely never stop. particles go on drawn after collision.
now, utilize callback mechanism inform renderer collision took place somewhere, , pass him coordinates of collision(x,y)
here renderer code. cutting parts code, leaving relevant ones:
bundle com.david.gameworld; import com.badlogic.gdx.gdx; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.graphics.orthographiccamera; import com.badlogic.gdx.graphics.g2d.sprite; import com.badlogic.gdx.graphics.g2d.spritebatch; import com.badlogic.gdx.math.vector2; import com.badlogic.gdx.physics.box2d.body; import com.badlogic.gdx.physics.box2d.box2ddebugrenderer; import com.badlogic.gdx.utils.array; import com.david.helpers.assetsloader; import com.david.helpers.collisionandtouchcallbacks; public class gamerenderer implements collisionandtouchcallbacks.drawthings{ /* gamerenderer class. responsible rendering work , graphics */ public static final float floor_height = 100.0f; // public variable - floor height private static final float pixels_to_meters = 32.0f; // ptm ration public static float screenwidth; // screen width public static float screenheight; // screen height public orthographiccamera camera; // orthographic photographic camera private gameworld world; // game world object, rendering private boolean drawtrajectory; private boolean drawparticle; private boolean isdrawnparticle; private box2ddebugrenderer debugrenderer; // debug renderer, see positions , sizes of bodies/fixtures private spritebatch batch; // sprite batch private array<body> tempbodies; // array contains bodies of box2d private array<vector2> collisionpositions; private float delta; public gamerenderer(float screenwidth, float screenheight, gameworld world) { this.world = world; gamerenderer.screenheight = screenheight; gamerenderer.screenwidth = screenwidth; debugrenderer = new box2ddebugrenderer(); batch = new spritebatch(); photographic camera = new orthographiccamera(); tempbodies = new array<body>(); collisionpositions = new array<vector2>(); drawtrajectory = false; drawparticle = false; isdrawnparticle = false; } public void render(float delta) { gdx.gl.glclearcolor(1, 1, 1, 1); gdx.gl.glclear(gl20.gl_color_buffer_bit); this.delta = delta; // update world world.updateworld(); batch.setprojectionmatrix(camera.combined); // box2d world bodies world.getbox2dworld().getbodies(tempbodies); // draw entire scene batch.begin(); drawbodiesbox2d(); if(drawtrajectory) drawtrajectory(); if(drawparticle) { if(!isdrawnparticle) { drawparticle(); isdrawnparticle = true; } else { if(assetsloader.explosion.iscomplete()) { drawparticle = false; isdrawnparticle = false; } } } batch.end(); // debug renderer box2d world } // if user aiming, draw trajectory of acorn public void drawtrajectory() { gdx.app.log("system out println!!!!!!!!!", "draw projectile"); float t = 0.1f, x = world.getarrayacorns().peek().getx(), y = world .getarrayacorns().peek().gety(); float width = assetsloader.orbit.getwidth() / 8 / pixels_to_meters; float height = assetsloader.orbit.getheight() / 8 / pixels_to_meters; float timeseparation = 0.08f; (int = 0; < 40; i++) { x = world.getacornequation().getx(t); y = world.getacornequation().gety(t); batch.draw(assetsloader.orbit, x, y, width, height); t += timeseparation; } } private void drawbodiesbox2d() { (body body : tempbodies) { if (body.getuserdata() != null && body.getuserdata() instanceof sprite) { sprite sprite = (sprite) body.getuserdata(); if (!sprite.gettexture().equals(assetsloader.ground)) { sprite.setposition(body.getposition().x - sprite.getwidth() / 2, body.getposition().y - sprite.getheight() / 2); } else { sprite.setsize(screenwidth, floor_height / pixels_to_meters); } sprite.setrotation((float) math.todegrees(body.getangle())); batch.draw(assetsloader.catapult_base1, 450 / pixels_to_meters, (floor_height - 10) / pixels_to_meters, assetsloader.catapult_base1.getwidth() / pixels_to_meters, assetsloader.catapult_base1.getheight() / pixels_to_meters); sprite.draw(batch); } } } public void dispose() { assetsloader.dispose(); batch.dispose(); world.dispose(); debugrenderer.dispose(); } // update method orthographic photographic camera public void updatecamera(int width, int height) { camera.settoortho(false, width, height); screenwidth = (float) width; screenheight = (float) height; camera.update(); // meters } public orthographiccamera getcamera() { homecoming this.camera; } private void drawparticle() { // todo auto-generated method stub assetsloader.explosion.setposition(collisionpositions.first().x,collisionpositions.first().y); assetsloader.explosion.start(); assetsloader.explosion.draw(batch,delta); assetsloader.explosion.allowcompletion(); } @override public void signaltotrajectory(boolean flag) { // todo auto-generated method stub drawtrajectory = flag; } @override public void signaltoparticle(boolean flag, array<vector2> positions) { // todo auto-generated method stub if(flag) collisionpositions = positions; drawparticle = flag; } }
how can draw particle effect 1 time , that's it?
you can use/optimise particle class, my post here
to draw particle effect 1 time , that's : ( it's in myparticle free method ) :
particleeffect effect = new particleeffect(); emitter emitter = effect.getcontrollers().first().emitter; if (emitter instanceof regularemitter){ regularemitter reg = (regularemitter) emitter; reg.setemissionmode(regularemitter.emissionmode.enableduntilcycleend); //reg.durationvalue.setlow(10f); reg.dispose(); } emitter.dispose();
opengl libgdx particles particle-system
No comments:
Post a Comment