Question

ok so I just started getting into 3D game programming I have a basic world where I have my own physics and basic controls however ever time I factor in speed for my movement it throws off my direction calculations and I don't know how to fix this

here is my camera class

package org.phin.platformer3d.game;

 import org.lwjgl.input.Keyboard;
 import org.lwjgl.opengl.GL11;
 import org.lwjgl.util.glu.GLU;
 import org.phin.platformer3d.lib.Strings;
 import org.phin.platformer3d.object.GameObject;

 public class Camera extends GameObject {

/**
 * the field of view angle 
 */
private float fovAngle;

/**
 * the ratio between points on a object
 */
private float aspectRatio;

/**
 *  nearest clipping point 
 *  (the near render distance so objects are not rendered into the camera
 */
private float near;

/** 
 * farthest clipping point (render distance)
 */
private float far;

private float amtJumped;

private boolean blockKeyUp = false;
private boolean blockKeyDown = false;

/**
 * initializes the <code>field of view angle, aspect ratio, near view clipping and 
 * far clipping (render distance) </code>
 * 
 * @param fovAngle
 * @param ar
 * @param near
 * @param far
 *
 */
public Camera(float fovAngle, float ar, float near, float far) {

    this.fovAngle = fovAngle;
    this.aspectRatio = ar;
    this.near = near;
    this.far = far;

    this.initGL();
}

/**
 * Initializes openGL
 */
private void initGL() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();

    GLU.gluPerspective(this.fovAngle, this.aspectRatio, this.near, this.far);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glEnable(GL11.GL_TEXTURE_2D);

}

/**
 * creates and updates the camera view (called every frame)
 */
public void updateView() {
    GL11.glRotatef(super.getRotX(), super.getX(), 0, 0);
    GL11.glRotatef(super.getRotY(), 0, super.getY(), 0);
    GL11.glRotatef(super.getRotZ(), 0, 0, super.getZ());

    GL11.glTranslatef(super.getX(), super.getY(), super.getZ());    
}

public void setAmtJumped(float amt) {
    this.amtJumped = amt;
}

public void setKeyBlocked(String key, boolean b) {
    if (key.equals("up")) {
        this.blockKeyUp = b;

    } else if (key.equals("down")) {
        this.blockKeyDown = b;

    } 
}

/**
 * translates the camera based on the user key input
 */
private void keyEvent() {

    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        if (!this.blockKeyUp) {
            this.blockKeyDown = false;

            super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY() + 90)));
            super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY() + 90)));

        }

    } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {    
        if (!this.blockKeyDown) {
            this.blockKeyUp = false;

            super.setX(super.getX() + (float) Math.cos(Math.toRadians(super.getRotY() + 90)));
            super.setZ(super.getZ() - (float) Math.sin(Math.toRadians(super.getRotY() + 90)));
        }

    }

    // rotate left or right
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {   
        this.blockKeyUp = false;
        this.blockKeyDown = false;

        super.setRotY(super.getRotY() - Strings.SENSITIVITY);

    } else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        this.blockKeyDown = false;
        this.blockKeyUp = false;

        super.setRotY(super.getRotY() + Strings.SENSITIVITY);
    }

    // Strife left or right
    if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
        super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY())));
        super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY())));

    } else if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
        super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY() + 180)));
        super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY() + 180)));
    }



    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {

        this.blockKeyDown = false;
        this.blockKeyUp = false;

        if (this.amtJumped < Strings.JUMP_HEIGHT - super.getHeight()) {
            super.setY(super.getY() - 2.5F); // 2.5
            this.amtJumped += 2.5F;
        } else {
            this.amtJumped = 10000;
        }
    }

}

public void update() {
    this.keyEvent();

    // camera gravity
    super.setY(super.getY() + Strings.GRAVITY);

    this.updateView();

}

// no need to render the camera
public void render() {}

}

the occurrences of

super.setX(super.getX() - (float) Math.cos(Math.toRadians(super.getRotY())));
super.setZ(super.getZ() + (float) Math.sin(Math.toRadians(super.getRotY())));

are where I need to add the speed but when I do it throws of the calculation. How can I add speed without throwing it off?

Was it helpful?

Solution

You need to multiply your direction by your speed, not add as i'm guessing your doing now. try this:

super.setX(super.getX() - (float) (speed*Math.cos(Math.toRadians(super.getRotY()))));
super.setZ(super.getZ() + (float) (speed*Math.sin(Math.toRadians(super.getRotY()))));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top