Env water

9 replies [Last post]
Offline
Joined: 01/29/2011

A question on env water
(advance feature lesson 2: http://env3d.org/beta/node/66)
Not sure what I did wrong but I kept getting this error
 
java.lang.NullPointerException
at env3d.advanced.EnvWater.update(EnvWater.java:80)
at env3d.Env.advanceOneFrame(Env.java:650)
at env3d.Env.advanceOneFrame(Env.java:591)
at Game.play(Game.java:79)
at Game.<init>(Game.java:14)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at bluej.runtime.ExecServer$3.run(ExecServer.java:790)
 
This is my play method if that helps

    public void play()
    {
        boolean finished = false;;
        Env env = new EnvAdvanced();
        
        
        env.setDefaultControl(false);
        env.setCameraXYZ(5,5,16);
        env.setCameraPitch(-15);
        
        env.setRoom(createSkyRoom());
        
        EnvWater water = new EnvWater();
        water.setScale(3);
        water.setX(4);
        water.setY(1);
        water.setZ(6);
        env.addObject(water);
        water.addReflectObject(character);
        
        
        // Game loop (default)
        while (!finished)
        {
            // Gather the user input
            int currentKeyDown = env.getKeyDown();
            
            if (currentKeyDown == 1) {
                // escape button - exit game (default)
                finished = true;
            }
            
            env.advanceOneFrame();
        }
        env.exit();
    }

Much appreciated!

Offline
Joined: 12/13/2010
Has the character object been

Has the character object been created?

Offline
Joined: 01/29/2011
 Sorry forgot to take that

 Sorry forgot to take that part away when pasting the code >< haha
but yes, the character is created.
Here's the whole game class (just in testing stage at the moment)
 

import env3d.Env;
import org.lwjgl.input.Keyboard;
import env3d.advanced.EnvAdvanced;
import env3d.advanced.EnvWater;
import env3d.advanced.EnvSkyRoom;

public class GameClass
{
    private Character character;

    public GameClass()
    {
        character = new Character();
        play();
    }
    
    public EnvSkyRoom createSkyRoom() {
        // All the textures required by the skybox
        String textureDir = "textures/skybox/";
        String north = textureDir+"north.jpg";
        String east = textureDir+"east.jpg";
        String south = textureDir+"south.jpg";
        String west = textureDir+"west.jpg";
        String top = textureDir+"top.jpg";
        String bottom = textureDir+"bottom.jpg";
        EnvSkyRoom skyroom = new EnvSkyRoom(5, 5, 5,
                north, east, south, west, top, bottom);
          
        return skyroom;
    }
    
    public void play()
    {
        boolean finished = false;;
        Env env = new EnvAdvanced();
        
        
        env.setDefaultControl(false);
        env.setCameraXYZ(5,5,16);
        env.setCameraPitch(-15);
        env.addObject(character);
        
        env.setRoom(createSkyRoom());
        
        EnvWater water = new EnvWater();
        water.setScale(3);
        water.setX(4);
        water.setY(1);
        water.setZ(6);
        env.addObject(water);
        water.addReflectObject(character);
        
        
        // Game loop (default)
        while (!finished)
        {
            // Gather the user input
            int currentKeyDown = env.getKeyDown();
            
            if (currentKeyDown == 1) {
                // escape button - exit game (default)
                finished = true;
            } else if (currentKeyDown == 200) {
                // Up arrow
                character.runZ(-0.1);
            } else if (currentKeyDown == 208) {
                // Down arrow
                character.runZ(0.1);
            } else if (currentKeyDown == 203) {
                // Left arrow
                character.runX(-0.1);
            } else if (currentKeyDown == 205) {
                // Right arrow
                character.runX(0.1);
            }
            
            character.rotate();
            
            env.advanceOneFrame();
        }
        env.exit();
    }
}

 

KMacKenzie's picture
Offline
Joined: 12/10/2010
Any objects that you are

Any objects that you are going to reflect off the water need to be added to the environment AFTER the water is created... so
env.addObject(character);
has to go AFTER
EnvWater water = new EnvWater();
Perhaps Jason can shed some light on why this is exactly, because I am not certain myself, but this should fix your nullPointerError
 
Cheers

Offline
Joined: 01/29/2011
 Toke the reflecting water

 Toke the reflecting water code away and no error message pops out, but now I get a dead screen for some reason...

AttachmentSize
a.JPG 26 KB
KMacKenzie's picture
Offline
Joined: 12/10/2010
Its not a dead screen, its

Its not a dead screen, its because your sky room dimensions are 5,5,5.
Right now your camera and water are both placed outside of your skyroom.
Set env.setCameraControl(true); and explore the area to see what im talking about.

KMacKenzie's picture
Offline
Joined: 12/10/2010
Also, you can leave the

Also, you can leave the reflecting water code in there. Just move this line of code
    public void play()
    {
        boolean finished = false;;
        Env env = new EnvAdvanced();
       
       
        env.setDefaultControl(false);
        env.setCameraXYZ(5,5,16);
        env.setCameraPitch(-15);

        (WAS HERE)        

        env.setRoom(createSkyRoom());
       
        EnvWater water = new EnvWater();
        water.setScale(3);
        water.setX(4);
        water.setY(1);
        water.setZ(6);
        env.addObject(water)
 
        env.addObject(character); (NOW HERE)

        water.addReflectObject(character);
 
With

Offline
Joined: 12/13/2010
The character must be created

The character must be created after env.setRoom().  Basically, the setRoom method clears the room so the character will not be there anymore to be reflected by the water. 
 

Offline
Joined: 01/29/2011
Changed the code and it works

Changed the code and it works now.
Thanks!
Another question though, not sure if this is my computer or what...
The water effect is only a 2D square for some reason, not a 3D cube.
Here's the newest code:

import env3d.Env;
import org.lwjgl.input.Keyboard;
import env3d.advanced.EnvAdvanced;
import env3d.advanced.EnvWater;
import env3d.advanced.EnvSkyRoom;

public class GameClass
{
    private Character character;

    public GameClass()
    {
        play();
    }
    
    public EnvSkyRoom createSkyRoom() {
        // All the textures required by the skybox
        String textureDir = "textures/skybox/";
        String north = textureDir+"north.jpg";
        String east = textureDir+"east.jpg";
        String south = textureDir+"south.jpg";
        String west = textureDir+"west.jpg";
        String top = textureDir+"top.jpg";
        String bottom = textureDir+"bottom.jpg";
        EnvSkyRoom skyroom = new EnvSkyRoom(5, 5, 5,
                north, east, south, west, top, bottom);
          
        return skyroom;
    }
    
    public void play()
    {
        boolean finished = false;
        Env env = new EnvAdvanced();
        
        // env.setDefaultControl(false);
        // env.setCameraXYZ(-5,-5,16);
        // env.setCameraPitch(-15);
        
        env.setRoom(createSkyRoom());
        
        character = new Character();
        env.addObject(character);
        
        EnvWater water = new EnvWater();
        water.setScale(3);
        water.setX(4);
        water.setY(1);
        water.setZ(6);
        env.addObject(water);
        
        
        // Game loop (default)
        while (!finished)
        {
            // Gather the user input
            int currentKeyDown = env.getKeyDown();
            
            if (currentKeyDown == 1) {
                // escape button - exit game (default)
                finished = true;
            } else if (currentKeyDown == 200) {
                // Up arrow
                character.runZ(-0.1);
            } else if (currentKeyDown == 208) {
                // Down arrow
                character.runZ(0.1);
            } else if (currentKeyDown == 203) {
                // Left arrow
                character.runX(-0.1);
            } else if (currentKeyDown == 205) {
                // Right arrow
                character.runX(0.1);
            }
            
            character.rotate();
            
            env.advanceOneFrame();
        }
        env.exit();
    }
}

 

AttachmentSize
2d.PNG 435.55 KB
Offline
Joined: 12/13/2010
Actually, water is suppose to

Actually, water is suppose to be just a 2D square.  You rotate the square and place it at the proper place to create your landscape.