libGDX中的三角学——速度和角度

libGDX中的三角学——速度和角度,libgdx,Libgdx,在我的游戏中,我想使用三角学。正如我最初搜索的一样,所有内容都与Actionscript3中的内容相同。但我错了。一切都没有达到应有的效果。因此,在搜索了libGDX论坛和stachoverflow帖子之后,我“玩”了数字游戏,并找到了解决方案。希望它能帮助一些人如何在libgdx中计算角度,以及如何使用三角法设置x和y速度(为了清晰起见,代码省略): //variables private Object object; private float touchX, touchY, angle,

在我的游戏中,我想使用三角学。正如我最初搜索的一样,所有内容都与Actionscript3中的内容相同。但我错了。一切都没有达到应有的效果。因此,在搜索了libGDX论坛和stachoverflow帖子之后,我“玩”了数字游戏,并找到了解决方案。希望它能帮助一些人

如何在libgdx中计算角度,以及如何使用三角法设置x和y速度(为了清晰起见,代码省略):

//variables
private Object object;
private float touchX, touchY, angle, xSpeed, ySpeed;
private final int power = 5;

//touch up event
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    touchX = screenX;
    touchY = h - screenY; // screenX gives an inverted y position (y == 0 is at the top)
    setPosition(touchX, touchY);        
}

//set angle and velocity method
public void setPosition(int x, int y){
    //example 1 (velocity is calculated without angle):
    angle = (float) (Math.atan2(y - object.y, x - object.x) * tdg) - 90.0f;
    //at the end there is not always -90.0f. It depends of how the texture (or sth else we 
    //want to rotate) is rotated in .png file. Play with nuber if -90.0f is not good. Use 
    //the scale from -360 to 360
    xSpeed = (x - bx) / 30;
    ySpeed = (y - by) / 30;

    //example 2 (velocity with angle):
    angle = (float) Math.atan2(y - object.y, x - object.x);
    xSpeed = (float) (Math.cos(angle)*power);
    ySpeed = (float) (Math.sin(angle)*power);
    angle = (angle*180/Math.PI) - 90.0f;
}