java 如何通过中间点旋转libgdx中的图像actor?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13920850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rotate an image actor in libgdx by the middle point?
提问by Peter Poliwoda
If I understand correctly, LibGDX is rotating an image with the use of the addActions method:
如果我理解正确,LibGDX 正在使用 addActions 方法旋转图像:
this.addAction( parallel(rotateBy(360, 0.5f), moveTo(320, 100, 0.5f)));
The thing is, it is being rotated by the point=(0,0)of the image.
问题是,它正在被 图像的point=(0,0)旋转。
Here's my question:
这是我的问题:
Is there a way to rotate an imageby the center pointof the object? Something like pinning it in the middle and then turning it like a wheel in a car? Both rotateByand rotateTomethods turn it by the (0,0) point of the image itself.
有没有办法通过对象的中心点旋转图像?像把它钉在中间然后像汽车的轮子一样转动它?既rotateBy和rotateTo方法由图像本身的(0,0)点转动。
回答by aacotroneo
You have to properly set the "origin" of you actor. You can read from the Actor API that the origin is relative to the position and is used for scale and rotation.
您必须正确设置演员的“原点”。您可以从 Actor API 中了解到原点是相对于位置的,用于缩放和旋转。
So, calculate your middle point, and set origin to middle point.
因此,计算您的中点,并将原点设置为中点。
Actor a;
....
a.setOrigin(a.getWidth()/2, a.getHeight()/2);
回答by Matt Stevens
The easiest way is to translate the object so the 0,0 point is the centre, rotate it, then translate it back into position. I'm not familiar with libGDX, but there should be a method for aggregating the three transforms, meaning you apply only the one aggregated transform to your object. Keep in mind the order you aggregate the transforms, makes a difference.
最简单的方法是平移对象,使 0,0 点成为中心,旋转它,然后将其平移回原位。我对 libGDX 不熟悉,但是应该有一种聚合三个转换的方法,这意味着您只将一个聚合的转换应用于您的对象。请记住,您聚合转换的顺序会有所不同。
A much more complicated way that gives exactly the same result involves calculating the amount of offset created by the rotate and translating the image back to its origin. Don't do it this way. Combining three simple transforms is the way to go and one of the best reasons for using transforms.
给出完全相同结果的一种更复杂的方法涉及计算由旋转产生的偏移量并将图像平移回其原点。不要这样做。结合三个简单的转换是可行的方法,也是使用转换的最佳理由之一。
** Edit Looking at the APIthe Actor object has a translate() method. Use this to shift your image so the 0,0 point is in the centre. In psuedocode;
** 编辑查看API,Actor 对象有一个 translate() 方法。使用它来移动您的图像,使 0,0 点位于中心。在伪代码中;
x = this.getX();
y = this.getY();
cx = this.getWidth() * .5;
cy = this.getHeight() * .5;
this.translate(-(x+cx), -(y+cy)); // move so 0,0 is the centre of the Actor object
this.rotate(thisMuch); // rotate the Actor
this.translate((x+cx), (y+cy)); // move it back to its original location
This will result in your object rotating on its centre point, on the spot. It looks convoluted, but its pretty efficient in practise, more so if you are able to work direct with the matrices (transforms) that will be driving these methods.
这将导致您的对象在其中心点原地旋转。它看起来令人费解,但在实践中非常有效,如果您能够直接使用将驱动这些方法的矩阵(变换),则效率更高。
回答by Sudhir singh
Here is the simple way how can you rotate an actor in libgdx.
这是如何在 libgdx 中旋转角色的简单方法。
First, you need to set the origin:
首先,您需要设置原点:
img.setorigin(width/2,hieght/2);
Now you can rotate clockwise and anticlockwise, according to your need:
现在您可以根据需要顺时针和逆时针旋转:
img.rotate(2f);
or
或者
img.rotate(-2f);
回答by Amit Kotlovski
The easiest and shortest way I found for setting the origin is to call:
我发现设置原点的最简单和最短的方法是调用:
Actor a;
...
a.setOrigin(Align.center);