用Java在Y轴周围旋转图像?

时间:2010-05-10 23:50:44

标签: java graphics rotation

我需要围绕y轴旋转2d精灵。例如,我有一架飞机的2d顶视图精灵。当用户转动飞机时,机翼应该倾斜进入(或离开)屏幕以显示它正在转动。

有没有办法将图像放入java3d,旋转它,然后将其放回缓冲图像? 或者也许以某种方式知道像素在进出屏幕时应该如何改变,我可以搞乱光栅来实现这一点。我知道如何在围绕y轴旋转后得到每个像素的最终x位置,但当然只有这些知识使得图像看起来像是因为像素在旋转后重叠而被压扁。

5 个答案:

答案 0 :(得分:0)

如果您使用BufferedImage格式,可以使用AffineTransform旋转它。有关示例,请参阅Problems rotating BufferedImage

答案 1 :(得分:0)

我相信你可以使用透视扭曲或转换来完成这样的事情。 JAI(Java Advanced Imaging)具有此功能。

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

好吧,如果你必须旋转一个图像,就像汤姆所说的那样,转换是可行的方式。如果你正在使用矢量图形,那只是一个小数学。在这个例子中,飞机只是一个三角形,有一条额外的线指向它的标题:

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);
}

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

答案 4 :(得分:0)

我相信你可以使用剪切变换来实现YZ旋转,类似于在Adobe Illustrator等设计应用程序中以等距透视方式绘制对象。

也许这个文档会对你有所帮助,PDF似乎处于离线状态,但Google的缓存中有一个副本。

3D Volume Rotation Using Shear Transformations

  

我们表明任意3D旋转可以分解为四个2D光束剪。