Problem with rotation matrix

Started by jacekcichy, December 06, 2009, 22:39:07

Previous topic - Next topic

jacekcichy

Hi again!  ;D

Last time I was trying to rotate my ship by local coordinate system. So after rotation I was reading each rotation matrix and multiply (axis)vectors by them and results put to glRotatef function. Program works but rotations not... :( ship rotations is very strange... I don't know what is wrong in my idea so please help me  :)

Init code:
static Vector4f zaxis = new Vector4f(0,0,1,1);
         static Vector4f yaxis = new Vector4f(0,1,0,1);
         static FloatBuffer macierzz = BufferUtils.createFloatBuffer(64);
         static FloatBuffer macierzy = BufferUtils.createFloatBuffer(64);
        
	//nowe wektory
	static Vector4f zaxisd = new Vector4f();
	static Vector4f yaxisd = new Vector4f();
	//macierze
	static Matrix4f zm = new Matrix4f();
	static Matrix4f ym = new Matrix4f();

Loop code:
                         GL11.glPushMatrix();			
			GL11.glRotatef(shipzrot,zaxis.getX(),zaxis.getY(),zaxis.getZ());
			macierzz.clear();			
			GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, macierzz);
			macierzz.rewind();			
			zm.load(macierzz);			
			zm.transpose();
						
			Matrix4f.transform(zm, zaxis, zaxisd);
			Matrix4f.transform(zm, yaxis, yaxisd);
			
			GL11.glRotatef(shipyrot,yaxisd.getX(),yaxisd.getY(),yaxisd.getZ());
			
			macierzy.clear();
			GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, macierzy);
			macierzy.rewind();			
			ym.load(macierzy);
			ym.transpose();
			
			Matrix4f.transform(ym, zaxisd, zaxis);		
			Matrix4f.transform(ym, yaxisd, yaxis);	
			
			GL11.glCallList(ship.getWskaznik());
			GL11.glPopMatrix();

dronus


GL's  MODELVIEW_MATRIX is relative to the viewer, so it changes if the viewpoint changes, which is usually not useful! If you read it back, you got your ships transform relative to the camera, but not to your "space".


It would be quite cleaner if you manage your ship's transforms inside a Matrix4f only and apply it as whole with GL11.glLoadMatrix. Somewhat like

Matrix4f transformMatrix=new Matrix4f(); 

...

GL11.glPushMatrix();

---now update transformMatrix with your ship controls

transformMatrix.store(matrixBuffer);
matrixBuffer.flip();
GL11.glMultMatrix(matrixBuffer);

---draw the ship

GL11.glPopMatrix();

jacekcichy

Thanks  :)
I resolved it about two days ago. Here is code without using Matrix4f:

private static void run(){
		int licz=0;		
		//petla gry
		while (gameRunning) {
		         long a = System.currentTimeMillis();
		         // wywoluj funkcje gamelogic
		         GameLogic();
		       
		    
			// czyscimy obraz i Depth Buffer
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          
					
			GL11.glMatrixMode(GL11.GL_MODELVIEW);			
			GL11.glLoadIdentity();
			
			cam.trans1(RotZ, RotY, PozY);
			cam.update();
				                     	
						
			if (licz != 0){
				GL11.glLoadMatrix(m);
			} 
					
			GL11.glPushMatrix();			
			
			GL11.glRotatef(shipyrot, 0, 1, 0);		    
			GL11.glRotatef(shipzrot, 0, 0, 1);
		           GL11.glRotatef(shipxrot, 1, 0, 0);
		           GL11.glTranslatef(0, 0, shipx);
		    
		           GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, m);
		    
			GL11.glCallList(ship.getWskaznik());			
			GL11.glPopMatrix();
						
			licz += 1;			
                                  // now tell the screen to update
			Display.update();
					 
		           // sprawdzamy czy uzytkownik niechce wyjsc, jezeli tak to wychodzimy z petli 
		           if (Display.isCloseRequested()) {
		    	gameRunning = false;
		           Display.destroy();
		           System.exit(0);
		    }
		    long b = System.currentTimeMillis();
		    time = (int) (b-a);		    
		}		    		    
	}