简体   繁体   English

在Java中绕Y轴旋转图像?

[英]Rotate an image around the Y-Axis in Java?

I need to rotate a 2d sprite about the y axis. 我需要围绕y轴旋转2d精灵。 Eg, I have a 2d top-view sprite of an aircraft. 例如,我有一个飞机的2D顶视图精灵。 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? 有没有一种方法可以将图像放入java3d中,旋转它,然后再将其放回到缓冲的图像中? 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. 我知道在绕y轴旋转后如何获得每个像素的x位置,但是当然有了这些知识会使图像看起来像被压缩了,因为像素在旋转后会重叠。

If you have it in a BufferedImage format you can use an AffineTransform to rotate it. 如果您具有BufferedImage格式,则可以使用AffineTransform对其进行旋转。 See Problems rotating BufferedImage for an example. 有关示例,请参见旋转BufferedImage的问题

I believe you could accomplish something like this using a perspective warp or transformation. 我相信您可以使用透视变形或变换来完成类似的操作。 JAI (Java Advanced Imaging) has this capability. JAI(Java高级映像)具有此功能。

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

Perhaps a bit off topic but why not look into a Game Engine for Java? 也许有点偏离主题,但是为什么不研究Java游戏引擎呢? Maybe they have solved this already (and other problems that will encounter in the future eg double buffering, sound, input). 也许他们已经解决了这个问题(以及将来会遇到的其他问题,例如双缓冲,声音,输入)。 You might find that there is already a well tested API for what you want. 您可能会发现已经有经过测试的API。

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

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. 这种内插也可以应用于图像以获得所需的旋转。

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. 我相信您可以使用剪切变换来实现YZ旋转,这类似于在Adobe Illustrator等设计应用程序中以等轴测透视图绘制对象的方法。

Maybe this document will help you, the PDF seems to be offline, but there's a copy in Google's cache. PDF可能已离线,但该文档可能会为您提供帮助,但Google缓存中有一个副本。

3D Volume Rotation Using Shear Transformations 使用剪切变换的3D体积旋转

We show that an arbitrary 3D rotation can be decomposed into four 2D beam shears. 我们显示了可以将任意3D旋转分解为四个2D光束剪。

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

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