The Camera

Hi. This is my first time posting here so I hope that I’m in the correct area. I’m trying to set up the camera (Camera not FlyByCamera or CameraNode) so I can track it. The reason I need to track the camera is because I’m trying to build a first person game and I need the camera’s position to place the model there.Every time I call cam.getLocation() and assign it to a vector, I get a null error. I have tried (vector name) = … ; and (vector name) .set( …); I have initialized the Camera and given it the location of 0f, 0f, 10f. Also, I’m trying to set it to look at the origin so I used the code:


Vector3f v = new Vector3f(cam.getLocation()); //This doesn't return null. Maybe something below is causing a null location?
        Vector3f object1 = new Vector3f(0f, 0f, 0f);
        float junk1;
        junk1 = (float) Math.sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
        float junk2;
        junk2 = (float) Math.sqrt((object1.x * object1.x) + (object1.y * object1.y) + (object1.z * object1.z));
        float norm_u_norm_v = junk1 * junk2; 
    float real_part = norm_u_norm_v + (v.x * object1.x) + (v.y * object1.y) + (v.z * object1.z);
    
    if (real_part < 1.e-6f * norm_u_norm_v)
    {
        Vector3f w;
        real_part = 0.0f;
        boolean w2;
        w2 = Math.abs(v.x) > Math.abs(object1.z);
                if (w2 == true) { 
            w = new Vector3f(-object1.y, object1.x, 0.f);
        }
        if (w2 != true) {
                                w = new Vector3f(0.f, -object1.z, object1.y);
        }
    if (real_part < 1.e-6f * norm_u_norm_v == false) {
        w.cross(object1, v);
    }

    Quaternion a = new Quaternion(real_part, w.x, w.y, w.z);
        cam.setRotation(a);

To set the rotation. That formula is from Internet C++ code, I converted. If you guys and girls could check that too, that would be great.

Also, how would I override the Escape key as to not have the game close 24/7?

Finally: I’m confused on how to set the Camera on how to move. Not player movement, but how to look. How do I use MouseAxisTrigger(MouseInput.AXIS_X, true) and the others? Or are they to change values not read them? I would use Java’s listeners except I couldn’t find the name of the Frame jME3 uses as to attach a MouseListener… I would be using FlyByCamera and just track all the movements, yet I don’t know what unit the .setRotationSpeed is in.

Thanks for everything,
–TheHero

If this:
Vector3f v = new Vector3f(cam.getLocation());

…is throwing a null pointer exception then cam is null. If it’s not a null pointer exception then I’m not sure what you mean because nothing in that line is null.

The rest of what you ask can be answered by looking through the tutorials and looking in the FlyByCamera source code which is one click away. You are kind of so far into the woods at this point that it would take me a wall of text to catch you up and most of it is in those places already.

Do note that even if you get your current approach working, it’s kind of backwards. Like dragging a person around by their eye. While you will still have to work out how to move something, you will be happier in the long run if you move your game object and let things like the camera sync to that.

The line you referenced doesn’t equal null… I would actually love a wall of text. It would give me something to truly go through and help me understand this completely (hopefully).

Thanks for responding,
TheHero

Look, man, I’m trying to guess the best I can from your post. But you said:
“.Every time I call cam.getLocation() and assign it to a vector, I get a null error.”

…so how am I supposed to interpret that.

I’m not going to take lots of time to type a wall of text that plenty of people have worked hard on and put in the tutorials. We make the tutorials precisely so we don’t have to reanswer the same questions with walls of text all the time.

…Especially since the FlyByCamera source is one click away and does basically exactly what you want for the rest of your post. You need to learn the right SDK button to click to show you the source for things.

Sorry. I guess that that can be misinterpreted. I thought it would be clear since I put in the comment this doesn’t return null. I have viewed the code and gone through the tutorials. I’m sorry if I’m being an inconvience. I’ll just mess around with the Custom Input. Thanks for everything,
–TheHero

@TheHero said: Sorry. I guess that that can be misinterpreted. I thought it would be clear since I put in the comment this doesn't return null. I have viewed the code and gone through the tutorials. I'm sorry if I'm being an inconvience. I'll just mess around with the Custom Input. Thanks for everything, --TheHero

Yeah, misunderstanding is easy when you say:
“Every time I call cam.getLocation() and assign it to a vector, I get a null error.”

And then right after it show code that calls getLocation() with a comment saying that you don’t get a null error.

So which is it? Do you have an error or not? And if you have an error, can you actually show that code instead of not relevant code?

The code you have is WAAAAAY more complicated than it needs to be. I can’t even follow what it’s supposed to be doing… but likely if you look at the source for FlyByCamera (which is only a menu item away in the SDK or a few clicks away on the web) it does exactly what you want moving a camera. Here, I’ll even shorten it to just one click:

You really really need to go through all tutorials. In my game that im developing i have “DelayedCamera” which chases player’s rotation slowly at fixed distance, “OrbitCamera” you know what, “AimCamera” that rotates player’s model where you look with mouse… and i never needed that code you used.

Some responses:

The reason I need to track the camera is because I’m trying to build a first person game and I need the camera’s position to place the model there.


model.setLocalTranslation(cam.getLocation());

Also, I’m trying to set it to look at the origin…


cam.lookAt(new Vector3f(0,0,0), Vector3f.UNIT_Y); // unit_y can be any axis, its like human head, it is where it is up
// or 
model.lookAt(new Vector3f(0,0,0), Vector3f.UNIT_Y);

To set the rotation. That formula is from Internet C++ code, I converted. If you guys and girls could check that too, that would be great.

  • Nope, you are doing it wrong

Also, how would I override the Escape key as to not have the game close 24/7?

  • when you solve this by yourself, you will earn a degree in jme’s input system. hint: some appstate that is added by default adding some keys including escape

Finally: I’m confused on how to set the Camera on how to move. Not player movement, but how to look.

How do I use MouseAxisTrigger(MouseInput.AXIS_X, true) and the others? Or are they to change values not read them?

I would use Java’s listeners except I couldn’t find the name of the Frame jME3 uses as to attach a MouseListener… I would be using FlyByCamera and just track all the movements, yet I don’t know what unit the .setRotationSpeed is in.