[英]Rotate Image Clockwise using LibGDX
How can we rotate a Image Clockwise using LibGDX? 如何使用LibGDX顺时针旋转图像? what i am looking is when a image is loaded,suppose a star, i need to rotate it from beginning of screen to end of the screen horizontally, with star rotating,how can i do that in libgdx?
我正在查看的是加载图像时(假设是星星),我需要将其从屏幕的开头水平旋转到屏幕的末端,同时星星旋转,我该如何在libgdx中做到这一点?
When you draw the Texture with your SpriteBatch, you can use one of the draw functions that includes rotation. 使用SpriteBatch绘制纹理时,可以使用包含旋转功能的绘制功能之一。 This javadoc has all the draw functions: SpriteBatch
此javadoc具有所有绘制函数: SpriteBatch
You can keep a variable for position and rotation, and increase the rotation and x component of the position each time you render to make it rotate while moving horizontally. 您可以保留位置和旋转的变量,并在每次渲染时增加位置的旋转和x分量以使其在水平移动时旋转。
Libgdx gives you more then one way to do that: You can use Scene2D
and add an Image
to your Stage
. Libgdx为您提供了更多的一种实现方法:您可以使用
Scene2D
并将Image
添加到Stage
。 Image
is a subclass of Actor
, so you can add Action
s to it: Image
是Actor
的子类,因此可以向其添加Action
:
Image myImage = new Image(myTexture);
myImage.addAction(Actions.parallel(Actions.moveTo(endX, endY, duration), Actions.rotateBy(degrees, duration)));
myImage.setPosition(startX, startY);
myImage.setOrigin(sizeX/2, sizeY/2);
stage.add(myImage);
In render
you can then call stage.act()
, which updates the position, rotation, scale... of all your Actor
s and then call stage.draw()
which will call draw()
for all your Actor
s. 然后在
render
您可以调用stage.act()
,它更新所有Actor
的位置,旋转,缩放...,然后调用stage.draw()
,它将为所有Actor
调用draw()
。 Image allready handles the draw()
so you don't need to care about that anymore. 图像已经准备好处理
draw()
因此您不再需要关心它。
You can also do it without scene2d
, by updating everything yourself: 您也可以在不使用
scene2d
情况下自己进行更新:
You can store a int rotationSpeed
in degrees/sec 您可以存储
int rotationSpeed
以度/秒为单位
You can store a int moveSpeed
in units/sec (maybe pixel but i would suggest to use camera or viewport and use your own unit, which is equal on all devices) 您可以将
int moveSpeed
存储为单位/秒(也许是像素,但我建议使用摄像头或视口并使用自己的单位,在所有设备上均相同)
Store the float angle
, which is the current rotation of your Texture
and store a Vector2 position
, which contains the x and y position of your Texture
. 存储
float angle
(即Texture
的当前旋转)并存储Vector2 position
(其中包含Texture
的x和y位置)。
If you want to move in x and y direction you can also store a Vector2 direction
, which is a normalized Vector
, giving the percent of movement in x and y direction, but thats a different story. 如果要在x和y方向上移动,还可以存储
Vector2 direction
(这是归一化的Vector
,给出x和y方向上的移动百分比,但这是一个不同的故事。
Then in your render(float delta)
you update everything: 然后在
render(float delta)
更新所有内容:
angle+=delta*rotationSpeed;
angl%=360; // Limits the angle to be <= 360
while (angle < 0) // Unfortunally the "modulo" in java gives negative result for negativ values.
angle+=360;
position.x+=direction.x*moveSpeed*delta;
position.y+=direction.y*movSpeed*delta;
spriteBatch.draw(yourTextureRegion, position.x, position.y, sizeX/2, sizeY/2, sizeX, sizeY, scaleX, scaleY, angle);
For clockwise rotation simply use a negative rotationSpeed
or replace the angle+=
with angle-=
. 对于顺时针旋转,只需使用负
rotationSpeed
或将angle+=
替换为angle-=
。
Hope it helps. 希望能帮助到你。
Following is the implementation to rotate any sprite 以下是旋转任何精灵的实现
batch.draw(sprite,(Gdx.graphics.getWidth() - sprite.getRegionWidth()) / 2.0f,(Gdx.graphics.getHeight() - sprite.getRegionHeight()) / 2.0f,sprite.getRegionWidth()/2.0f,sprite.getRegionHeight()/2.0f, sprite.getRegionWidth(), sprite.getRegionHeight(), 1f, 1f,count, false);
if(count < 0.0f)
count = 360.0f;
else
count --;
Initially set counter to 最初将计数器设置为
private float count =360.0f;
To rotate anticlockwise and horizontally.. create a textureRegion then 要逆时针旋转和水平旋转..创建一个textureRegion然后
Sprite sprite = new Sprite(textureRegion, 0, 0, 128, 128);
sprite.setPosition(++mX, 0);
angle++;
sprite.setRotation(angle);
sprite.draw(batcher);
You can also use the Scene2D actions. 您也可以使用Scene2D操作。 I have an example here with asteroid-type thing falling down the screen and rotating.
我这里有一个例子,小行星类型的东西从屏幕上掉下来旋转。
http://www.netthreads.co.uk/2012/02/09/libgdx-scene2d-demo-with-scene-transitions/ http://www.netthreads.co.uk/2012/02/09/libgdx-scene2d-demo-with-scene-transitions/
You can do it too like this: 您也可以这样做:
on your create method 在您的创建方法上
sprite.setOrigin(sprite.getWitdh() /2f, sprite.getHeight() /2f);
sprite.setPosition( 0, 200 ); //200 it's a example
on your render(float delta) 在您的渲染(浮动增量)上
sprite.setX( sprite.getX() + delta ).setRotation( sprite.getRotation() + delta );
Here is a simple to rotate an actor in libgdx. 在libgdx中旋转actor很简单。 First you need to set the origin:
首先,您需要设置原点:
img.setOrigin(getWidth/2,getHeight/2);
And then you can rotate clockwise and anticlockwise according to your need: 然后您可以根据需要顺时针和逆时针旋转:
img.rotate(2f); or img.rotate(-2f);
So the following sample worked for me (infinite rotation) 因此以下示例对我有用(无限旋转)
Method 1: (recommended) 方法1:(推荐)
loadingActor.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(360, 1)));
Method 2: 方法2:
Image loadingActor = new Image(AssetsController.getInstance().getLoading());
loadingActor.setOrigin(Align.center);
final SequenceAction infiniteRotate = Actions.sequence();
infiniteRotate.addAction(Actions.rotateTo(0 , 0f) );
infiniteRotate.addAction(Actions.rotateTo(360 , 1f) );
loadingActor.addAction(Actions.forever(infiniteRotate));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.