Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Converting Screen Coordinates to World Coordinates  (Read 8354 times)
0 Members and 1 Guest are viewing this topic.
Offline Yuri Vl. Gushchin

Senior Member




Speak Java!


« Reply #30 - Posted 2004-03-06 10:36:15 »

It should be tested, and there is no pbs to include to, say, PickRenderResults, or somewhere there. Or, View itself...

Please submit a patch alone with a test case that tests/demonstrates the functionality.

Yuri

Yuri Vl. Gushchin
JProof Group
Offline gpothier

Senior Newbie




Say it no more!


« Reply #31 - Posted 2004-03-08 12:56:45 »

I created issue 69 (https://xith3d.dev.java.net/issues/show_bug.cgi?id=69)

It seems that the source code of my methods somehow vanished from my previous post, so those interested can download the attachment of issue 69.
Offline ewills

Junior Member




Java skeletal animation systems rock!


« Reply #32 - Posted 2004-08-26 17:31:13 »

This method is based on the Java3D source:

public void createPickRay(int x, int y, Point3f o, Vector3f d) {
 float rx  = (float)x/(float)getWidth() - 0.5f;
 float ry  = 1.0f - (float)y/(float)getHeight() - 0.5f;
 float px  = rx*physicalWidth;
 float py  = ry*physicalHeight;
 float vpd = camera.getViewPlaneDistance();
 if (camera.getPerspective())
   o.set(0.0f, 0.0f, vpd);
 else
   o.set(px, py, vpd);
 d.set(px, py, 0.0f);
 d.sub(o);
 d.normalize();

 view.getTransform(t1);
 t1.transform(o);
 t1.transform(d);
}

I maintain physicalWidth and physicalHeight as the actual  width and height of the canvas on the screen for accurate image scale.  Any values for physicalWidth and physicalHeight should work, providing the aspect ratio is preserved.

View plane distance can be calculated as:
0.25f*physicalWidth*(float)Math.tan(0.5f*fieldOfView))

It works!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ewills

Junior Member




Java skeletal animation systems rock!


« Reply #33 - Posted 2004-08-31 17:11:04 »

A better way to calculate view plane distance is:
0.5f*physicalHeight/(float)Math.tan(fieldOfView);
 Smiley
Offline Patheros

Senior Newbie





« Reply #34 - Posted 2005-10-17 04:12:19 »

I know this is a long time after, but if anyone is still interested I've done excatly what the inital poser wanted, but in c++. I don't know enough about Xith3D to know if this is even doable here., but here is the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
void XField::locate(int clickX, int clickY, double *x, double *z){
   double pos[3];
   
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glPushMatrix();
   glTranslated  ( 0.0 , 0.0 , 0.0 );
   glBegin(GL_TRIANGLES);
            glVertex3f(-virWidth/2,0.0,virDepth/2);
            glVertex3f(virWidth/2,0.0,virDepth/2);
            glVertex3f(virWidth/2,0.0,-virDepth/2);
   glEnd();
   glBegin(GL_TRIANGLES);
            glVertex3f(-virWidth/2,0.0,virDepth/2);
            glVertex3f(-virWidth/2,0.0,-virDepth/2);
            glVertex3f(virWidth/2,0.0,-virDepth/2);
   glEnd();
   glPopMatrix();

   GLfloat winX, winY, winZ;
   GLdouble modelMatrix[16];
   GLdouble projMatrix[16];
   GLint viewport[4];

   glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
   glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
   glGetIntegerv(GL_VIEWPORT,viewport);

   winX = (float)clickX;
   winY = (float)viewport[3] - (float)clickY;
   glReadPixels( int(winX), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

   gluUnProject(
      winX,
      winY,
      winZ,
      modelMatrix,
      projMatrix,
      viewport,
      &pos[0],
      &pos[1],
      &pos[2]
   );

   *x=pos[0];
   *z=pos[2];
}


Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #35 - Posted 2005-12-13 11:14:23 »

So what is the easiest way to implement this.....i mean do we have to do ..


    gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix);
    gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix);
    gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
     glu.gluUnProject


also if i  call gl functins than i am getting


Exception in thread "main" net.java.games.jogl.GLException: glGetError() returned the following error codes after a call to glGetDoublev(): GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION GL_INVALID_OPERATION
   at net.java.games.jogl.DebugGL.checkGLGetError(DebugGL.java:13910)
   at net.java.games.jogl.DebugGL.glGetDoublev(DebugGL.java:4218)


this is may be i am not getting the correct gl context ..to get gl context i am using
1  
2  
3  
      ((GLCanvas) canvasPeer.getComponent()).setRenderingThread(Thread.currentThread());
      gl=((CanvasPeerImpl)canvasPeer).getGl();
      glu=((CanvasPeerImpl)canvasPeer).getGlu();


shochu you must have done this ..how you solved this problem ?
so what is the right way to go about this.....it would be of great help if somebody has a code snippet for this...........thanks in advance

If i have to query the state machine than in xith how to go about it...

Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #36 - Posted 2005-12-13 18:53:40 »

think about it: A point on your screen converts to a ray in world coordinates.

So simply use

Canvas3D.createPickRay

:: JOODE :: Xith3d :: OdeJava ::
Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #37 - Posted 2005-12-22 07:50:01 »

I am able to convert the Screen Coordinates to world Coordinates by using  glu.gluUnProject in display of  CanvasPeerImpl.java , to get the modelview matrix i am using shape.getLocalToVworld() .
    currently  if i translate my objects its giving me correct coordinates  ex: if i draw a cube ( vertex 1=0,0,0
                                                                                                                                                                 vertex 2=1,0,0
                                                                                                                                                                 vertex 3=1,1,0
                                                                                                                                                                 vertex 4=0,1,0  ) and clicks on vertex 2 it return co-ordinates (1,0,0), Now if i translate this cube on screen and again clicks on vertex 2 i am getting correct co-ordinates that is (1,0,0).
   the problem comes if i rotate this cube about  X or Y axis and than clicks vertex 2 now the co-ordinates i am getting are something junk.
  Strangely if i rotate about Z axis this problem is not coming.
So only on rotation about X and Y axis i am not able to map my Screen Co-ordinates to World Co-ord.

Arne if you can suggest what i am may  be doing wrong.

Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #38 - Posted 2005-12-22 09:49:21 »

I am able to convert the Screen Coordinates to world Coordinates by using  glu.gluUnProject in display of  CanvasPeerImpl.java , to get the modelview matrix i am using shape.getLocalToVworld() .
    currently  if i translate my objects its giving me correct coordinates  ex: if i draw a cube ( vertex 1=0,0,0
                                                                                                                                                                 vertex 2=1,0,0
                                                                                                                                                                 vertex 3=1,1,0
                                                                                                                                                                 vertex 4=0,1,0  )
That's not a cube, thats a Square.
Quote
and clicks on vertex 2 it return co-ordinates (1,0,0), Now if i translate this cube on screen and again clicks on vertex 2 i am getting correct co-ordinates that is (1,0,0).
   the problem comes if i rotate this cube about  X or Y axis and than clicks vertex 2 now the co-ordinates i am getting are something junk.
What are those coordinates?
How do you translate the cube?
Quote
  Strangely if i rotate about Z axis this problem is not coming.
So only on rotation about X and Y axis i am not able to map my Screen Co-ordinates to World Co-ord.
I don't know what glu.gluUnProject does. Does it perform a pick - or what?

:: JOODE :: Xith3d :: OdeJava ::
Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #39 - Posted 2005-12-22 10:38:33 »

well sorry fr small typo...basically in my project i was using a cube but for simplicity a used square example , let me explain what i am doing ..

1) First i am creating a square using QuadArray by setting the specified co-oordinates.
vertex 1=0,0,0     vertex 2=1,0,0   vertex 3=1,1,0   vertex 4=0,1,0 .

2) To map the co-ordinates  i am using

         
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
                        double projection[] = new double[16];
         int viewport[] = new int[4];

         gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);    //TO Get Projection Matrix
        gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);                           //To Get ViewPort

                      //  For ModelView matrix of selected Node i am using
                         float model_view [ ]= new float [16];
                         node.getLocalToVworld().getOpengl(model_view);

         glu.gluUnProject(mouseX_Pos,viewport[3]-mouseY_Pos,o,model_view,projection,viewport,worldX,worldY,worldZ);


Hence i get the world co-ordinates of the mouse  in worldX,,worldY,worldZ,.

4) I am not using createPickray as my requirement is such that even if i translate or rotate the square ,on Clicking at vertices of square i should get the same values  that is for vertex 2 it should always be (1,0,0).
This means i have to take into account ModelView matrix of the shape  which is been moved or rotated so i have used GluUnproject.

3) Now to translate this square i am translating the TransformGroup which contain this square , or to rotate it a apply rotation to the TransformGroup.

4) Now if i rotate my Square about Z axis say by 30 deg clockwise  and now clicks any  vertex it gives me coorect values, as while applying rotation its modelview matrix gets updated so on using gluUnproj its able to map to correct values .

5) But when i apply rotation in X  and y direction  i am getting wrong values.

hopefully this time i am more clear about my req

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #40 - Posted 2005-12-22 13:06:19 »

Yes you're more clear this time Smiley

From your code it seems as if the geometry of your quad is not important. You don't use it in the calculation of the world coordinates.

I don't know if you realized this, but one point on your screen represents an infinite number of points in 3D-Space.
If the method only returns one point, then it can return any point on that ray, unless you specified something like "I want to have the point on the ray in 5 units distance". Because I don't see in your code, where you have such an restriction, I can only guess that you were lucky in the cases, where it worked Wink

:: JOODE :: Xith3d :: OdeJava ::
Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #41 - Posted 2005-12-22 14:46:43 »

oop's ..you are right ,earliar also i was getting only correct x and y axis but not  z  value.....thanks for pointing it out...dont know how i missed this point.
  I'll try now keeping this point in mind.....thanks fr the help.

Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #42 - Posted 2005-12-23 12:40:51 »

i am stuck with how to get the intersection point of the ray with the shape3D object i am able to see whether the ray intersect or not but how to get the intersection Point.
i am using.....my code for this
1  
2  
3  
4  
5  
          Point3f center =new Point3f();
          Vector3f forward =new Vector3f();
                       canvas.createPickRay(x,y,center,forward);
             BoundingSphere boundingSphere= node.getVworldBounds();          
             boolean isIntersection =boundingSphere.intersect(center,forward);

but how to get the intersection point

Offline arne

Senior Member




money is the worst drug- we should not let it rule


« Reply #43 - Posted 2005-12-23 17:59:53 »

that's called picking and there's loads of code here in these forums about the topic. try a search

:: JOODE :: Xith3d :: OdeJava ::
Offline khangharoth

Junior Member




There is more to life than JAVA...But Java Rocks


« Reply #44 - Posted 2005-12-27 11:15:33 »

now i am able to get the intersection point .... Wink  used your code and tweaked according to my requirement............

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
                                                 Point3f center = new Point3f();
                  Vector3f d = new Vector3f();
                  canvas.createPickRay(mouseClickPos_X,mouseClickPos_Y, center, d);

                  PickResult r = new PickResult((Shape3D) selectedNode, 0);
                                               
                  QuadIntersection t = new QuadIntersection();
                  Geometry geo = r.getGeom();


                  r.transform(center);
                  r.transform(d);

                  Vector3f pos1 = new Vector3f();
                  Vector3f pos2 = new Vector3f();
                  Vector3f pos3 = new Vector3f();
                  Vector3f pos4 = new Vector3f();
                  geo.getVertex(0, pos1);
                  geo.getVertex(1, pos2);
                  geo.getVertex(2, pos3);
                  geo.getVertex(3, pos4);
                  t.set(pos1, pos2, pos3, pos4);
                  float closestIntersect = Float.POSITIVE_INFINITY;
                  float f = t.test(center, d, closestIntersect);

 now i am getting correct   X,Y,Z  values , even on Translation and Rotation about Z axis  ,on clinking on vertices i am getting thier correct values....but when i apply rotation  about X and Y axis corresponding x and y values are coming wrong...
 In my project getting exact co-ordinates is very important ...........one more thing......if  one of the cube face is at an angle to my view direction and  on click of that face i need to change my view to be normal to that face......that is that face is facing the view direction ....how to go about it..
  thanks for the help ... Smiley


P.S : i am using PARALLEL_PROJECTION 

Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (20 views)
2014-04-15 18:08:23

BurntPizza (16 views)
2014-04-15 03:46:01

UprightPath (29 views)
2014-04-14 17:39:50

UprightPath (14 views)
2014-04-14 17:35:47

Porlus (30 views)
2014-04-14 15:48:38

tom_mai78101 (55 views)
2014-04-10 04:04:31

BurntPizza (112 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!