简体   繁体   English

从中心旋转缩放的图像

[英]Rotate a Scaled Image From the Center

I have some sprites (Well, custom classes that implement Sprite , but whatever) that I resize. 我有一些调整大小的Sprites(嗯,实现Sprite自定义类,但无论如何)。 AndEngine resizes the image from the center, which makes an image placed at 0,0 no longer appear at 0,0. AndEngine从中心调整图像大小,从而使放置在0,0处的图像不再显示在0,0处。 To fix this I applied 为了解决这个问题,我申请了

    sprite.setScaleCenterX(0);
    sprite.setScaleCenterY(0);

This places the image where I want it. 这会将图像放在我想要的位置。 However, now when I rotate the image, the image moves around (If the image were a plain square, rotating it should make no visible change). 但是,现在,当我旋转图像时,图像会四处移动(如果图像是一个普通的正方形,则旋转它应该不会产生可见的变化)。 To fix this I applied 为了解决这个问题,我申请了

    sprite.setRotationCenterX((sprite.getWidth() * sprite.getScaleX()) / 2);
    sprite.setRotationCenterY((sprite.getHeight() * sprite.getScaleY()) / 2);

(For some reason, resizing a Sprite doesn't change the dimensions of the sprite, just the visual image, hence multiplying it by the scale). (由于某种原因,调整Sprite大小不会更改Sprite的尺寸,而只会更改视觉图像,因此将其乘以比例)。 This, however, did not correct the problem, but merely changed where the image moved to when flipped. 但是,这并不能解决问题,只是改变了图像在翻转时移动到的位置。

Is my math off here? 我的数学在这里吗? Wouldn't this center the rotation on the image so that the image doesn't move position? 这样是否不会使旋转在图像上居中,以使图像不移动位置? Or is there something else I'm missing? 还是我还缺少其他东西?

Below is full code: 下面是完整的代码:

    Sprite sprite = new Sprite(0, 0, singleTrackTR, getVertexBufferObjectManager());
    sprite.setScale(scaleX, scaleY);
    sprite.setScaleCenterX(0);
    sprite.setScaleCenterY(0);
    sprite.setRotationCenterX((sprite.getWidth() * sprite.getScaleX()) / 2);
    sprite.setRotationCenterY((sprite.getHeight() * sprite.getScaleY()) / 2);

All your code is correct. 您所有的代码都是正确的。 I tried it myself, both the setProperty(x, y) and the setPropertyX/Y(a) versions. 我自己尝试过setProperty(x,y)和setPropertyX / Y(a)版本。

By any chance, do you have it connected to a Body? 您是否有机会将它连接到身体? Note that the Body also doesn't scale with a Sprite's setScale. 请注意,Body也不随Sprite的setScale缩放。 It has its own setTransform method, which takes x and y (that you both have to divide by PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT) and a rotation value. 它有自己的setTransform方法,该方法使用x和y(必须分别除以PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT)和一个旋转值。

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

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