简体   繁体   English

LibGDX 1.5围绕其中心旋转精灵

[英]LibGDX 1.5 rotating sprite around its center

I am trying to rotate a sprite around its center, but no matter what I try, I always get it rotating around a corner. 我试图围绕它的中心旋转精灵,但无论我尝试什么,我总是让它在拐角处旋转。 I create the sprite in this way: 我以这种方式创建精灵:

Pixmap pixmap = new Pixmap(64, 64, Format.RGBA8888);
pixmap.setColor(153/255f, 255/255f, 153/255f, 255/255f);
pixmap.fillTriangle(0, 0, 0, 32, 32, 16);
Texture texture = new Texture(pixmap);
triangle = new Sprite(texture);
triangle.setSize(3, 3);
triangle.setOriginCenter();
triangle.setPosition(0 - triangle.getWidth() / 2.0f, 0 - triangle.getHeight() / 2.0f);

then I rotate it every deltaTime in this way: 然后我以这种方式每个deltaTime旋转它:

triangle.rotate(90 * deltaTime);

and render it like this: 并像这样呈现:

batch.begin();
worldController.triangle.draw(batch);
batch.end();

I am following the example in the book "Learning LibGDX Game Development, Second edition", so I have a WorldController and a WorldRenderer. 我正在按照“学习LibGDX游戏开发,第二版”一书中的例子,所以我有一个WorldController和一个WorldRenderer。 What am I missing? 我错过了什么? Shouldn't it be enough to set the sprite origin to its center and draw it? 将精灵原点设置为中心并绘制它是不是足够了? This is the behaviour I am having: 这是我的行为:

在此输入图像描述

while I would like it to rotate "in place". 虽然我希望它“旋转到位”。

It seems that you have missed the line 看来你错过了这条线

triangle.setOrigin(originX, originY);

(originX, originY) being the center coordinates of your triangle. (originX,originY)是三角形的中心坐标。

EDIT: The reason why your current code doesn't work is because the current center origin is (0, 0). 编辑:您当前代码不起作用的原因是因为当前中心原点是(0,0)。

(I am not sure why but it appeared to be the center when I tested your code). (我不确定为什么,但在我测试你的代码时它似乎是中心)。 You can add logs to your code through using 您可以通过使用将日志添加到代码中

Gdx.app.log("Tag", "LogInformation");

Here is what you should do 这是你应该做的

originX = (0+32)/2;
originY = (0+32)/2;

or 要么

originX = (lowestXValue+highestXValue)/2;
originY = (lowestYValue+heightYValue)/2;

Here is the API on Sprite, it could help you more on rotate() 是Sprite上的API,它可以帮助你更多关于rotate()

Ok, I found the problem. 好的,我发现了问题。 At first I declared the pixmap with a 32x32 size and filled the triangle accordingly. 起初我宣布像素图的大小为32x32并相应地填充三角形。 Then I was messing around with it and changed the size to 64x64, but the triangle was drawn by using the old coordinates, thus occupying the bottom left "subsquare" of the whole pixmap, that's why it was rotating in that way. 然后我正在搞乱它并将大小改为64x64,但三角形是使用旧坐标绘制的,因此占据了整个像素图的左下角“子方格”,这就是它以这种方式旋转的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM