views:

344

answers:

5

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.

A: 

If you have it in a BufferedImage format you can use an AffineTransform to rotate it. See http://stackoverflow.com/questions/2257141/problems-rotating-bufferedimage for an example.

Tansir1
AffineTransform can only do rotations in the x-y plane. I'm not trying to rotate in the x-y plane.
mlaw
A: 

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

Tom
A: 

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

kazanaki
A: 

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.

Federico Cristina
A: 

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.

Vicente Reig
Ah. I haven't tried it out yet, but I think this is exactly what I need.
mlaw