简体   繁体   English

如何在鼠标上旋转Box2D世界固定装置

[英]How to rotate a box2D world fixture at the mouse

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);

    }
}

Sprite has a method #setRotation(float degrees) to rotate the image with an amount of degrees. Sprite具有#setRotation(浮动度)方法,以一定程度旋转图像。

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 . 我假设您具有鼠标坐标,否则设置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 希望这对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM