在Java中绕Y轴旋转图像?

发布于 2024-08-31 16:05:40 字数 229 浏览 3 评论 0原文

我需要绕 y 轴旋转 2d 精灵。例如,我有一个飞机的二维俯视图精灵。当用户转动飞机时,机翼应向屏幕倾斜(或向外倾斜),以表明飞机正在转动。

有没有办法将图像放入java3d中,旋转它,然后将其放回到缓冲图像中? 或者也许以某种方式知道像素在到达/离开屏幕时应该如何变化,我可以通过弄乱光栅来实现这一点。我知道如何在绕 y 轴旋转后获得每个像素的 x 位置,但当然,仅仅掌握这些知识就会使图像看起来像被压扁,因为旋转后像素重叠。

I need to rotate a 2d sprite about the y axis. E.g., I have a 2d top-view sprite of an aircraft. When the user turns the aircraft the wings should tilt into (or out of) the screen to show that it is turning.

Is there a way to put the image into java3d, rotate it, and then put it back into a buffered image?
Or maybe somehow knows how the pixels should change as they come to / away from the screen and I can just mess with the rasters to accomplish this. I know how to get the resulting x positions of each pixel after a rotation about the y-axis, but of course just having this knowledge makes the image look like it gets squished since the pixels overlap after the rotation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凝望流年 2024-09-07 16:05:40

我相信您可以使用剪切变换来实现 YZ 旋转,这与在 Adob​​e Illustrator 等设计应用程序中用于以等距透视绘制对象类似。

也许这个文档会对你有帮助,PDF似乎是离线的,但谷歌的缓存中有一个副本。

3D使用剪切变换进行体积旋转

我们证明任意 3D 旋转可以分解为四个 2D 梁剪切。

I believe you can achieve the YZ rotation using shear transforms, something similar used to draw objects in isometric perspective in design apps such as Adobe Illustrator.

Maybe this document will help you, the PDF seems to be offline, but there's a copy in Google's cache.

3D Volume Rotation Using Shear Transformations

We show that an arbitrary 3D rotation can be decomposed into four 2D beam shears.

小糖芽 2024-09-07 16:05:40

如果它是 BufferedImage 格式,则可以使用 AffineTransform 来旋转它。有关示例,请参阅旋转 BufferedImage 时遇到的问题

If you have it in a BufferedImage format you can use an AffineTransform to rotate it. See Problems rotating BufferedImage for an example.

拥抱没勇气 2024-09-07 16:05:40

我相信您可以使用透视扭曲或变换来完成类似的事情。 JAI(Java高级成像)具有这种能力。

http: //java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#58571

I believe you could accomplish something like this using a perspective warp or transformation. JAI (Java Advanced Imaging) has this capability.

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html#58571

街角迷惘 2024-09-07 16:05:40

也许有点偏离主题,但为什么不研究一下 Java 游戏引擎呢?也许他们已经解决了这个问题(以及将来会遇到的其他问题,例如双缓冲、声音、输入)。您可能会发现已经有一个经过充分测试的 API 可以满足您的需求。

http://en.wikipedia.org/wiki/List_of_game_engines

Perhaps a bit off topic but why not look into a Game Engine for Java? Maybe they have solved this already (and other problems that will encounter in the future e.g. double buffering, sound, input). You might find that there is already a well tested API for what you want.

http://en.wikipedia.org/wiki/List_of_game_engines

情痴 2024-09-07 16:05:40

好吧,如果您必须旋转图像,则可以使用“变换”,就像 Tom 所说的那样。如果您使用矢量图形,这只是一点数学知识。在这个例子中,飞机只是一个三角形,有一条额外的线指向它的航向:

public void rotateRight() {
    heading = (heading + vectorIncrement);
}

public void rotateLeft() {
    heading = (heading - vectorIncrement);
}

public synchronized void render(Graphics2D g) {
    g.setColor(COLOR_SHIP);            

    // Main line ship
    g.drawLine((int)xPos, (int)yPos, (int)(xPos+Math.cos(heading) * width), (int)(yPos+Math.sin(heading) * height) );
    g.drawLine((int)xPos, (int)yPos, (int)(xPos-Math.cos(heading) * width/2), (int)(yPos-Math.sin(heading) * height/2) );        

    // Wings
    p = new Polygon();
    p.reset();
    p.addPoint((int)(xPos+Math.cos(heading) * width), (int)(yPos+Math.sin(heading) * height) );
    p.addPoint((int)(xPos+Math.cos((heading+90)%360) * width), (int)(yPos+Math.sin((heading+90)%360) * height) );
    p.addPoint((int)(xPos+Math.cos((heading-90)%360) * width), (int)(yPos+Math.sin((heading-90)%360) * height) );
    g.drawPolygon(p);
}

这种插值也可以应用于图像以获得所需的旋转。

Well, if you have to rotate an image, a Transformation is the way to go, just like Tom said. If you are working with vectorial graphics, it's just a little math. In this example, the aircraft is simply a triangle with an extra line pointing it's heading:

public void rotateRight() {
    heading = (heading + vectorIncrement);
}

public void rotateLeft() {
    heading = (heading - vectorIncrement);
}

public synchronized void render(Graphics2D g) {
    g.setColor(COLOR_SHIP);            

    // Main line ship
    g.drawLine((int)xPos, (int)yPos, (int)(xPos+Math.cos(heading) * width), (int)(yPos+Math.sin(heading) * height) );
    g.drawLine((int)xPos, (int)yPos, (int)(xPos-Math.cos(heading) * width/2), (int)(yPos-Math.sin(heading) * height/2) );        

    // Wings
    p = new Polygon();
    p.reset();
    p.addPoint((int)(xPos+Math.cos(heading) * width), (int)(yPos+Math.sin(heading) * height) );
    p.addPoint((int)(xPos+Math.cos((heading+90)%360) * width), (int)(yPos+Math.sin((heading+90)%360) * height) );
    p.addPoint((int)(xPos+Math.cos((heading-90)%360) * width), (int)(yPos+Math.sin((heading-90)%360) * height) );
    g.drawPolygon(p);
}

This kind of interpolation can also be applied to images in order to get the desired rotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文