Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在某处制造精灵点?_Java_Rotation_Libgdx_Sprite - Fatal编程技术网

Java 如何在某处制造精灵点?

Java 如何在某处制造精灵点?,java,rotation,libgdx,sprite,Java,Rotation,Libgdx,Sprite,我不想让我的精灵指向光标,我如何计算需要旋转多少 //First is get the cursor position x=Gdx.input.getX(); y=Gdx.input.getY(); //then rotate the sprite and make it point where the cursor is Sprite.rotate(??); 更新--> 我试过这个,但我不知道什么条件应该使它停止旋转,当它已经指向正确的方向 double radians = Math.at

我不想让我的精灵指向光标,我如何计算需要旋转多少

//First is get the cursor position
x=Gdx.input.getX();
y=Gdx.input.getY();

//then rotate the sprite and make it point where the cursor is
Sprite.rotate(??);
更新--> 我试过这个,但我不知道什么条件应该使它停止旋转,当它已经指向正确的方向

double radians = Math.atan2(mouseY - spriteY, mouseX - spriteX)
float fl=(float)radians;

Sprite.rotate(fl)
更新2--> 当我这样做时,它几乎不动:

public void update(){
        int x=Gdx.input.getX();
        int y=Gdx.input.getY();

        double radians=Math.atan2(y-Spr.getY(),x-Spr.getX());
        float angle=(float)radians;
        Spr.setRotation(angle);
    }
更新3-->所以现在我认为它在跟随光标,但它使用精灵的相反部分,想象一个箭头,而不是使用箭头的尖头部分指向它,它使用箭头的另一侧,希望您理解我的意思,顺便说一句,这是我所做的:

 public void update(){
        int x=Gdx.input.getX();
        int y=Gdx.graphics.getHeight()-Gdx.input.getY();

        double radians= Math.atan2(y - launcherSpr.getY(), x - launcherSpr.getX());
        float angle=(float)radians*MathUtils.radiansToDegrees; //here
        launcherSpr.setRotation(angle);
    }

您可以使用
Math.atan2(双y,双x)
获得所需的效果

此方法在极坐标中计算角度,假设您有一个点位于
(0,0)
,另一个点位于给定的
(xo,yo)
。因此,要使其正常工作,您必须规范化这些值,以便原点是精灵的位置,
(xo,yo)
是鼠标的相对位置

这基本上归结为:

double radians = Math.atan2(mouseY - y, mouseX - x)

尝试使用
sprite.setRotation(浮动角度)如果要使精灵适合特定旋转

编辑

我不知道
rotate()
是否会将旋转添加到实际值,因此,如果是这种情况,每次调用此方法时,对象都会旋转。尝试使用
setRotation()
,但请记住,这适用于度,有些方法,如
MathUtils.atan2()
返回弧度值,因此必须将结果转换为度

我想有一个函数像
MathUtils.toDegrees()
MathUtils.radiansToDegrees()
可以帮助您实现这一目的

编辑2

另一个可能发生的问题是
input.getY()
被倒置,因此(0,0)位于左上角

正确的方法应该是这样的:

public void update(){
    int x = Gdx.input.getX();
    int y = HEIGHT - Gdx.input.getY(); 
    // HEIGHT is an static user variable with the game height

    float angle = MathUtils.radiansToDegrees * MathUtils.atan2(y - Spr.getY(), x - Spr.getX());
    Spr.setRotation(angle);
}

我试过了,但旋转方向不对,有时也不会停止旋转。@Jack answer根据您的需要进行响应,但请记住,每次指向某个位置时,您都会将精灵x旋转几度……旋转精灵x是什么意思?我的精灵不会改变位置,它只是旋转。检查我的更新2 pls这不会解决您的问题,但请尝试使用
MathUtils.atan2()
它使用float,因此不需要进行铸造…因此在更新3中,您必须手动旋转纹理,或在
setRotation()之前添加纹理
类似于
浮动角度+=certainAmountOfDegrees
的东西,尝试规范化精灵的位置