围绕另一个 Sprite 旋转一个 Sprite -libGDX-
Rotate a Sprite around another Sprite -libGDX-
我正在尝试制作一个游戏(参见上文 link),我需要让操纵杆绕着自己旋转以保持面向圆心的方向。
这是我声明 Sprite 的方式,以及我如何绕着圆移动它:
声明:
line = new Sprite(new Texture(Gdx.files.internal("drawable/blockLine.png")));
line.setSize(140, 20);
lineX = Gdx.graphics.getWidth()/2 - line.getWidth()/2;
lineY = (Gdx.graphics.getHeight()/2 - line.getHeight()/2) + circle.getHeight()/2;
运动:
Point point = rotatePoint(new Point(lineX, lineY), new Point(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2), angle+= Gdx.graphics.getDeltaTime() * lineSpeed);
line.setPosition(point.x, point.y);
旋转点函数:
Point rotatePoint(Point point, Point center, double angle){
angle = (angle ) * (Math.PI/180); // Convert to radians
float rotatedX = (int) (Math.cos(angle) * (point.x - center.x) - Math.sin(angle) * (point.y-center.y) + center.x);
float rotatedY = (int) (Math.sin(angle) * (point.x - center.x) + Math.cos(angle) * (point.y - center.y) + center.y);
return new Point(rotatedX,rotatedY);
}
有什么建议吗?
我现在无法测试,但我认为线的旋转应该只是:
Math.atan2(rotatedPoint.getOriginX() - middlePoint.getOriginX(), rotatedPoint.getOriginY() - middlePoint.getOriginY()));
然后您必须将 rad 调整为度数或您将使用的任何值。如果它不起作用,请告诉我!
我会采取不同的方法,我只是创建了一个方法,将 n 个按钮放在屏幕上的单击周围。我正在使用看起来像这样的东西:
float rotation; // in degree's
float distance; //Distance from origin (radius of circle).
vector2 originOfRotation; //Center of circle
vector2 originOfSprite; //Origin of rotation sprite we are calculating
Vector2 direction = new vector2(0, 1); //pointing up
//rotate the direction
direction.rotate(rotation);
// add distance based of the direction. Warning: originOfRotation will change because of chaining method.
// use originOfRotation.cpy() if you do not want to init each frame
originOfSprite = originOfRotation.add(direction.scl(distance));
现在你有了精灵的位置。您需要将 rotation
每帧增加 x 以使其旋转。如果你想改变精灵的方向,你可以使用方向向量,可能再次旋转 180 度。效率方面我不确定会有什么不同。
我正在尝试制作一个游戏(参见上文 link),我需要让操纵杆绕着自己旋转以保持面向圆心的方向。
这是我声明 Sprite 的方式,以及我如何绕着圆移动它:
声明:
line = new Sprite(new Texture(Gdx.files.internal("drawable/blockLine.png")));
line.setSize(140, 20);
lineX = Gdx.graphics.getWidth()/2 - line.getWidth()/2;
lineY = (Gdx.graphics.getHeight()/2 - line.getHeight()/2) + circle.getHeight()/2;
运动:
Point point = rotatePoint(new Point(lineX, lineY), new Point(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2), angle+= Gdx.graphics.getDeltaTime() * lineSpeed);
line.setPosition(point.x, point.y);
旋转点函数:
Point rotatePoint(Point point, Point center, double angle){
angle = (angle ) * (Math.PI/180); // Convert to radians
float rotatedX = (int) (Math.cos(angle) * (point.x - center.x) - Math.sin(angle) * (point.y-center.y) + center.x);
float rotatedY = (int) (Math.sin(angle) * (point.x - center.x) + Math.cos(angle) * (point.y - center.y) + center.y);
return new Point(rotatedX,rotatedY);
}
有什么建议吗?
我现在无法测试,但我认为线的旋转应该只是:
Math.atan2(rotatedPoint.getOriginX() - middlePoint.getOriginX(), rotatedPoint.getOriginY() - middlePoint.getOriginY()));
然后您必须将 rad 调整为度数或您将使用的任何值。如果它不起作用,请告诉我!
我会采取不同的方法,我只是创建了一个方法,将 n 个按钮放在屏幕上的单击周围。我正在使用看起来像这样的东西:
float rotation; // in degree's
float distance; //Distance from origin (radius of circle).
vector2 originOfRotation; //Center of circle
vector2 originOfSprite; //Origin of rotation sprite we are calculating
Vector2 direction = new vector2(0, 1); //pointing up
//rotate the direction
direction.rotate(rotation);
// add distance based of the direction. Warning: originOfRotation will change because of chaining method.
// use originOfRotation.cpy() if you do not want to init each frame
originOfSprite = originOfRotation.add(direction.scl(distance));
现在你有了精灵的位置。您需要将 rotation
每帧增加 x 以使其旋转。如果你想改变精灵的方向,你可以使用方向向量,可能再次旋转 180 度。效率方面我不确定会有什么不同。