Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  clicking on sphere  (Read 2737 times)
0 Members and 1 Guest are viewing this topic.
Offline Middy

Junior Devvie




Java games rock!


« Posted 2003-10-26 15:58:44 »

well while I am spamming I might as well continue. I have spheres in space how do I catch mouse over and mouse click?

When do I get my makeMyGameAsILike() extension?
Offline tom
« Reply #1 - Posted 2003-10-26 17:56:06 »

There is two methods:

1. Cast a ray from the center of the camera threw the pixel of the mouse cursor hotspot, and find which sphere it intersects. Ray - sphere intersection is easy and fast, so this might be the best solution.

2. Set up the view frustum so that it only covers the one pixel of the mouse cursor hotspot. Then render each object using an unique color. Then read back the rendered pixel. Its color will give you what object were intersected. This is a more general solution.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2003-10-26 17:59:08 »

Quote
2. Set up the view frustum so that it only covers the one pixel of the mouse cursor hotspot. Then render each object using an unique color. Then read back the rendered pixel. Its color will give you what object were intersected. This is a more general solution.


And will be slow as hell because you've got to transfer that information back the wrong way across the AGP. You'll end up needing proper geometry intersection testing like ray-sphere anyway so you might as well code it right in the first place.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Middy

Junior Devvie




Java games rock!


« Reply #3 - Posted 2003-10-26 18:42:21 »

So the GL_SELECTION in the red book is method 2?

When do I get my makeMyGameAsILike() extension?
Offline tom
« Reply #4 - Posted 2003-10-26 19:16:49 »

Looking in the red book I see that there are 3 methods Smiley

Method 2 is the one described under "Object Selection Using the Back Buffer".

Opengl also has it's own selection mechanism. It has it's own chapter in the red book. You render your objects in a special selection mode. You give the object a name by calling glPushName(int name) or glLoadName(int name). At the end opengl returns the  picked names. You limit the rendered area using a special pick matrix.

Offline Middy

Junior Devvie




Java games rock!


« Reply #5 - Posted 2003-10-27 07:14:36 »

Seems like GL_SELECTION is the easiste.

Even though I will need ray intersection... any good toutorials for that?

When do I get my makeMyGameAsILike() extension?
Offline Middy

Junior Devvie




Java games rock!


« Reply #6 - Posted 2003-10-28 10:19:17 »

well I managed to set up a ray and make it intersect witha  sphere... BUT I have run into another problem.

Translating mouse coords(screen coords) into world coords so I can set the origin of the ray.

What about the direction... uuhm

/me head hurts

When do I get my makeMyGameAsILike() extension?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2003-10-28 11:09:26 »

You can use gluUnProject with two different depth values to convert a mouse click into a couple of world coordinates, then you can construct your ray from them.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline whome

Junior Devvie




Carte Noir Java


« Reply #8 - Posted 2003-10-28 12:31:43 »

Middy,
Do you mind releasing your code as an example. I wish to do same but am just started to do simple 3d programming. Abstracts and terms used in several answers usually give me zero information :-)
Offline Middy

Junior Devvie




Java games rock!


« Reply #9 - Posted 2003-10-28 12:57:22 »

its distributed in a great deal of classes. So it easier if U ask for specific stuff

When do I get my makeMyGameAsILike() extension?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Middy

Junior Devvie




Java games rock!


« Reply #10 - Posted 2003-10-28 13:13:25 »

actually speaking of code I could use an example on calling gluUnproject

especially the GL_PROJECTION_MATRIX and GL_MODELVIEW

When do I get my makeMyGameAsILike() extension?
Offline whome

Junior Devvie




Carte Noir Java


« Reply #11 - Posted 2003-10-30 18:10:48 »

I've looked at the Xith3D sourcecode implemented. It uses gl.glRenderMode(GL.GL_SELECT); mode if picking is enabled. (code can be see in CanvasPeerImpl.java.renderStart method). It's not really "cast a ray..." method suggested previously.

Quote
Cast a ray from the center of the camera threw the pixel of the mouse cursor hotspot, and find which sphere it intersects. Ray - sphere intersection is easy and fast, so this might be the best solution.

If anyone had a very simple example code to do "cast a ray" picking method I appreciate a lot.
Offline Middy

Junior Devvie




Java games rock!


« Reply #12 - Posted 2003-10-30 18:32:51 »

I tried implementing it... Its not that easy..

First U must translate your mousecoord to world coord to get a origin(use gluUnport). A ray is a origon coord and a vector. Then U can cast the ray and test on all objects and see if it intersects. I gave up on it since I actually managed to crash the VM.

So.. I am desperatly trying to make my selection work(using GL.SELECT).. I dont think I get the concept, I only get data in buffer when I click weird places

Translation code (do not work)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      DoubleBuffer       glProjection =  ByteBuffer.allocateDirect(8*4).order(ByteOrder.nativeOrder()).asDoubleBuffer();
            DoubleBuffer       glModel      = ByteBuffer.allocateDirect(8*4).order(ByteOrder.nativeOrder()).asDoubleBuffer();
            IntBuffer viewPort = ByteBuffer.allocateDirect(4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
            DoubleBuffer result = ByteBuffer.allocateDirect(8*4).order(ByteOrder.nativeOrder()).asDoubleBuffer();
           
           
            GL.glGetDouble(GL.GL_PROJECTION_MATRIX, glProjection);
            GL.glGetDouble(GL.GL_MODELVIEW_MATRIX, glProjection);
            GL.glGetInteger(GL.GL_VIEWPORT,viewPort);
           
           
            GLU.gluUnProject(0,0,0,glModel,glProjection,viewPort,result);



Intersect code for a sphere:
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  
public boolean intersect(Ray ray) {

         float dx = _center.getX() - ray.get_origin().getX();
         float dy = _center.getY() - ray.get_origin().getY();
         float dz = _center.getZ() - ray.get_origin().getZ();
       
         float v = ray.get_direction().dot(dx, dy, dz);
       

         // Do the following quick check to see if there is even a chance
         // that an intersection here might be closer than a previous one
         if (v -_radius > ray.getT())
              return false;

         // Test if the ray actually intersects the sphere
         float t = this._radSqr + v*v - dx*dx - dy*dy - dz*dz;
         if (t < 0)
             return false;

         // Test if the intersection is in the positive
         // ray direction and it is the closest so far
         t = v - ((float) Math.sqrt(t));
         if ((t > ray.getT()) || (t < 0))
             return false;

         ray.setT(t);
         ray._asterHit= this;
         
         return true;
         
       }

When do I get my makeMyGameAsILike() extension?
Offline Middy

Junior Devvie




Java games rock!


« Reply #13 - Posted 2003-10-30 18:34:19 »

where do I find that example?.. I only seen NeHes

When do I get my makeMyGameAsILike() extension?
Offline whome

Junior Devvie




Carte Noir Java


« Reply #14 - Posted 2003-10-31 15:24:49 »

New 3D engine and is about to start using JOGL as renderer.
http://www.mind2machine.com
http://sourceforge.net/projects/open-mind

They had a sample for picking objects: openmind.samples.pickingDemo

I looked at it, but the actual picking code is somewhere behind the OM engine. I did not browse that codebase.

Xith3D example had more pure gl implementation without the engine support.
Offline tom
« Reply #15 - Posted 2003-10-31 17:55:02 »

I see a couple of problems with your translation code:

1. glProjection and glModel represents double matrices which has 16 elements, not 4. This is why the vm crashes. LWJGL does not handle buffers with wrong size cracefully Smiley

2. You store the model view matrix in glProjection instead of glModel. I usume this is a typo Wink

Here is my go at it:
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  
/**
 * Creates a ray that goes from the origin of the camera and threw the mouse
 * cursor. The result is in the current model view space.
 * @param mousex, mousey position of the mouse in screen space
 * @param eyeOut where the un projected position of the origin of the camera is stored
 * @param dirOut where the direction of the resulting ray is stored.
 */

private void unProject(int mousex, int mousey, Vector3f eyeOut, Vector3f dirOut) {
      DoubleBuffer projection = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
      DoubleBuffer model = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
      IntBuffer viewPort = ByteBuffer.allocateDirect(4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
      DoubleBuffer result = ByteBuffer.allocateDirect(3*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
      GL.glGetDouble(GL.GL_PROJECTION_MATRIX, projection);
      GL.glGetDouble(GL.GL_MODELVIEW_MATRIX, model);
      GL.glGetInteger(GL.GL_VIEWPORT, viewPort);
      GLU.gluUnProject(mousex, mousey, 1, model, projection, viewPort, result);
      Vector3f target = new Vector3f((float)result.get(), (float)result.get(), (float)result.get());
      // reuse buffers
      projection.rewind();
      model.rewind();
      viewPort.rewind();
      result.rewind();
      GLU.gluUnProject(mousex, mousey, 0, model, projection, viewPort, result);
      Vector3f eye = new Vector3f((float)result.get(), (float)result.get(), (float)result.get());
      dirOut.sub(target, eye);
      dirOut.normalize();
      System.out.println("eye="+eyeOut.x+" "+eyeOut.y+" "+eyeOut.z);
      System.out.println("dir="+dirOut.x+" "+dirOut.y+" "+dirOut.z);
}

Vector3f is a 3d vector class from java3d.

The resulting ray is defined in the current modelview matrix. So if you call unProject right after gluLookAt, you will get the ray in world coordinates. If you call it right before you render your sphere, you get the ray in object space. In object space you can check intersection between ray, and the sphere at location (0, 0, 0).

Hope this makes sense.

Offline tom
« Reply #16 - Posted 2003-10-31 17:59:52 »

..and here is a descent opengl tutorial: http://isgwww.cs.uni-magdeburg.de/graphik/lehre/cg1/dokumente/gltutorial.pdf

Offline shochu

Junior Devvie




Java games rock!


« Reply #17 - Posted 2003-12-13 00:32:07 »

I had a few general questions about the ray casting technique here.

1)  What exactly is the ray given by dirOut in your example code.  Is this the ray which goes from the camera through the point clicked on by the mouse?

2) Does each object in  your scene have its own model view matrix?

3) If each object has its own model view matrix then which one do you use when you want to get the ray of the mouse pick?


THanks for your help!
Offline tom
« Reply #18 - Posted 2003-12-13 12:00:24 »

Hi shochu

1) yes, dirOut is the ray which goes from the camera through the point clicked on by the mouse.

2) I haven't actually used the code. I only made it to help. But Each object has it's own modelview matrix. It is the matrix that is on top of the opengl modelview stack when the object is drawn.

3) If you want to get the ray in object space, you use the modelview matrix that is used to draw the object.

Pages: [1]
  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.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!