jPCT - a 3d engine for Java > Support

PolygonManager.getTransformedVertex is Unreliable

(1/3) > >>

The following is supposed to test if a character is over a particular square in a 2d array of planes (as created from the ExtendedPrimitives class). It doesn't work. The previous version of this method compared the centers of all planes and it worked (but looked terrible). I should note that I'm using the software renderer.

--- Code: ---     public java.awt.Point boardSpaceFromWorld(SimpleVector ws) {
for (int y = 0; y < planes[0].length; y++) {
     for (int x = 0; x < planes.length; x++) {
Object3D plane = planes[x][y];
PolygonManager polyMan = plane.getPolygonManager();
SimpleVector[] vertices = new SimpleVector[6];
vertices[0] = polyMan.getTransformedVertex(0, 0);//, plane.getInverseWorldTransformation(), new SimpleVector());
vertices[1] = polyMan.getTransformedVertex(0, 1);//, plane.getInverseWorldTransformation(), new SimpleVector());
vertices[2] = polyMan.getTransformedVertex(0, 2);//, plane.getInverseWorldTransformation(), new SimpleVector());
vertices[3] = polyMan.getTransformedVertex(1, 0);//, plane.getInverseWorldTransformation(), new SimpleVector());
vertices[4] = polyMan.getTransformedVertex(1, 1);//, plane.getInverseWorldTransformation(), new SimpleVector());
vertices[5] = polyMan.getTransformedVertex(1, 2);//, plane.getInverseWorldTransformation(), new SimpleVector());
float minX = Float.MAX_VALUE, maxX = Float.MIN_VALUE, minZ = Float.MAX_VALUE, maxZ = Float.MIN_VALUE;
for (int i = 0; i < 6; i++) {
     if (minX > vertices[i].x)
minX = vertices[i].x;
     if (maxX < vertices[i].x)
maxX = vertices[i].x;
     if (minZ > vertices[i].z)
minZ = vertices[i].z;
     if (maxZ < vertices[i].z)
maxZ = vertices[i].z;
if (ws.x > minX && ws.x <= maxX && ws.z > minZ && ws.z <= maxZ)
     return new java.awt.Point(x, y);
return new java.awt.Point();

--- End code ---

No, it's not unreliable. It can't be, because it's just a simple straight forward calculation. Have you checked the results against getTransformedCenter() for each object? They should be somehow related.

Do you see any reason why my method shouldn't work?

Yours? Not at first glance, but I don't know what ws exactly is and if the planes do consist of only 2 triangles each.

getTransformedVertex()? No, not at all. It's a simple multiplication of a vector with a matrix. It can't be wrong unless some neutrino from space hits the computer's fpu or something like that... ;)

Just make sure that you are calling this in the rendering thread and not in some other one, because that might cause some troubles.

Ahh, it's probably being called in the event thread. ws is character.getTransformedCenter(). How would that hurt things?


[0] Message Index

[#] Next page

Go to full version