我正在尝试制作游戏(请参阅上面的链接),我需要让游戏杆绕自己旋转,以保持方向面到圆圈的中心。
这是我如何声明Sprite,以及我如何围绕圆圈移动它:
声明:
rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
@Override
public void onComplete(RippleView rippleView) {
Log.d("Sample", "Ripple completed");
}
});
运动:
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;
rotatePoint函数:
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);
任何sugestions?
答案 0 :(得分:0)
我现在无法测试,但我认为该线的旋转应该只是:
Math.atan2(rotatedPoint.getOriginX() - middlePoint.getOriginX(), rotatedPoint.getOriginY() - middlePoint.getOriginY()));
然后你必须将rad调整到度数或你将要使用的任何东西。告诉我它是否无效!
答案 1 :(得分:0)
我会采用不同的方法,我只是创建了一个方法,在屏幕上点击一下就可以放置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。效率明智我不确定会有什么区别。