Author Topic: PolygonManager.getTransformedVertex is Unreliable  (Read 5194 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
PolygonManager.getTransformedVertex is Unreliable
« 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();
     }
« Last Edit: February 24, 2017, 05:38:55 pm by AGP »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #1 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #2 on: February 24, 2017, 06:30:10 pm »
Do you see any reason why my method shouldn't work?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #3 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #4 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?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #5 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #6 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #7 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...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #8 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #9 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #10 on: March 09, 2017, 10:38:39 am »
Are you doing this in a thread?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: PolygonManager.getTransformedVertex is Unreliable
« Reply #11 on: April 11, 2017, 07:39:14 pm »
It wasn't done on a thread other than the main thread.