Search code examples
javaandroidlibgdxgame-engine

Libgdx ,can i attach animation in a Body?


im kinda new here, anywho... i am interested in the android gaming app development and i am learning on my own how to do so, using Libgdx as my game-engine and already made a small game not something exciting.

i have recently started to learn how to use the World variable and creating bodies in it, i want to create a class that extends Actor, give it animations (i know how to give animations) and to try and display it on the body, so where ever the body goes, the animation is played on it (basically below the animation is the body that is not displayed ofc)

so my question is : is there a way to give this costume actor into the body so it will play it over the body? (attaching them)

Will appreciate the help!

** NOTE : be aware that my knowledge is still limited, i am a college student for Program Engineering and i am not fully into much depth in java yet (my college not teaching game-engines) **

UPDATE

i have created a costume class, made animations to each action (like herowalk, herosit ... etc...)

i wrote a render method in it :

public void render(SpriteBatch batch) {
    float posX = bbody.getPosition().x * PPM;
    float posY = bbody.getPosition().y * PPM;

    float rotation = (float) Math.toDegrees(bbody.getAngle());

    sprite.setPosition(posX,posY);
    sprite.setRotation(rotation);

    sprite.draw(batch);
}

and created a body, placed it in the middle of my screen and attached the sprite to it using SetUserData() :

BodyDef bdef = new BodyDef();
    bdef.position.set(160 / PPM, 200 / PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(5 / PPM, 5 / PPM);
    bbody = world.createBody(bdef);
    Bodyarray.add(bbody);
    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;
    fdef.filter.categoryBits = BIT_BOX;
    fdef.filter.maskBits = BIT_PLATFORM | BIT_BALL;
    bbody.createFixture(fdef);
    bbody.setUserData(sprite);

and in my main class where i draw with batch i wrote :

player.sprite.setRegion(player.getAnimationup().getKeyFrame(dt, true));
        dt += elapsedTime;
        player.render(batch);

to constantly change the animations depends on if the player turns to the right, left, up , down.

the only problem i have is that the sprite it self (the animation works perfectly) is being drawn on the bottom left side of the screen (not on 0,0, it looks like it got the x,y of the body but still away from it) and can see that its attached to it when moving the body (i put controls to control the body movement) and i see the sprite moving with it.

for example im trying to check the coordination of X,Y for both the body and the sprite with Gdx.app.log as usual and both have the SAME exact X and Y. (ofc the sprite has his multiplied by PPM (placed it as 100) since the sprite is not a physical body)

what is causing the wrong location of the sprite?


Solution

  • Welcome to Stack Overflow!

    The answer to your question is "Not exactly." Box2D gives you a method called Body#setUserData() which lets you create a reference from the Body to any object (in this case an Animation object, but it is up to you to keep their positions in sync. See the pseudo-code below adapted from the libGDX wiki:

    // Create an array to be filled with the bodies
    // (better don't create a new one every time though)
    Array<Body> bodies = new Array<Body>();
    // Now fill the array with all bodies
    world.getBodies(bodies);
    
    for (Body b : bodies) {
        // Get the body's user data - in this example, our user 
        // data is an instance of the Entity class
        Animation anim = (Animation) b.getUserData();
    
        if (anim != null) {
            // Update the entities/sprites position and angle
            TextureRegion frame = anim.getKeyFrame( ... );
            // Now draw the frame using b.getPosition() and b.getAngle()
        }
    }
    

    By the way, the libGDX wiki is an excellent resource and reference as you start learning libGDX.