Basic Matrix Rotation

Started by
1 comment, last by Dae 17 years, 11 months ago
Hey, I'm having troubles figuring out exactly how to get the new coords after rotating around the x axis. I read what you do is identity a matrix, do the rotation (sin/cos), and then to get the new coords take your old coords and multiply that vector by that matrix. I didn't find it so hard reading materials for 3d opengl programming, but since I switched to Flash it's been harder knowing exactly what to do. Is the below even close? If you can't tell, I'd love some C++ code specific to updating the camera's coords after rotating, if anyone minds.
	//CCamera.as
	public function RotateX(angle:Number):Void
	{
		m_nXRotation += angle; //rotation from the axis plus new rotation

		m_Orient.Identity();

		m_Orient.RotateX(m_nXRotation);
		var vec:CVector = m_Orient.MultiplyVector(m_Coords);
		
		m_Coords.y = vec.y;
		m_Coords.z = vec.z;
	}

	//CMatrix.as
	public function RotateX(angle:Number):Void
	{
		angle *= DEG2RAD;
		
		var cos:Number = Math.cos(angle);
		var sin:Number = Math.sin(angle);
		
		m_Data[1][1] = cos; m_Data[1][2] = -sin;
		m_Data[2][1] = sin; m_Data[2][2] =  cos;
	}

	public function MultiplyVector(vec:CVector):CVector
	{
		var temp:CVector = new CVector(0, 0, 0);
	  
		temp.x = vec.x * m_Data[0][0] + vec.y * m_Data[0][1] + vec.z * m_Data[0][2];
		temp.y = vec.x * m_Data[1][0] + vec.y * m_Data[1][1] + vec.z * m_Data[1][2];
		temp.z = vec.x * m_Data[2][0] + vec.y * m_Data[2][1] + vec.z * m_Data[2][2];
      
		return temp;
	}
Thanks!
010001000110000101100101
Advertisement
It looks correct to me. The only things you have to worry about is making sure that m_Coords doesn't lose precision over time. You might want to keep a base camera position, and rotate that around the X axis to get the new camera position, as opposed to updating the same variable over and over. I don't know how you're initializing m_Data, but it should start out as the identity matrix, except (0,0) should be 0.
Quote:Original post by Zipster
It looks correct to me. The only things you have to worry about is making sure that m_Coords doesn't lose precision over time. You might want to keep a base camera position, and rotate that around the X axis to get the new camera position, as opposed to updating the same variable over and over. I don't know how you're initializing m_Data, but it should start out as the identity matrix, except (0,0) should be 0.


Thanks, but I'm still unsure. I'm not sure how do go about this double camera suggestion. How would I lose precision? m_Coords.z/y are set to the returned value's z/y, not += or anything. If I could get some more help I'd really appreciate it.

Yeah, m_Data starts out as the identity matrix, and I change to the identity matrix before every rotation.
010001000110000101100101

This topic is closed to new replies.

Advertisement