www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: janineh on December 12, 2007, 06:03:27 pm

Title: Select a side?
Post by: janineh on December 12, 2007, 06:03:27 pm
Hi,

I'm new to 3D and jPCT, and I hope this isn't a really stupid question.

Is is possible to use the mouse to 'select' a side of an object?

For example, a Primitive box has the texture of one side changed when that side is clicked on.

I can use Interact2D functions to get the PolygonID and use PolygonManager.setPolygonTexture to 'select' the polygon I clicked, but how can I also select the adjoining polygons on the same 'face' of the box?

Many thanks,
Janine

Title: Re: Select a side?
Post by: EgonOlsen on December 12, 2007, 10:20:28 pm
You have to write your own method to detect adjacent polygons i'm afraid, because "adjacent" highly depends on the object and the purpose. You can get vertex and polygon information back from an object using a GenericVertexController and the PolygonManager. I once did something similar, but sadly i can't find the code ATM...
Title: Re: Select a side?
Post by: janineh on December 13, 2007, 11:36:48 am
I thought that might be the case.

Can you point me in the direction of any examples of using vertex & polygon info from the GenericVertexController and the PolygonManager?

As I said, I'm new at this, and would like to see some example code to try and work out what I'm doing  ;)
Title: Re: Select a side?
Post by: janineh on December 14, 2007, 05:45:19 pm
I've been playing about with PolygonManager and GenericVertexController and have a couple of questions.

I've set up a very simple class the extends GenericVertexController:
Code: [Select]
public class TestVertexController extends GenericVertexController {

     TestVertexController() {
     }

     public void apply() {
       SimpleVector[] srcMesh=this.getSourceMesh();

       int size=this.getMeshSize();

       for (int i=0; i<size; i++) {
       System.out.println("A" + i + ": (" + srcMesh[i].x + ", " + srcMesh[i].y + ", " + srcMesh[i].z +")");
       
       }
     }
}

I have also put code into the mouse click event to print out vertex info from the PolygonManager:
Code: [Select]
SimpleVector td  = Interact2D.reproject2D3D(theCamera,frameBuffer,mousePosition.x,mousePosition.y);
int[] res = Interact2D.pickPolygon(theWorld.getVisibilityList(), td, Interact2D.EXCLUDE_NOT_SELECTABLE);

Object3D obj = theWorld.getObject(Interact2D.getObjectID(res));
PolygonManager polyManager = obj.getPolygonManager();
           
SimpleVector v0 = polyManager.getTransformedVertex(res[1], 0);
SimpleVector v1 = polyManager.getTransformedVertex(res[1], 1);
SimpleVector v2 = polyManager.getTransformedVertex(res[1], 2);
                       
System.out.println("V0: (" + v0.x + ", " + v0.y + ", " + v0.z + ")");
System.out.println("V1: (" + v1.x + ", " + v1.y + ", " + v1.z + ")");
System.out.println("V2: (" + v2.x + ", " + v2.y + ", " + v2.z + ")");

When I run the applet, and click on one polygon I get the following:
Code: [Select]
A0: (2.0, 2.0000002, 2.0000002)
A1: (2.0, 2.0000002, -1.9999996)
A2: (-2.0, 2.0000002, -1.9999996)
A3: (-2.0, 2.0000002, 2.0000002)
A4: (-2.0, -1.9999996, -1.9999998)
A5: (-2.0, -1.9999999, 2.0000002)
A6: (2.0, -1.9999996, -1.9999998)
A7: (1.999998, -2.0000017, 2.0000002)

V0: (2.0000002, 2.0000002, -1.9999999)
V1: (-1.9999998, -1.9999996, -2.0)
V2: (-1.9999998, 2.0000002, -1.9999999)

The problem is that the numbers don't exactly match - is this a rounding problem, or simply a System.out.println problem?

If I did V0.equals(A1) (assuming they were SimpleVector variables) would it return true?
(I know I could write code to compare and find out, but I haven't worked out how to do that yet and it's 4.30pm on Friday evening...)

Many Thanks,
Janine

Title: Re: Select a side?
Post by: EgonOlsen on December 16, 2007, 08:10:26 pm
The vertex controller gives you the vertices in object space, the polygon manager in world space. As long as there is no world transformation on that object (i.e. no rotation, translation...) both spaces match (in respect to that one object of course). So the numbers should be equal. If they aren't, that are accuracy problems with float format that happen during the transformation from object into world space (some matrix math is applied here even if the world's matrix hasn't changed). Just make your compares with a small +-delta value instead of doing a simple == compare. That's good idea for all kind of floating point math, because it's never exact.
Title: Re: Select a side?
Post by: janineh on December 18, 2007, 12:34:43 pm
Thank you for the explanation, I'll try it as you've suggested.