0

I want to set my arm of my player to point at my mouse in the window, this is my arm code for a fixture I couldn't find how to set it to rotate and point at the mouse

package com.mygdx.game.Sprites.player;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.mygdx.game.PlayScreen;
import com.mygdx.game.main;



@SuppressWarnings("unused")
public class LArm extends Sprite {
    public LArm(PlayScreen screen){
        setBounds(getX(), getY(), 3 / main.PPM, 8 / main.PPM);
        setPosition(Player.b2body.getPosition().x, (float) Player.b2body.getPosition().y );
        setRegion(new Texture(Gdx.files.internal("LArm.png")));

    }
    public void update(float dt) {
        //setRotation(to the mouse);
        if(Player.runningRight)
            setPosition(Player.b2body.getPosition().x-1/main.PPM,Player.b2body.getPosition().y-8/main.PPM);
        if(!Player.runningRight)
            setPosition(Player.b2body.getPosition().x-2/main.PPM,Player.b2body.getPosition().y-8/main.PPM);
        setRegion(new Texture(Gdx.files.internal("LArm.png")));
        setOrigin((float)1.5 / main.PPM, 8 / main.PPM);

    }
}

1 Answers1

0

Sprite has a method #setRotation(float degrees) to rotate the image with an amount of degrees.

All you need is to calculate the degrees between the object and mouse, now this has been answered numerous times, this is one of many posts, it calculates the radians between two coordinates, so I'm changing it to degrees:

double angle = Math.toDegrees(Math.atan2(mouse.x - arm.x, mouse.y - arm.y));

I assume you have the mouse coordinates, otherwise it would be a good idea to setup an InputProcessor. Now the specific coordinates in your case may be different, you may want to calculate the angle between mouse and your player instead of the arm to keep it consistent, in such case just change the coordinates to your liking. Hope this helped you