I followed through a Java/LWJGL n lloydgoodall.com for a first person camera. There were about 33 errors that I fixed, though I get a message in the Eclipse console "java.lang.NoSuchMethodError: main Exception in thread "main" " Here is my code:

import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.*;
import org.lwjgl.util.vector.Vector3f;

//First Person Camera Controller
public class FPCameraController
{
    //3d vector to store the camera's position in
    private Vector3f    position    = null;
    //the rotation around the Y axis of the camera
    private float       yaw         = 0.0f;
    //the rotation around the X axis of the camera
    private float       pitch       = 0.0f;
    //Constructor that takes the starting x, y, z location of the camera
    public FPCameraController(float x, float y, float z)
    {
        //instantiate position Vector3f to the x y z params.
        position = new Vector3f(x, y, z);
    }
    //increment the camera's current yaw rotation
    public void yaw(float amount)
    {
        //increment the yaw by the amount param
        yaw += amount;
    }

    //increment the camera's current yaw rotation
    public void pitch(float amount)
    {
        //increment the pitch by the amount param
        pitch += amount;
    }
    //moves the camera forward relitive to its current rotation (yaw)
    public void walkForward(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw));
    }

    //moves the camera backward relitive to its current rotation (yaw)
    public void walkBackwards(float distance)
    {
        position.x += distance * (float)Math.sin(Math.toRadians(yaw));
        position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
    }

    //strafes the camera left relitive to its current rotation (yaw)
    public void strafeLeft(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
    }

    //strafes the camera right relitive to its current rotation (yaw)
    public void strafeRight(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
    }

    //translates and rotate the matrix so that it looks through the camera
    //this dose basic what gluLookAt() does
    public void lookThrough()
    {
        //roatate the pitch around the X axis
        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        //roatate the yaw around the Y axis
        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        //translate to the position vector's location
        GL11.glTranslatef(position.x, position.y, position.z);
    }

public void gameLoop()
{
    FPCameraController camera = new FPCameraController(0, 0, 0);

    float dx        = 0.0f;
    float dy        = 0.0f;
    float dt        = 0.0f; //length of frame
    float lastTime  = 0.0f; // when the last frame was
    float time      = 0.0f;

    float mouseSensitivity = 0.05f;
    float movementSpeed = 10.0f; //move 10 units per second

    //hide the mouse
    Mouse.setGrabbed(true);

    // keep looping till the display window is closed the ESC key is down
    while (!Display.isCloseRequested() &&
            !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
    {
        time = Sys.getTime();
        dt = (time - lastTime)/1000.0f;
        lastTime = time;


        //distance in mouse movement from the last getDX() call.
        dx = Mouse.getDX();
        //distance in mouse movement from the last getDY() call.
        dy = Mouse.getDY();

        //controll camera yaw from x movement fromt the mouse
        camera.yaw(dx * mouseSensitivity);
        //controll camera pitch from y movement fromt the mouse
        camera.pitch(dy * mouseSensitivity);


        //when passing in the distrance to move
        //we times the movementSpeed with dt this is a time scale
        //so if its a slow frame u move more then a fast frame
        //so on a slow computer you move just as fast as on a fast computer
        if (Keyboard.isKeyDown(Keyboard.KEY_W))//move forward
        {
            camera.walkForward(movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_S))//move backwards
        {
            camera.walkBackwards(movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_A))//strafe left
        {
            camera.strafeLeft(movementSpeed*dt);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_D))//strafe right
        {
            camera.strafeRight(movementSpeed*dt);
        }

        //set the modelview matrix back to the identity
        GL11.glLoadIdentity();
        //look through the camera before you draw anything
        camera.lookThrough();
        //you would draw your scene here.

        //draw the buffer to the screen
        Display.update();
    }

	}
}

Please help :(

Recommended Answers

All 10 Replies

NoSuchMethodError: main

Your class needs a main() method if you are going to start execution in that class by using the java command:
java FPCameraController

The java command looks for and calls the static method: main when it starts executing a class.

Not to be a total Java n00b, but... where? :/ I tried adding it in multiple ways but it didn't work.

it didn't work

Hard to fix a problem I can't see. Please post full text of error messages here.

Add the main method to the class that you want to pass to the java command to start the program.

If it is a problem with your IDE, sorry I can't help you.

Sorry for the really late reply.

What I mean is, what should I insert into the main function, can you tell based off of my code?

Looks like maybe you could change line 75 from

public void gameLoop()

to

public static void main(String[] args)

Yes, gameLoop looks like the main method here.
It's normally considered bad practice to put too much code in main() because this hinders any possible re-use. The ideal main() just instantiates an instance of the class, and calls an instance method that does whatever needs to be done.
So, the public static void main(String[] args) method should just create a new FPCameraController and call its gameLoop method.
The gameLoop method should not create a new instance, ie no line 77. ganeLoop is an instance method, so there must have already been an instance created before this method can be called!

THANK YOU! That helped so much! But another problem :/

It loads the application for about 2 & 1/2 seconds, then closes. The Eclipse console displays:
"Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path"
I DID include -Djava.library.path=\lwjgl-2.5\native\macosx in the VM thing.

Sorry, I have no experience with that library, nor with Java installation under OSX. Hopefully someone else can help here.
J

So nobody has any idea on how to fix this? Sad D: lol

You might want to start a new thread with a better title for your new problem. People might be looking at the old thread name and not even looking at your new question. Give it an appropriate title and someone who is familiar with that library might respond.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.