代码之家  ›  专栏  ›  技术社区  ›  Matt Wonlaw

在Java中围绕Y轴旋转图像?

  •  2
  • Matt Wonlaw  · 技术社区  · 15 年前

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

    有没有一种方法可以将图像放入Java3D中,旋转它,然后将其放回缓冲图像? 或者也许不知何故知道像素在屏幕上或离开屏幕时应该如何变化,我可以用光栅来完成这一点。我知道如何在围绕y轴旋转后得到每个像素的X位置,但是当然,只要有了这个知识,图像就会看起来像被压扁了,因为旋转后像素会重叠。

    5 回复  |  直到 15 年前
        1
  •  0
  •   Vicente Reig    15 年前

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

    也许这份文件会对你有所帮助,PDF文件似乎离线了,但谷歌的缓存里有一份副本。

    3D Volume Rotation Using Shear Transformations

    结果表明,任意三维旋转可以分解为四个二维剪切梁。

        2
  •  0
  •   Community CDub    8 年前

    如果你有一个bufferedimage格式,你可以使用affinetransform来旋转它。见 Problems rotating BufferedImage 举个例子。

        3
  •  0
  •   Tom    15 年前

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

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

        4
  •  0
  •   kazanaki    15 年前

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

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

        5
  •  0
  •   Federico Cristina    15 年前

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

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

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