1

I'm trying to position billboard sprites using their x, y and z coordinates, the camera's position and the rotation of the camera in radians.

I'm looking for an effect similar to the entities in Wolfenstein 3D.

Here's what I have so far:

public void render(Graphics g) {
        double xx = x-camera.x;
        double yy = y-camera.y;
        double zz = z-camera.z;

        xx = rotate2d(xx, zz, camera.rot)[0];
        zz = rotate2d(xx, zz, camera.rot)[1];

        double f = 200/zz;
        xx *= f;
        yy *= f;

        xx += game.width/2;
        yy += game.height/2;

        if(xx >= 0 && xx <= game.width)
            g.drawImage(image, (int) xx, (int) yy, null);
    }

private double[] rotate2d(double x, double y, double rad) {
        double s = Math.sin(rad);
        double c = Math.cos(rad);

        double[] pos = new double[2];
        pos[0] = x*c-y*s;
        pos[1] = y*c+x*s;

        return pos;
    }

The sprites seem to be skewed around the edges of the screen and at a certain rotation they will be positioned completely incorrectly.

The camera's rotation is in radians and game.width and height refer to the width and height of the frame.

Thanks.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
Joel Ham
  • 11
  • 2

0 Answers0