gpt4 book ai didi

java - 如何在 Slick2D 中绘制从中心旋转的图像?

转载 作者:行者123 更新时间:2023-11-30 07:54:10 24 4
gpt4 key购买 nike

问题如下:我有一个位置,比如说一艘宇宙飞船,并且该位置必须定义在它的中间(例如,由于碰撞问题等)。如果飞船不旋转,图像将如下所示:

spaceship.draw(xPos - 宽度/2 , yPos - 高度/2);

宇宙飞船现在是 Slick2D 中的图像,具有随机角度(0 <= 角度 <360),并且仍然必须在其中心定义位置。 slick 在draw()方法中的作用如下:

enter image description here它在图像周围绘制一个椭圆形(我猜是尽可能小的表面积),然后查看与中心成 135° 角的线与椭圆形(红点)相交的位置,并将其设置为位置。

我仍然想将它居中绘制,这意味着我必须在两个维度上减去一个值(draw(xPos - xOff, yPos - yOff)):

enter image description here

但我无法计算!有人能帮助我吗?我浪费了两天时间来了解问题所在......

非常感谢并问候,

这里是渲染代码:

> public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
> throws SlickException{
>
> // This sets all rotation to the middle of the graphics.
> // The method works RELATIVE TO THE TOP-LEFT-CORNER OF THE IMAGE.
> skin.setCenterOfRotation(skin.getWidth() * Revolve.getGS()/2 + Revolve.getXShift(),
> skin.getHeight() * Revolve.getGS()/2 + Revolve.getYShift());
>
> // This sets the Angle properly
> skin.setRotation(angle);
>
> // This draws the image: xPos and yPos define THE CENTER of the Image.
> skin.draw(
> (float) (Revolve.getXShift() + Revolve.getGS() * xPos),
> (float) (Revolve.getYShift() + Revolve.getGS() * yPos),
> Revolve.getGS()
> );
>
> // Draws the xPos and yPos of the Ship
> g.setColor(Color.white);
> g.drawOval(Revolve.getXShift() + Revolve.getGS() * xPos, Revolve.getYShift() + Revolve.getGS() * yPos, 2, 2);
> }

最佳答案

通过一些代码,一切都会变得清晰。

这是一个解决方案:

@Override
public void init(GameContainer container) throws SlickException
{
this.character = new Image("/home/ronan/Images/20150517010612.jpg");
this.scale = 0.5f;
// When you set your center of rotation is is relative to your image ONLY.
// this will make the center of my image to be the center of the rotation
// NOTHING more to add
this.character.setCenterOfRotation((this.character.getWidth()/2)*this.scale, (this.character.getHeight()/2)*this.scale);
}

@Override
public void update(GameContainer container, int delta) throws SlickException
{
this.rotation +=delta/10;
this.character.setRotation(this.rotation);
}

public void render(GameContainer container, Graphics g) throws SlickException
{
// Here I consider that my position is (500,500). then to draw it, I just substract hal the size of my image.
// It will draw from top-lefft corner but based on my center.
character.draw(500-(this.character.getWidth()/2)*this.scale, 500 - (this.character.getHeight()/2)*this.scale,this.scale);
}

为了清楚起见,我将所有内容分开。

init方法中,您必须设置旋转中心。该中心是相对于图像而言的,因此只需将宽度和高度除以二即可得到图像中心的坐标。当它起作用时,你会乘以你的规模。无需添加更多内容,因为它对于您的图像来说确实是本地的。

在渲染方法中,始终从左上角开始绘制图像。只需减去一半宽度和一半高度即可设置使用您的中心。

只需将此代码改编为您的代码即可,应该会顺利进行。询问您是否需要更高的精度。

enter image description here enter image description here

关于java - 如何在 Slick2D 中绘制从中心旋转的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32908291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com