1
\$\begingroup\$

Hello I am a newbie with libgdx, my question is: how could I add a Particleeffect to a Body that is moving? I searched about the matter and I didnt find anything related to it.

I have the following code to add my ball:

public Body createThrowedBall(float x, float y) {

    Gdx.app.log("createThrowedBall", "("+x+","+y+")");

    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DynamicBody;
    ballDef.position.set(x,y);

    CircleShape shape = new CircleShape();
    shape.setRadius(5.5f);

    FixtureDef fixtureDef= new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    fixtureDef.friction = 0f;
    fixtureDef.restitution = 0f;
    fixtureDef.filter.groupIndex = -1;

    Body body = world.createBody(ballDef);
    body.createFixture(fixtureDef);
    shape.dispose();


    // note the massive impulse vector

    body.applyLinearImpulse(new Vector2(0, 5500), new Vector2(x,camera.viewportHeight), false);

    return body;
}

That draws a simple circle shape that is throwed up. I want to add like a fire effect burning during the throw ( I did it with the Particle Editor) but I dont know how to attach it.

\$\endgroup\$
1
\$\begingroup\$

I solved it adding the ParticleEffect to he body and before rendering the effect in render function:

    public Body createThrowedBall(float x, float y) {

    Gdx.app.log("createThrowedBall", "("+x+","+y+")");

    BodyDef ballDef = new BodyDef();
    ballDef.type = BodyType.DynamicBody;
    ballDef.position.set(x,y);

    CircleShape shape = new CircleShape();
    shape.setRadius(5.5f);

    FixtureDef fixtureDef= new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1f;
    fixtureDef.friction = 0f;
    fixtureDef.restitution = 0f;
    fixtureDef.filter.groupIndex = -1;

    Body body = world.createBody(ballDef);
    body.createFixture(fixtureDef);

    ParticleEffect effect = new ParticleEffect();
    effect.load(Gdx.files.internal("effects/ball_in_fire.p"), Gdx.files.internal("img"));
    effect.setPosition((x+camera.viewportWidth/2)*10, (y+camera.viewportHeight/2)*10);
    effect.start();     
    body.setUserData(effect);

    shape.dispose();


    // note the massive impulse vector

    body.applyLinearImpulse(new Vector2(0, 5500), new Vector2(x,camera.viewportHeight), false);

    return body;
}

in render:

    public void render(float delta)
{
...
        game.batch.begin();
        //imageBackground.draw(game.batch, 1.0f);
        Array<Body> bodies = new Array<Body>();
        world.getBodies(bodies);
        for(int i=0;i<bodies.size;i++)
        {
            ParticleEffect effect = (ParticleEffect)bodies.get(i).getUserData();
            if(effect != null)
            {
                effect.setPosition((bodies.get(i).getPosition().x+camera.viewportWidth/2)*10, (bodies.get(i).getPosition().y+camera.viewportHeight/2)*10);
                effect.draw(game.batch,delta);
            }

        }

        game.batch.end();
}

NOTE: I calculated the position of the effect because the ParticleEffect need the x,y of the screen and the ball needs the x,y of the camera.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.