# Create camera movement 3D

## Recommended Posts

I'm using the OPENGL with eclipse+JOGL.
My goal is to create movement of the camera and the player.
I create main class, which create some box in 3D and hold
an object of PlayerAxis.
I create PlayerAxis class which hold the axis of the player.
If we want to move the camera, then in the main class I call to
the func "cameraMove"(from PlayerAxis) and it update the player axis.
That's work good.
The problem start if I move the camera on 2 axis,
for example if I move with the camera right(that's on the y axis)
and then down(on the x axis) -
in some point the move front is not to the front anymore..
In order to move to the front, I do
player.playerMoving(0, 0, 1);
And I learn that in order to keep the front move,
I need to convert (0, 0, 1) to the player axis, and then add this.
I think I dont do the convert right..
I will be glad for help!

Here is part of my PlayerAxis class:

	//player coordinate
float x[] = new float[3];
float y[] = new float[3];
float z[] = new float[3];

public PlayerAxis(float move_step, float angle_move) {
x[0] = 1;
y[1] = 1;
z[2] = -1;
step = move_step;
angle = angle_move;
setTransMatrix();
}

public void cameraMoving(float angle_step, String axis) {

float[] new_x = x;
float[] new_y = y;
float[] new_z = z;
float alfa = angle_step * angle;

switch(axis) {
case "x":
new_z = addVectors(multScalar(z, COS(alfa)), multScalar(y, SIN(alfa)));
new_y = subVectors(multScalar(y, COS(alfa)), multScalar(z, SIN(alfa)));
break;
case "y":
new_x = addVectors(multScalar(x, COS(alfa)), multScalar(z, SIN(alfa)));
new_z = subVectors(multScalar(z, COS(alfa)), multScalar(x, SIN(alfa)));
break;
case "z":
new_x = addVectors(multScalar(x, COS(alfa)), multScalar(y, SIN(alfa)));
new_y = subVectors(multScalar(y, COS(alfa)), multScalar(x, SIN(alfa)));
}

x = new_x;
y = new_y;
z = new_z;
normalization();

}

public void playerMoving(float x_move, float y_move, float z_move) {
float[] move = new float[3];
move[0] = x_move;
move[1] = y_move;
move[2] = z_move;
setTransMatrix();
float[] trans_move = transVector(move);
position[0] = position[0] + step*trans_move[0];
position[1] = position[1] + step*trans_move[1];
position[2] = position[2] + step*trans_move[2];
}

public void setTransMatrix() {
for (int i = 0; i < 3; i++) {
coordiTrans[0][i] = x[i];
coordiTrans[1][i] = y[i];
coordiTrans[2][i] = z[i];
}
}

public float[] transVector(float[] v) {
return multiplyMatrixInVector(coordiTrans, v);
}

and in the main class i have this:

public void keyPressed(KeyEvent e) {

if (e.getKeyCode()== KeyEvent.VK_ESCAPE) {
﻿  	System.exit(0);

//player move
} else if (e.getKeyCode()== KeyEvent.VK_W) {
//front
﻿	//moveAmount[2] += -0.1f;
player.playerMoving(0, 0, 1);
} else if (e.getKeyCode()== KeyEvent.VK_S) {
//back
//moveAmount[2] += 0.1f;
player.playerMoving(0, 0, -1);
} else if (e.getKeyCode()== KeyEvent.VK_A) {
//left
//moveAmount[0] += -0.1f;
player.playerMoving(-1, 0, 0);
} else if (e.getKeyCode()== KeyEvent.VK_D) {
//right
//moveAmount[0] += 0.1f;
player.playerMoving(1, 0, 0);
} else if (e.getKeyCode()== KeyEvent.VK_E) {
//moveAmount[0] += 0.1f;
player.playerMoving(0, 1, 0);
} else if (e.getKeyCode()== KeyEvent.VK_Q) {
//moveAmount[0] += 0.1f;
player.playerMoving(0, -1, 0);

//camera move
} else if (e.getKeyCode()== KeyEvent.VK_I) {
//up
player.cameraMoving(1, "x");
} else if (e.getKeyCode()== KeyEvent.VK_K) {
//down
player.cameraMoving(-1, "x");
} else if (e.getKeyCode()== KeyEvent.VK_L) {
//right
player.cameraMoving(-1, "y");
} else if (e.getKeyCode()== KeyEvent.VK_J) {
//left
player.cameraMoving(1, "y");
} else if (e.getKeyCode()== KeyEvent.VK_O) {
//right round
player.cameraMoving(-1, "z");
} else if (e.getKeyCode()== KeyEvent.VK_U) {
//left round
player.cameraMoving(1, "z");
}
}

Edited by Inbar_xz

##### Share on other sites

Please don't cross post the same question to multiple forums.  I'll close this one and leave the one in Beginners.

##### Share on other sites
This topic is now closed to further replies.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×