Hi,
ich habe ein witziges, interessantes und nerviges Problem.
Meine Kamera, die ich jetzt mal endlich in meine Bibliothek gepackt habe, habe ich verbessert und kleiner vom Code her gemacht, schön und gut, aber mein Problem ist, wenn ich nach links gucken will und meine rotation (yaw) auf 0 ist das ich dann einfach nicht nach links gucken kann, habe ich aber mich rechts gedreht, dann kann ich mich nach links drehen, bis zu dem Punkt 0.
Sonst funktioniert alles. Ich wollte mir eine Funktion einbauen, die es mir ermöglicht wenn ich nach unten gucke das ich meine Kamera nicht um 360° drehen kann, sondern das ich noch auf den Boden schau, das gleiche nach oben, das funktioniert aber nur das nach links gucken nicht.
Der Code:
Camera:
Main:
Das Programm, damit ihr das Problem selbst testen könnt: (.exe Datei)
3DTests.exe
MfG:
Florilu
ich habe ein witziges, interessantes und nerviges Problem.
Meine Kamera, die ich jetzt mal endlich in meine Bibliothek gepackt habe, habe ich verbessert und kleiner vom Code her gemacht, schön und gut, aber mein Problem ist, wenn ich nach links gucken will und meine rotation (yaw) auf 0 ist das ich dann einfach nicht nach links gucken kann, habe ich aber mich rechts gedreht, dann kann ich mich nach links drehen, bis zu dem Punkt 0.
Sonst funktioniert alles. Ich wollte mir eine Funktion einbauen, die es mir ermöglicht wenn ich nach unten gucke das ich meine Kamera nicht um 360° drehen kann, sondern das ich noch auf den Boden schau, das gleiche nach oben, das funktioniert aber nur das nach links gucken nicht.
Der Code:
Camera:
Java:
package Methods.Cameras;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
public class FPSCamera
{
//3d vector to store the camera's position in
public Vector3f position = null;
//the rotation around the Y axis of the camera
public float yaw = 0.0f;
//the rotation around the X axis of the camera
public float pitch = 0.0f;
public static int mouseSpeed = 2;
public static int walkingSpeed = 10;
public static final int maxLookDown = -85;
public static final int maxLookUp = 85;
//Constructor that takes the starting x, y, z location of the camera
public FPSCamera(float x, float y, float z)
{
//instantiate position Vector3f to the x y z params.
position = new Vector3f(x, y, z);
}
public FPSCamera() { // This is made so the line FPCameraController app = new FPCameraController(); will work
// TODO Auto-generated constructor stub
}
//increment the camera's current yaw rotation
public void yaw(float amount)
{
//increment the yaw by the amount param
yaw += amount;
}
//increment the camera's current yaw rotation
public void pitch(float amount)
{
//increment the pitch by the amount param
pitch += amount;
}
//moves the camera forward relitive to its current rotation (yaw)
public void walkForward(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
position.z += distance * (float)Math.cos(Math.toRadians(yaw));
}
//moves the camera backward relitive to its current rotation (yaw)
public void walkBackwards(float distance)
{
position.x += distance * (float)Math.sin(Math.toRadians(yaw));
position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
}
//strafes the camera left relitive to its current rotation (yaw)
public void strafeLeft(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
}
//strafes the camera right relitive to its current rotation (yaw)
public void strafeRight(float distance)
{
position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
}
public void moveUp(float distance){
position.y -= distance;
}
public void moveDown(float distance){
position.y += distance;
}
//translates and rotate the matrix so that it looks through the camera
//this dose basic what gluLookAt() does
public void lookThrough()
{
//roatate the pitch around the X axis
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
if (Mouse.isGrabbed()) {
float mouseDX = Mouse.getDX() * mouseSpeed * 0.16f;
float mouseDY = Mouse.getDY() * mouseSpeed * 0.16f;
if (yaw + mouseDX >= 360) {
yaw = yaw + mouseDX - 360;
} else if (yaw + mouseDX < 0) {
yaw = 360 - yaw + mouseDX;
} else {
yaw += mouseDX;
}
if (pitch - mouseDY >= maxLookDown && pitch - mouseDY <= maxLookUp) {
pitch += -mouseDY;
} else if (pitch - mouseDY < maxLookDown) {
pitch = maxLookDown;
} else if (pitch - mouseDY > maxLookUp) {
pitch = maxLookUp;
}
}
//translate to the position vector's location
GL11.glTranslatef(position.x, position.y, position.z);
}
}
Main:
Java:
import java.awt.event.KeyEvent;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import Methods.Methods;
import Methods.Cameras.FPSCamera;
import Methods.Cubes.DefaultCube;
public class Main
{
private static boolean gameRunning=true;
private static int targetWidth = 800;
private static int targetHeight = 600;
public static boolean printFPS = false;
private static int fps;
private static int lastFPS;
private static int lastFrame;
private void init(){
//load textures here and other things
}
private float xrot=0.1f;
private float yrot=0.1f;
private float zrot=0.1f;
/** The texture that’s been loaded */
private static void initDisplay(boolean fullscreen){
DisplayMode chosenMode = null;
try {
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i=0;i<modes.length;i++) {
if ((modes[i].getWidth() == targetWidth) && (modes[i].getHeight() == targetHeight)) {
chosenMode = modes[i];
break;
}
}
} catch (LWJGLException e) {
Sys.alert("Error", "Unable to determine display modes.");
System.exit(0);
}
// at this point if we have no mode there was no appropriate, let the user know
// and give up
if (chosenMode == null) {
Sys.alert("Error", "Unable to find appropriate display mode.");
System.exit(0);
}
try {
Display.setDisplayMode(chosenMode);
Display.setFullscreen(fullscreen);
Display.setTitle("3D Game Engine");
Display.create();
}
catch (LWJGLException e) {
Sys.alert("Error","Unable to create display.");
System.exit(0);
}
}
private static boolean initGL(){
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// Calculate the aspect ratio of the window
GLU.gluPerspective(45.0f,((float)targetWidth)/((float)targetHeight),0.1f,100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
return true;
}
public boolean isKeyPressed(int keyCode) {
// apparently, someone at decided not to use standard
// keycode, so we have to map them over:
switch(keyCode) {
case KeyEvent.VK_SHIFT:
keyCode = Keyboard.KEY_LSHIFT;
break;
}
return org.lwjgl.input.Keyboard.isKeyDown(keyCode);
}
private void run(){
FPSCamera camera = new FPSCamera(0, 0, 0);
float dx = 0.0f;
float dy = 0.0f;
float dt = 0.0f; //length of frame
float lastTime = 0.0f; // when the last frame was
float time = 0.0f;
float mouseSensitivity = 0.15f;
float movementSpeed = 10.0f; //move 10 units per second
//hide the mouse
Mouse.setGrabbed(true);
while(gameRunning){
update();
render();
Display.update();
Display.setTitle("X: "+camera.position.x+" Y: "+camera.position.y+" Z: "+camera.position.z);
String jpg = "jpg";
String png = "png";
String txt = "txt";
time = Sys.getTime();
//here is your movement speed, which can be changed to anything
dt = 0.0005f;
lastTime = time;
//distance in mouse movement from the last getDX() call.
dx = Mouse.getDX();
//distance in mouse movement from the last getDY() call.
dy = Mouse.getDY();
//control camera yaw from x movement from the mouse
camera.yaw(dx * mouseSensitivity);
//control camera pitch from y movement from the mouse
camera.pitch(-dy * mouseSensitivity);
//when passing in the distrance to move
//we times the movementSpeed with dt this is a time scale
//so if its a slow frame u move more then a fast frame
//so on a slow computer you move just as fast as on a fast computer
//OVER HERE! What do I do to make the boolean canWalk actually work the right way?
//If Statements for Camera Controls
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
camera.walkForward(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_S)){
camera.walkBackwards(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_A)){
camera.strafeLeft(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_D)){
camera.strafeRight(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
camera.moveUp(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
camera.moveDown(movementSpeed * dt);
}if(Keyboard.isKeyDown(Keyboard.KEY_P)){
if(Mouse.isGrabbed()){
Mouse.setGrabbed(false);
}else{
Mouse.setGrabbed(true);
}
}if(Keyboard.isKeyDown(Keyboard.KEY_F1)){
Methods.screenShot();
}if(Keyboard.isKeyDown(Keyboard.KEY_F)){
if(printFPS == true){
printFPS = false;
}else{
printFPS = true;
}
}
//set the modelview matrix back to the identity
GL11.glLoadIdentity();
//updateFPS();
//look through the camera before you draw anything
camera.lookThrough();
//you would draw your scene here.
//draw the buffer to the screen
//Display.update();
//}
// finally check if the user has requested that the display be
// shutdown
if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
gameRunning = false;
Display.destroy();
System.exit(0);
}
}
}
private void update(){
xrot+=0.1f;
yrot+=0.1f;
zrot+=0.1f;
}
private void render(){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
//GL11.glLoadIdentity();
//Drawing Method
DefaultCube.draw();
}
public static void main(String[] args)
{
Main main = new Main();
initDisplay(false);
initGL();
main.init();
main.run();
}
}
Das Programm, damit ihr das Problem selbst testen könnt: (.exe Datei)
3DTests.exe
MfG:
Florilu
Zuletzt bearbeitet: