www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: AGP on February 24, 2017, 05:02:18 pm

Title: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on February 24, 2017, 05:02:18 pm
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: [Select]
     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();
     }
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: EgonOlsen on February 24, 2017, 06:20:50 pm
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.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on February 24, 2017, 06:30:10 pm
Do you see any reason why my method shouldn't work?
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: EgonOlsen on February 24, 2017, 07:22:18 pm
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.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on February 24, 2017, 07:24:14 pm
Ahh, it's probably being called in the event thread. ws is character.getTransformedCenter(). How would that hurt things?
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on February 24, 2017, 07:59:02 pm
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.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: EgonOlsen on February 24, 2017, 08:36:05 pm
Well, then just output some debug information. It's impossible to tell why it's not working as expected without any further information.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on March 03, 2017, 09:49:35 pm
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...
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: EgonOlsen on March 08, 2017, 08:38:06 am
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.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on March 08, 2017, 04:34:38 pm
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.
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: EgonOlsen on March 09, 2017, 10:38:39 am
Are you doing this in a thread?
Title: Re: PolygonManager.getTransformedVertex is Unreliable
Post by: AGP on April 11, 2017, 07:39:14 pm
It wasn't done on a thread other than the main thread.