- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
*问题已解决好吧,问题是在 LWJGL 中我使用 glTranslatef 和 glRotatef 制作了一个基本的 FPS 相机。它一开始的表现就像它应该的那样,但是当我移动相机时,它开始围绕相机原来所在的枢轴旋转!这是我的代码(没有导入等):
public class Main {
public static float camera_x,camera_y,camera_z,camera_rot;
public static ArrayList<Block>blocks = new ArrayList<Block>();
public Main(){
try{
Display.setDisplayMode(new DisplayMode(800,600));
Display.setTitle("Voxel");
Display.create();
}catch(LWJGLException e){
e.printStackTrace();
}
//Init
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective((float)45,800f/600f,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
generateWorld();
glTranslatef(0,15,0);
float dt,time,lastTime = 0;
Mouse.setGrabbed(true);
while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
time = Sys.getTime();
dt = (time - lastTime)/1000.0f;
lastTime = time;
render();
tick();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public void tick(){
camera_x = 0;
camera_y = 0;
camera_z = 0;
camera_rot = 0;
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
camera_z = +1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_S)){
camera_z = -1;
}
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
camera_x = +1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_D)){
camera_x = -1;
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
camera_y = -1;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
camera_y = +1;
}
while(Keyboard.next()){
if(Keyboard.isKeyDown(Keyboard.KEY_R)){
generateWorld();
}
}
//Updating all of the blocks
for(int i=0; i < blocks.size(); i++){
blocks.get(i).tick();
}
camera_rot += Mouse.getDX();
}
public void render(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(camera_rot, 0f, 1f, 0f);
glTranslatef(camera_x, camera_y, camera_z);
for(int i=0; i < blocks.size(); i++){
blocks.get(i).render();
}
}
public static void main(String[] arguments){
new Main();
}
public void generateWorld(){
blocks.clear();
int heightlevel = 0;
Random r = new Random();
for(int i=0; i < 50; i++){
for(int j=0; j < 50; j++){
if(r.nextBoolean() == false){
heightlevel -= 4;
}
else{
heightlevel += 4;
}
blocks.add(new Block((i*4)-64,-8+ heightlevel,(j*4)-64,4,4,4));
float y = -8+heightlevel;
for(int k = 0; k < 10; k++){
blocks.add(new Block((i*4)-64,y - (k*4),(j*4)-64,4,4,4));
}
}
}
}
}
还有一个 Block 类:
public class Block {
public float x,y,z,width,height,depth,shade;
public Random rand;
public Block(float xx,float yy,float zz,float ww,float hh,float dd){
x = xx;
y = yy;
z = zz;
width = ww;
height = hh;
depth = dd;
rand = new Random();
shade = (rand.nextFloat()+0.2f);
}
public void tick(){
}
public void render(){
glBegin(GL_QUADS);
glColor3f(0,shade,0);
//Front
glTexCoord2f(0,0);
glVertex3f(x,y,z);
glTexCoord2f(1,0);
glVertex3f(x+width,y,z);
glTexCoord2f(1,1);
glVertex3f(x+width,y+height,z);
glTexCoord2f(0,1);
glVertex3f(x,y+height,z);
//Back
glVertex3f(x,y,z+depth);
glVertex3f(x+width,y,z+depth);
glVertex3f(x+width,y+height,z+depth);
glVertex3f(x,y+height,z+depth);
//Left
glVertex3f(x,y,z);
glVertex3f(x,y,z+depth);
glVertex3f(x,y+height,z+depth);
glVertex3f(x,y+height,z);
//Right
glVertex3f(x+width,y,z);
glVertex3f(x+width,y,z+depth);
glVertex3f(x+width,y+height,z+depth);
glVertex3f(x+width,y+height,z);
//Top
glVertex3f(x,y,z);
glVertex3f(x+width,y,z);
glVertex3f(x+width,y,z+depth);
glVertex3f(x,y,z+depth);
//Bottom
glVertex3f(x,y+height,z);
glVertex3f(x+width,y+height,z);
glVertex3f(x+width,y+height,z+depth);
glVertex3f(x,y+height,z+depth);
glEnd();
}
}
最佳答案
那是因为您需要翻转调用。所以就像:
glRotatef(camera_rot, 0f, 1f, 0f);
glTranslatef(camera_x, camera_y, camera_z);
而不是这样:
glTranslatef(camera_x, camera_y, camera_z);
glRotatef(camera_rot, 0f, 1f, 0f);
原因是您旋转相机,然后平移相机。这给出了您所看到的效果,因为它使用 camera_rot
旋转,然后根据 camera_x、camera_y、camera_z
进行平移。
您需要更改此设置:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(camera_rot, 0f, 1f, 0f);
glTranslatef(camera_x, camera_y, camera_z);
进入此:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(camera_rot, 0f, 1f, 0f);
glTranslatef(camera_x, camera_y, camera_z);
因此,当您完成此操作后,您会发现相机的位置和旋转将被卡住。那是因为每次调用 tick()
您调用:
camera_x = 0;
camera_y = 0;
camera_z = 0;
camera_rot = 0;
它的作用是重置位置和旋转,这就是相机“卡住”的原因。
因此您需要更改它,以便值递增或递减,而不仅仅是停留在 -1、0 和 1。
关于java - LWJGL 相机绕点旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560929/
我正在编写国际象棋人工智能程序,并且在实现棋子方格表时遇到了问题。由于我只想每边有一张正方形 table ,因此我需要一个函数来翻转保存 x 轴值的一维数组。例如,这个数组: [ 2, 4, 5, 3
在应用程序中,在尝试使用触摸旋转对象时,我注意到一段时间后对象的位置发生了漂移(没有应用任何平移!!)。旋转仅围绕 z 轴,工作正常,但仅在旋转几次后才会发生漂移。 ds 将用于进行翻译(使用上下按钮
我正在尝试构建金字塔并为其设置动画。问题是在围绕 X 轴旋转它之后,我尝试围绕金字塔的高度(Y 轴)旋转它,但我每次都在移动。我基本上尝试了每个 transform-origin 选项,但它不起作用。
我需要绕其 x 轴(或 y 轴)旋转图像。我可以使用 avisynth 轻松创建这样的动画,但现在我需要使用 Python 的 moviepy 模块来实现该效果。我可以使用以下脚本轻松旋转图像,但需要
我有一个图像被分成两个相等的部分。我正在尝试在悬停时围绕 y 轴将图像的右侧部分旋转 -180°(逆时针)。 问题是有时(随机)图像旋转 180°(顺时针)而不是 -180°(逆时针)。这背后的原因可
如何使用CGAffineTransform(或其他)绕其Y轴旋转UIView?例如,我尝试过: self.image.transform = CGAffineTransform(rotationAng
我正在尝试围绕 y 轴旋转查看器。我有一个名为 tranform_eye() 的函数,它将计算 eyex、eyey 和 eyez 的下一个位置更新。 谁能帮我弄清楚如何计算 eyex、eyey 和 e
我无法解决我遇到的一个奇怪的错误,想知道是否有其他人遇到过类似的问题或可以提供解决方案。 我正在使用 javascript 访问 iOS 设备上的陀螺仪,并且对围绕 z 轴的旋转很感兴趣;我想要设备平
Qt 使用 3x3 变换矩阵进行透视变换和仿射变换。如果矩阵的最后一行等于 [0 0 1],则矩阵被认为是仿射的。由于这个原因,x 轴和 y 轴旋转矩阵是“非仿射”和透视失真结果。但还有进一步的影响。
我是一名优秀的程序员,十分优秀!