安卓:根据目的地的坐标如何旋转移动的动画精灵坐标、目的地、根据、精灵

2023-09-12 22:50:01 作者:残城碎梦

我的应用程序的火灾围绕一个画布,然后在屏幕上移动朝斧精灵实例/ y坐标。我希望能绕其中心的精灵,使其朝向它的目的地坐标。我使用的是精灵表,并曾与裁剪的问题。我还发现了很多很好的例子,但似乎没有任何遮盖我寻找什么。 这个例子是非常接近,但为了提高效率,我使用的是ImagePooler类,不能重装每个抽奖/旋转图像。因此,如果有人对如何旋转preloaded形象的想法瓦特/出切我的精灵表,我将非常感激。

My application fires up sprite instances around a Canvas which then move across the screen towards a x/y coordinate. I would like to be able to rotate the sprite around its center so that it faces its destination coordinates. I am using a sprite-sheet and have had issues with clipping. I have also found a lot of good examples but nothing seems to cover exactly what I'm looking for. This example is very close but for efficiency I am using an ImagePooler class and cannot reload an image on each draw/rotation. So if someone had an idea on how to rotate the preloaded image w/out cutting my sprite-sheet I would be very grateful.

推荐答案

首先,它很容易旋转精灵,你可以使用帆布或矩阵:

First it is easy to rotate a sprite you can use canvas or matrix:

Matrix matrix = new Matrix();
matrix.postRotate(angle, (ballW / 2), (ballH / 2)); //rotate it
matrix.postTranslate(X, Y); //move it into x, y position
canvas.drawBitmap(ball, matrix, null); //draw the ball with the applied matrix

// method two 
canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas' matrix
canvas.drawBitmap(ball, X, Y, null); //draw the ball on the "rotated" canvas
canvas.restore(); //rotate the canvas' matrix back
//in the second method only the ball was roteded not the entire canvas

要打开它朝你需要知道的精灵和目的地之间的夹角目标:

To turn it towards a destination you need to know the angle between the sprite and the destination:

spriteToDestAngle =  Math.toDegrees(Math.atan2((spriteX - destX)/(spriteY - destY)));

现在,所有你需要做的就是用这个角度精灵旋转加上与像angleShift恒定的依赖到你的精灵最初指向ajust它。

Now all you need to do is to use this angle for the sprite rotation plus ajust it with a constant like angleShift which depends to where your sprite initially points.

我不知道这是否会工作,但希望它可能给你一些想法......

I am not sure if this will work but hope it may give you some ideas...