[英]AndEngine - Getting Center Coordinates of a Sprite
I have a sprite in my game and I want to get it's center coordinates. 我的游戏中有一个精灵,我想获取它的中心坐标。 The sprite can be rotated though so I can't just get it's x/y coordinates then add half of the sprite's width/height because the getWidth and getHeight methods return the dimensions of the original, unrotated sprite.
尽管可以旋转精灵,所以我不能只获得它的x / y坐标,然后加上精灵的宽度/高度的一半,因为getWidth和getHeight方法返回原始的未旋转精灵的尺寸。
I tried getSceneCenterCoordinates() but that for some reason returns the same coordinates for all sprites even if they are nowhere near each other. 我试过了getSceneCenterCoordinates(),但是由于某种原因,即使所有小精灵彼此之间都不远,它们也会返回相同的坐标。
Here's a graphic to describe what I want, the red dot is the coordinate I want, and the width/height labels on the right side figure respresent what I WANT the getWidth/Height methods to return (but they don't): 这是一张描述我想要的图形,红点是我想要的坐标,右侧图上的宽度/高度标签表示我想要getWidth / Height方法返回的内容(但他们没有):
You can use coordinates transformer 您可以使用坐标转换器
final float[] spriteCoordinates = sprite.convertLocalToSceneCoordinates(x,y);
final float canonX = spriteCoordinates[VERTEX_INDEX_X];
final float canonY = spriteCoordinates[VERTEX_INDEX_Y];
Sprite sprite_in_fixedpoint_of_the_first_sprite=new Sprite(canonX,canonY,textureregion)
Get the minimum and maximum value of all the corners (for both - x and y components). 获取所有角的最小值和最大值(对于-x和y分量)。 Then take average from max and min to get middle:
然后取最大和最小值的平均值为中间值:
middleX = (maxX + minX) / 2
Repeat the same for y component. 对y分量重复相同的步骤。
Consider something like this 考虑这样的事情
Rectangle test = new Rectangle(100, 100, 50, 100, vboManager);
mainScene.attachChild(test);
System.out.println("Before RotCtrX = " + test.getRotationCenterX());
System.out.println("Before RotCtrY = " + test.getRotationCenterY());
test.setRotation(45);
System.out.println("After RotCtrX = " + test.getRotationCenterX());
System.out.println("After RotCtrY = " + test.getRotationCenterY());
and the result are 结果是
System.out(4526): Before RotCtrX = 25.0
System.out(4526): Before RotCtrY = 50.0
System.out(4526): After RotCtrX = 25.0
System.out(4526): After RotCtrY = 50.0
AndEngine Entities rotate around the center (unless you change the RotationCenter values), so applying setRotate() will not affect the "center" point location AndEngine实体围绕中心旋转(除非更改RotationCenter值),因此应用setRotate()不会影响“中心”点的位置
those "center" points are relative to the Entity, so if you need the actual screen coordinates, you will need to add those to the getX() and getY() values - which BTW also won't change based solely on applying a setRotate() 这些“中心”点是相对于实体的,因此,如果您需要实际的屏幕坐标,则需要将其添加到getX()和getY()值中-BTW也不会仅基于应用setRotate而改变()
I figured out how to use getSceneCenterCoordinates() properly, which is to hand it a float[]. 我弄清楚了如何正确使用getSceneCenterCoordinates(),这是将其交给float []。 Here is my solution:
这是我的解决方案:
float[] objCenterPos = new float[2];
obj.sprite.getSceneCenterCoordinates(objCenterPos);
Log.d(this.toString(), "Coordinates: ("+objCenterPos[0]+","+objCenterPos[1]+")");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.