PolygonManager.getTransformedVertex is Unreliable

Started by AGP, February 24, 2017, 05:02:18 PM

Previous topic - Next topic

AGP

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.


     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();
     }

EgonOlsen

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.

AGP

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

EgonOlsen

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.

AGP

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

AGP

OK, I wrote circles around the event thread. The code is ugly as it stands and it's still not working. It's now being called by the game loop, which is in the main thread.

EgonOlsen

Well, then just output some debug information. It's impossible to tell why it's not working as expected without any further information.

AGP

Might it be a child/parent thing? My planes get added as children of a dummy pivot and the pivot gets translated, not the individual planes themselves...

EgonOlsen

Quote from: AGP on March 03, 2017, 09:49:35 PM
Might it be a child/parent thing?
No, that shouldn't matter. The used matrix is the same one that is used for all other transformations (including for rendering) and it takes the parent objects into account just as it should.

AGP

The product of getTransformedVertex() often didn't work. I re-wrote boardSpaceFromWorld by getting the transformed centers of all surounding planes, and now it works every time.

EgonOlsen


AGP

It wasn't done on a thread other than the main thread.