Java 我的轮换';他们不工作了
我正在研究我的“游戏引擎”,在完成玩家课程后,我意识到相机的运动不起作用。。。我做了一系列try/catch if/else语句,并将其缩小为Mouse.getDX无法正常工作的问题,或者glRotate无法正常工作 这是我的游戏课:Java 我的轮换';他们不工作了,java,opengl,3d,lwjgl,Java,Opengl,3d,Lwjgl,我正在研究我的“游戏引擎”,在完成玩家课程后,我意识到相机的运动不起作用。。。我做了一系列try/catch if/else语句,并将其缩小为Mouse.getDX无法正常工作的问题,或者glRotate无法正常工作 这是我的游戏课: package com.matisse.engine; import static org.lwjgl.opengl.GL11.GL_POINTS; import static org.lwjgl.opengl.GL11.glBegin; import stat
package com.matisse.engine;
import static org.lwjgl.opengl.GL11.GL_POINTS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glCallList;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex3f;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import assets.TestBlock;
import com.matisse.world.Chunk;
import com.matisse.world.Level;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Game {
public boolean[] keys;
public XStream xstream;
public Level world;
public File file;
public Camera camera;
public Game() throws LWJGLException {
run();
}
public void run() {
try {
Keyboard.create();
keys = new boolean[256];
xstream = new XStream(new DomDriver());
Level first_world = new Level(0, 0);
Chunk first_chunk = new Chunk();
TestBlock floor = new TestBlock(-10, -2, -10, 10, -1, 10);
first_chunk.voxels.add(floor);
first_world.chunks.add(first_chunk);
first_world.genLists();
String xml = xstream.toXML(first_world);
saveFile("world", xml);
world = (Level) xstream.fromXML(readFile("res/maps/world.xml"));
camera = new Camera(this, world.startx, world.startz);
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public void update() {
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
Engine.state = State.MENU;
}
mapKeys();
camera.update();
camera.translateCamera();
}
public void draw3D() {
for (Chunk i : world.chunks) {
i.render();
glCallList(i.displayListHandle);
}
}
public void draw2D() {
glBegin(GL_POINTS);
glColor3f(1, 0, 0);
glVertex3f(0, 0, 10);
glEnd();
}
public void mapKeys() {
for (int i = 0; i < keys.length; i++) {
keys[i] = Keyboard.isKeyDown(i);
}
}
public void saveFile(String mapname, String xml) {
FileOutputStream fop = null;
String content = xml;
try {
file = new File("res/maps/" + mapname + ".xml");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String readFile(String filename) {
StringBuffer result = new StringBuffer();
// The name of the file to open
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(filename);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
// Always close files.
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + filename + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + filename + "'");
// Or we could just do this:
// ex.printStackTrace();
}
String product = result.toString();
return product;
}
}
package com.matisse.engine;
导入静态org.lwjgl.opengl.GL11.GL_点;
导入静态org.lwjgl.opengl.GL11.glBegin;
导入静态org.lwjgl.opengl.GL11.glCallList;
导入静态org.lwjgl.opengl.GL11.glColor3f;
导入静态org.lwjgl.opengl.GL11.glEnd;
导入静态org.lwjgl.opengl.GL11.glVertex3f;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.input.Keyboard;
导入assets.TestBlock;
导入com.matisse.world.Chunk;
导入com.matisse.world.Level;
导入com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.io.xml.DomDriver;
公开课游戏{
公共布尔[]键;
公共XStream XStream;
公共层面的世界;
公共文件;
公共摄像机;
public Game()抛出了LWJGLException{
run();
}
公开募捐{
试一试{
create();
键=新布尔值[256];
xstream=newxstream(newdomdriver());
级别第一世界=新级别(0,0);
Chunk first_Chunk=新Chunk();
测试块地板=新测试块(-10,-2,-10,10,-1,10);
第一个块。体素。添加(地板);
first\u world.chunks.add(first\u chunk);
first_world.genLists();
字符串xml=xstream.toXML(第一世界);
保存文件(“世界”,xml);
world=(Level)xstream.fromXML(readFile(“res/maps/world.xml”);
摄像头=新摄像头(此,world.startx,world.startz);
}捕获(LWJGLEXE){
e、 printStackTrace();
}
}
公共无效更新(){
if(键盘.isKeyDown(键盘.KEY_退出)){
Engine.state=state.MENU;
}
mapKeys();
camera.update();
camera.translateCamera();
}
公共空间{
for(Chunk i:world.Chunk){
i、 render();
glCallList(即displayListHandle);
}
}
公共空间{
glBegin(总分);
gl3f(1,0,0);
glVertex3f(0,0,10);
格伦德();
}
公共void映射键(){
for(int i=0;i
这是我的Camera类:(注意:这是我尝试使用的第一个“引擎”之一,几乎没有外部引用,因此代码的其余部分有点粗糙)
package com.matisse.engine;
导入静态org.lwjgl.opengl.GL11.glRotatef;
导入静态org.lwjgl.opengl.GL11.glTranslatef;
导入org.lwjgl.input.Keyboard;
导入org.lwjgl.input.Mouse;
导入org.lwjgl.util.vector.Vector3f;
公共级摄像机{
静态浮动速度=0.35f;
向量3f向量=新向量3f(7,1,7);
向量3f旋转=新向量3f(0,1,0);
Vector3f previous=新Vector3f();
布尔值moveForward=false,moveBackward=false,strafeLeft=false,
Straferlight=假;
游戏世界;
公共摄像机(游戏应用程序、float startx、float starty){
世界=应用程序;
向量x=startx;
向量y=星形;
}
公共无效translateCamera(){
glRotatef(rotation.x,1,0,0);
glRotatef(rotation.y,0,1,0);
glRotatef(旋转z,0,0,1);
glTranslatef(-vector.x,-vector.y-1.4f,-vector.z);
}
公共无效更新(){
if(Engine.state==state.GAME){
鼠标。setgrapped(true);
}否则{
Mouse.setgrapped(false);
}
updatePreviousVector();
更新情感();
输入();
}
公共无效输入(){
if(world.keys[Keyboard.KEY\u W]){
向前移动=真;
}否则{
向前移动=错误;
}
if(world.keys[键盘.键]){
向后移动=真;
}否则{
向后移动=错误;
}
if(world.keys[Keyboard.KEY\u A]){
Strafeflet=真;
}否则{
Strafeflet=假;
}
if(world.keys[Keyboard.KEY\u D]){
斯特拉弗赖特=真;
}否则{
斯特拉
package com.matisse.engine;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslatef;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector3f;
public class Camera {
static float speed = 0.35f;
Vector3f vector = new Vector3f(7, 1, 7);
Vector3f rotation = new Vector3f(0, 1, 0);
Vector3f previous = new Vector3f();
boolean moveForward = false, moveBackward = false, strafeLeft = false,
strafeRight = false;
Game world;
public Camera(Game app, float startx, float starty) {
world = app;
vector.x = startx;
vector.y = starty;
}
public void translateCamera() {
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(-vector.x, -vector.y - 1.4f, -vector.z);
}
public void update() {
if (Engine.state == State.GAME) {
Mouse.setGrabbed(true);
} else {
Mouse.setGrabbed(false);
}
updatePreviousVector();
updateMotion();
input();
}
public void input() {
if (world.keys[Keyboard.KEY_W]) {
moveForward = true;
} else {
moveForward = false;
}
if (world.keys[Keyboard.KEY_S]) {
moveBackward = true;
} else {
moveBackward = false;
}
if (world.keys[Keyboard.KEY_A]) {
strafeLeft = true;
} else {
strafeLeft = false;
}
if (world.keys[Keyboard.KEY_D]) {
strafeRight = true;
} else {
strafeRight = false;
}
try {
float mouseDX = Mouse.getDX() * 0.8f * 0.16f;
float mouseDY = Mouse.getDY() * 0.8f * 0.16f;
System.out.println(Mouse.getDX());
if (rotation.y + mouseDX >= 360) {
rotation.y = rotation.y + mouseDX - 360;
} else if (rotation.y + mouseDX < 0) {
rotation.y = 360 - rotation.y + mouseDX;
} else {
rotation.y += mouseDX;
System.out.println(mouseDX);
}
if (rotation.x - mouseDY >= -89 && rotation.x - mouseDY <= 89) {
rotation.x += -mouseDY;
} else if (rotation.x - mouseDY < -89) {
rotation.x = -89;
} else if (rotation.x - mouseDY > 89) {
rotation.x = 89;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void updatePreviousVector() {
previous.x = vector.x;
previous.y = vector.y;
previous.z = vector.z;
}
public void updateMotion() {
if (moveForward) {
vector.x += Math.sin(rotation.y * Math.PI / 180) * speed;
vector.z += -Math.cos(rotation.y * Math.PI / 180) * speed;
}
if (moveBackward) {
vector.x -= Math.sin(rotation.y * Math.PI / 180) * speed;
vector.z -= -Math.cos(rotation.y * Math.PI / 180) * speed;
}
if (strafeLeft) {
vector.x += Math.sin((rotation.y - 90) * Math.PI / 180) * speed;
vector.z += -Math.cos((rotation.y - 90) * Math.PI / 180) * speed;
}
if (strafeRight) {
vector.x += Math.sin((rotation.y + 90) * Math.PI / 180) * speed;
vector.z += -Math.cos((rotation.y + 90) * Math.PI / 180) * speed;
}
}
}