Original Post
🔒 Locked
Matrices!!
Advertisement
i hope this helps.
http://www.gamedev.net/hosted/3dgraphics/tutorial/index.html
that is a 3d tutorial and it''ll show you how to work with matrices.
i recommend you get a book on it and start learning. a uni math text on it should be fine. you''ll likely have to know calculus, however.
http://www.gamedev.net/hosted/3dgraphics/tutorial/index.html
that is a 3d tutorial and it''ll show you how to work with matrices.
i recommend you get a book on it and start learning. a uni math text on it should be fine. you''ll likely have to know calculus, however.
There isn''t much to know about Matrices, atleast not enough to buy a book about it, you can probably write the basics of matrix calculations on 10 pages, heck, on school i have it explained in 4 pages, however i will provide you with a 2 classes coded in JAVA, you can probably rewrite it in another language if you want to.
It is part of a project for school, on which i am working, hope you find it usefull.
the code below is slightly skewed. sorry about that.
-----------------------------------------------------------------
//////////////////////////////////////////////////////////////
// class StackedMatrixHandler //
// helper class to combine and stack multiple matrices //
//////////////////////////////////////////////////////////////
package engine3D;
public class StackedMatrix4DHandler
{
private Matrix4D[] stackedMatrix;
private int currentMatrix;
// baseMatrices for translation, scale and rotation, used for multiplication with other matrices
private Matrix4D translateMatrix;
private Matrix4D scaleMatrix;
private Matrix4D rotateMatrix;
private float[] sinTable;
private float[] cosTable;
public StackedMatrix4DHandler()
{
stackedMatrix = new Matrix4D[32];
clearMatrix4DStack();
translateMatrix = new Matrix4D();
scaleMatrix = new Matrix4D();
rotateMatrix = new Matrix4D();
sinTable = new float[360];
cosTable = new float[360];
for (int i =0; i < 360; i++)
{
sinTable = ((float)Math.sin((Math.PI/180)*i));
cosTable[i] = ((float)Math.cos((Math.PI/180)*i));
}
}
public void pushMatrix4D(Matrix4D matrix) throws MatrixException
{
if (currentMatrix == 31) throw new MatrixException("Matrix stack overloaded");
stackedMatrix[currentMatrix] = (Matrix4D)matrix.clone();
currentMatrix++;
stackedMatrix[currentMatrix] = matrix;
}
public Matrix4D popMatrix4D() throws MatrixException
{
if (currentMatrix == 0) throw new MatrixException("Matrix stack depleted");
stackedMatrix[currentMatrix] = null;
currentMatrix--;
return stackedMatrix[currentMatrix];
}
public Matrix4D getCurrentMatrix4D()
{
if (stackedMatrix[currentMatrix] == null) System.out.println("niks op de huidige matrixstack plek aanwezig");
return stackedMatrix[currentMatrix];
}
public Matrix4D clearMatrix4DStack()
{
currentMatrix = 0;
stackedMatrix[currentMatrix] = new Matrix4D();
return stackedMatrix[currentMatrix];
}
public Matrix4D translate(float x, float y, float z)
{
translateMatrix.matrix[0][3] = x;
translateMatrix.matrix[1][3] = y;
translateMatrix.matrix[2][3] = z;
stackedMatrix[currentMatrix].multMatrix(translateMatrix);
return stackedMatrix[currentMatrix];
}
public Matrix4D scale(float x, float y, float z)
{
scaleMatrix.matrix[0][0] = x;
scaleMatrix.matrix[1][1] = y;
scaleMatrix.matrix[2][2] = z;
stackedMatrix[currentMatrix].multMatrix(scaleMatrix);
return stackedMatrix[currentMatrix];
}
// rotate, have to review this method for correctness
public Matrix4D rotate(float angle, float x, float y, float z)
{
int iAngle = ((int)angle) % 360;
// float sin_r = ((float)Math.sin((Math.PI/180)*angle));
// float cos_r = ((float)Math.cos((Math.PI/180)*angle));;
float sin_r = sinTable[iAngle];
float cos_r = cosTable[iAngle];
float i = 1.0f - cos_r;
float x_x_i = x * x * i;
float y_y_i = y * y * i;
float z_z_i = z * z * i;
float x_y_i = x * y * i;
float x_z_i = x * z * i;
float y_z_i = y * z * i;
float x_sin_r = x * sin_r;
float y_sin_r = y * sin_r;
float z_sin_r = z * sin_r;
Matrix4D tmpMatrix = new Matrix4D();
tmpMatrix.matrix[0][0] = x_x_i + cos_r;
tmpMatrix.matrix[1][0] = x_y_i - z_sin_r;
tmpMatrix.matrix[2][0] = x_z_i + y_sin_r;
tmpMatrix.matrix[0][1] = x_y_i + z_sin_r;
tmpMatrix.matrix[1][1] = y_y_i + cos_r;
tmpMatrix.matrix[2][1] = y_z_i - x_sin_r;
tmpMatrix.matrix[0][2] = x_z_i - y_sin_r;
tmpMatrix.matrix[1][2] = y_z_i + x_sin_r;
tmpMatrix.matrix[2][2] = z_z_i + cos_r;
tmpMatrix.matrix[3][3] = 1;
stackedMatrix[currentMatrix].multMatrix(tmpMatrix);
return stackedMatrix[currentMatrix];
}
}
-----------------------------------------------------------------
package engine3D;
public class Matrix4D implements Cloneable
{
float[][] matrix;
public Matrix4D()
{
matrix = new float[4][4];
clearMatrix();
}
void clearMatrix()
{
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
matrix[x][y]= (x==y)?1:0;
}
public Object clone()
{
Matrix4D clone = new Matrix4D();
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
clone.matrix[x][y] = matrix[x][y];
return clone;
}
public void multMatrix(Matrix4D multiplier)
{
float[][] bufMatrix = new float[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
bufMatrix[i][j] = matrix[i][0]*multiplier.matrix[0][j]+matrix[i][1]*multiplier.matrix[1][j]+
matrix[i][2]*multiplier.matrix[2][j]+matrix[i][3]*multiplier.matrix[3][j];
matrix = bufMatrix;
}
public void multVector(Vertex3D multiplier) // calculate new coordinates for the Vertex (same data as Vector (x,y,z)) with this Matrix
{
float tempX = multiplier.x;
float tempY = multiplier.y;
float tempZ = multiplier.z;
multiplier.x = matrix[0][0] * tempX + matrix[0][1] * tempY + matrix[0][2] * tempZ + matrix[0][3];
multiplier.y = matrix[1][0] * tempX + matrix[1][1] * tempY + matrix[1][2] * tempZ + matrix[1][3];
multiplier.z = matrix[2][0] * tempX + matrix[2][1] * tempY + matrix[2][2] * tempZ + matrix[2][3];
}
public void printMatrix()
{
for (int i = 0; i < 4; i++)
System.out.println(matrix[0][i] + " " + matrix[1][i] + " " + matrix[2][i] + " " + matrix[3][i] + " ");
}
}
It is part of a project for school, on which i am working, hope you find it usefull.
the code below is slightly skewed. sorry about that.
-----------------------------------------------------------------
//////////////////////////////////////////////////////////////
// class StackedMatrixHandler //
// helper class to combine and stack multiple matrices //
//////////////////////////////////////////////////////////////
package engine3D;
public class StackedMatrix4DHandler
{
private Matrix4D[] stackedMatrix;
private int currentMatrix;
// baseMatrices for translation, scale and rotation, used for multiplication with other matrices
private Matrix4D translateMatrix;
private Matrix4D scaleMatrix;
private Matrix4D rotateMatrix;
private float[] sinTable;
private float[] cosTable;
public StackedMatrix4DHandler()
{
stackedMatrix = new Matrix4D[32];
clearMatrix4DStack();
translateMatrix = new Matrix4D();
scaleMatrix = new Matrix4D();
rotateMatrix = new Matrix4D();
sinTable = new float[360];
cosTable = new float[360];
for (int i =0; i < 360; i++)
{
sinTable = ((float)Math.sin((Math.PI/180)*i));
cosTable[i] = ((float)Math.cos((Math.PI/180)*i));
}
}
public void pushMatrix4D(Matrix4D matrix) throws MatrixException
{
if (currentMatrix == 31) throw new MatrixException("Matrix stack overloaded");
stackedMatrix[currentMatrix] = (Matrix4D)matrix.clone();
currentMatrix++;
stackedMatrix[currentMatrix] = matrix;
}
public Matrix4D popMatrix4D() throws MatrixException
{
if (currentMatrix == 0) throw new MatrixException("Matrix stack depleted");
stackedMatrix[currentMatrix] = null;
currentMatrix--;
return stackedMatrix[currentMatrix];
}
public Matrix4D getCurrentMatrix4D()
{
if (stackedMatrix[currentMatrix] == null) System.out.println("niks op de huidige matrixstack plek aanwezig");
return stackedMatrix[currentMatrix];
}
public Matrix4D clearMatrix4DStack()
{
currentMatrix = 0;
stackedMatrix[currentMatrix] = new Matrix4D();
return stackedMatrix[currentMatrix];
}
public Matrix4D translate(float x, float y, float z)
{
translateMatrix.matrix[0][3] = x;
translateMatrix.matrix[1][3] = y;
translateMatrix.matrix[2][3] = z;
stackedMatrix[currentMatrix].multMatrix(translateMatrix);
return stackedMatrix[currentMatrix];
}
public Matrix4D scale(float x, float y, float z)
{
scaleMatrix.matrix[0][0] = x;
scaleMatrix.matrix[1][1] = y;
scaleMatrix.matrix[2][2] = z;
stackedMatrix[currentMatrix].multMatrix(scaleMatrix);
return stackedMatrix[currentMatrix];
}
// rotate, have to review this method for correctness
public Matrix4D rotate(float angle, float x, float y, float z)
{
int iAngle = ((int)angle) % 360;
// float sin_r = ((float)Math.sin((Math.PI/180)*angle));
// float cos_r = ((float)Math.cos((Math.PI/180)*angle));;
float sin_r = sinTable[iAngle];
float cos_r = cosTable[iAngle];
float i = 1.0f - cos_r;
float x_x_i = x * x * i;
float y_y_i = y * y * i;
float z_z_i = z * z * i;
float x_y_i = x * y * i;
float x_z_i = x * z * i;
float y_z_i = y * z * i;
float x_sin_r = x * sin_r;
float y_sin_r = y * sin_r;
float z_sin_r = z * sin_r;
Matrix4D tmpMatrix = new Matrix4D();
tmpMatrix.matrix[0][0] = x_x_i + cos_r;
tmpMatrix.matrix[1][0] = x_y_i - z_sin_r;
tmpMatrix.matrix[2][0] = x_z_i + y_sin_r;
tmpMatrix.matrix[0][1] = x_y_i + z_sin_r;
tmpMatrix.matrix[1][1] = y_y_i + cos_r;
tmpMatrix.matrix[2][1] = y_z_i - x_sin_r;
tmpMatrix.matrix[0][2] = x_z_i - y_sin_r;
tmpMatrix.matrix[1][2] = y_z_i + x_sin_r;
tmpMatrix.matrix[2][2] = z_z_i + cos_r;
tmpMatrix.matrix[3][3] = 1;
stackedMatrix[currentMatrix].multMatrix(tmpMatrix);
return stackedMatrix[currentMatrix];
}
}
-----------------------------------------------------------------
package engine3D;
public class Matrix4D implements Cloneable
{
float[][] matrix;
public Matrix4D()
{
matrix = new float[4][4];
clearMatrix();
}
void clearMatrix()
{
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
matrix[x][y]= (x==y)?1:0;
}
public Object clone()
{
Matrix4D clone = new Matrix4D();
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
clone.matrix[x][y] = matrix[x][y];
return clone;
}
public void multMatrix(Matrix4D multiplier)
{
float[][] bufMatrix = new float[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
bufMatrix[i][j] = matrix[i][0]*multiplier.matrix[0][j]+matrix[i][1]*multiplier.matrix[1][j]+
matrix[i][2]*multiplier.matrix[2][j]+matrix[i][3]*multiplier.matrix[3][j];
matrix = bufMatrix;
}
public void multVector(Vertex3D multiplier) // calculate new coordinates for the Vertex (same data as Vector (x,y,z)) with this Matrix
{
float tempX = multiplier.x;
float tempY = multiplier.y;
float tempZ = multiplier.z;
multiplier.x = matrix[0][0] * tempX + matrix[0][1] * tempY + matrix[0][2] * tempZ + matrix[0][3];
multiplier.y = matrix[1][0] * tempX + matrix[1][1] * tempY + matrix[1][2] * tempZ + matrix[1][3];
multiplier.z = matrix[2][0] * tempX + matrix[2][1] * tempY + matrix[2][2] * tempZ + matrix[2][3];
}
public void printMatrix()
{
for (int i = 0; i < 4; i++)
System.out.println(matrix[0][i] + " " + matrix[1][i] + " " + matrix[2][i] + " " + matrix[3][i] + " ");
}
}
I'll try and explain simple matrices to you but I'm no expert.
A matrix is used to show which vertexs are connected and by how many edges. Say you had three points A,B + C and A was connected to B twice and C once you would write a matrix like this.
My Homepage
Visit my forums
Edited by - crazy-vasey on November 21, 2000 3:05:51 PM
A matrix is used to show which vertexs are connected and by how many edges. Say you had three points A,B + C and A was connected to B twice and C once you would write a matrix like this.
|
My Homepage
Visit my forums
Edited by - crazy-vasey on November 21, 2000 3:05:51 PM
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement

