Heighmap exercise

3 replies [Last post]
Offline
Joined: 12/10/2010

 I run into some issue with implementing my own camera thingie ... it starts to misbehave and W and A move camera in same direction after i change view with the mouse :
 
code:
double step = 0.3;
        double newCamX, newCamY, newCamZ;
        newCamY = newCamX = newCamZ = 0.0;
          
        // Exit when the escape key is pressed
        while (env.getKey() != 1) {
            
            double prevCamX = env.getCameraX();
            double prevCamY = env.getCameraY();
            double prevCamZ = env.getCameraZ();
            
            
            
            if (env.getKeyDown(Keyboard.KEY_W)) {
                newCamX = env.getCameraX() - step*Math.sin(Math.toRadians(env.getCameraYaw()));
                newCamZ = env.getCameraX() - step*Math.cos(Math.toRadians(env.getCameraYaw()));
                newCamY = terrain.getHeight(newCamX, newCamZ);
            }
 
            if (env.getKeyDown(Keyboard.KEY_S)) {
                newCamX = env.getCameraX()+ step*Math.sin(Math.toRadians(env.getCameraYaw()));
                newCamZ = env.getCameraX()+ step*Math.cos(Math.toRadians(env.getCameraYaw()));
                newCamY = terrain.getHeight(newCamX, newCamZ);
            }
 
            if (env.getKeyDown(Keyboard.KEY_A)) {
                newCamX = env.getCameraX()- step*Math.cos(Math.toRadians(env.getCameraYaw()));
                newCamZ = env.getCameraZ()+step*Math.sin(Math.toRadians(env.getCameraYaw()));
                newCamY = terrain.getHeight(newCamX, newCamZ);
 
            }
            if (env.getKeyDown(Keyboard.KEY_D)) {
                newCamX = env.getCameraX()+step*Math.cos(Math.toRadians(env.getCameraYaw()));
                newCamZ = env.getCameraZ()-step*Math.sin(Math.toRadians(env.getCameraYaw()));
                newCamY = terrain.getHeight(newCamX, newCamZ);
            }
            
            
            env.setCameraXYZ(newCamX,newCamY,newCamZ);
            
 
            double newPitch = env.getCameraPitch()+env.getMouseDY();
             
            // Restrict the pitch to within a "reasonable" amount
            if (newPitch > 70) {
                env.setCameraPitch(70);
            } else if (newPitch < -50) {
                env.setCameraPitch(-50);
            } else {
                env.setCameraPitch(newPitch);
            }
             
 

Offline
Joined: 12/13/2010
You made a mistake in the

You made a mistake in the newCamZ line for W and S:
newCamZ = env.getCameraX() - step*Math.cos(Math.toRadians(env.getCameraYaw()));
Should be env.getCameraZ().
 
 

Offline
Joined: 12/10/2010
 Oops :(

 Oops :(

Offline
Joined: 12/10/2010
 I have this code now for

 I have this code now for checking of movements, but it seems I can climb the hill a bit and never come back down or turn around and move to the hill anyhow
double i = Math.abs(newCamY - prevCamY);
            env.setDisplayStr( "Prev: " + prevCamY + " New: " + newCamY + " Diff: " + i );
            if ( i <= 1.1 )
            {
                env.setCameraXYZ(newCamX,newCamY,newCamZ);