如何在某个地方创建精灵点? [英] How to make a sprite point somewhere?

查看:97
本文介绍了如何在某个地方创建精灵点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wan't my sprite to point at the cursor, how do I compute on how much do I need to rotate it?

//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(??);

更新-> 我试过了,但我不知道在已经指向正确方向的情况下使其停止旋转的条件是什么

update--> I tried this but I don't know what should be the condition to make it stop rotating when is already pointed at the right direction

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

Sprite.rotate(fl)

更新2-> 当我这样做时,它几乎不动:

update 2--> It barely moves when I do this:

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

update 3->因此,现在我认为它位于光标之后,但是它使用了精灵的相反部分,想象一个箭头而不是使用箭头的尖点来指向它使用了箭头的另一侧,希望您明白我的意思了,顺便说一句,这是我所做的:

update 3 --> So now I think it's following the cursor but it uses the opposite part of the sprite, Imagine an arrow instead of pointing using the pointy part of the arrow it uses the opposite side of the arrow, hope you understand what I mean, btw this is what I did instead:

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

推荐答案

如果您想让Sprite适应特定的旋转角度,请尝试使用sprite.setRotation(float angle);.

Try with sprite.setRotation(float angle); if you want to fit your sprite to a certain rotation.

编辑

我不知道rotate()是否在实际值上添加了旋转,因此,在这种情况下,每次调用此方法时,对象都会旋转.尝试使用setRotation(),但是请记住,它可以与度一起使用,并且某些方法(例如MathUtils.atan2()返回弧度值),因此必须将该结果转换为度.

I don't know if rotate() adds a rotation to the actual so, if this the case, your object will rotate everytime you call this method. Try use setRotation(), but remember that works with degrees, and some methods, like MathUtils.atan2() returns a radian value, so you have to convert that result to degree.

我猜想有像MathUtils.toDegrees()MathUtils.radiansToDegrees()这样的功能可以帮助您达到此目的.

I guess there's a function like MathUtils.toDegrees() or MathUtils.radiansToDegrees() that can help you for that purpose.

编辑2

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

Another problem that could be happening is that input.getY() is inverted so (0,0) is at the top left.

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

The correct way should be something like this:

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

这篇关于如何在某个地方创建精灵点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆