Matrix math in Java

Started by
14 comments, last by jbviklund 13 years, 4 months ago
Without projection matrix:


With projection matrix:


Latest code:
    public void setProjection(int width, int height, double zNear, double zFar){        double right = (width / height);        double left = -(width / height);        double top = 1.0;        double bottom = -1.0;        double a_a = (2.0 * zNear) / (right - left);        double a_c = -((right + left) / (right - left));        double b_b = (2.0 * zNear) / (top - bottom);        double b_c = -((top + bottom) / (top - bottom));        double c_c = zFar / (zFar - zNear);        double c_d = -((zNear * zFar) / (zFar - zNear));        double d_c = 1.0;        double[][] temp = {{a_a,  0.0,  a_c,  0.0},                           {0.0,  b_b,  b_c,  0.0},                           {0.0,  0.0,  c_c,  c_d},                           {0.0,  0.0,  d_c,  0.0}};        matrix = multiply(matrix, temp);    }

I think i need some beer! :P
Advertisement
Do you remember to do the division by w bit at the end?
When? after transforming the vertices, W is 1.0!?
I do this:

Matrix flattenedMatrix = projection.multiplyByMatrix(transform);					for(int i = 0; i < vertices.length; i++){	double[] result = flattenedMatrix.multiplyByVector(vertices);	result[0] /= result[3]; //x = x / w	result[1] /= result[3]; //y = y / w	points.add(pointFromVertex(result));}


If w is 1f, than something borked earlier in the chain.

I was wrong earlier, I actually do multiple the result. The pointFromVertex method is defined as:

public static Point pointFromVertex(double[] vertex){	Point p = new Point();	p.translate((int) Math.round(vertex[0] * 400) + 200, (int) Math.round(vertex[1] * 300) + 150);	return p;}


This code is assuming a 400x300 viewport. Of course you should never hardcode such values. I don't know what was going through my head, but that's that.

No idea why I use translate to set the point, either. Don't do that, that's redonkulous. You might trawl through the code I linked to earlier. It's exactly what you're doing, and pretty much nothing else.

Here it is in action. That's a jar, don't let Windows save it as a zip.
Well? Any progress?
I had to make a little visit to the hospital :P Has not been able to try anyting more yet..

This topic is closed to new replies.

Advertisement