Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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  
  Mouse to 3D space?  (Read 680 times)
0 Members and 1 Guest are viewing this topic.
Offline Varkas

JGO Knight


Medals: 14
Projects: 5


iDream


« Posted 2012-11-15 12:30:34 »

I'd assume that it should be possible to use the projection matrix "backwards", and calculate a line in 3D space from the 2D mouse coordinate on the screen. Intersecting this line with the objects it should be possible to find the object(s) which was pointed at.

Is this the way to do it? And if yes, how does one do the math? Matrix operations are nontrivial for me, so I will ned a bit of help with that. Thanks Smiley

if (error) throw new Brick();
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #1 - Posted 2012-11-16 01:38:50 »

Have you tried asking the all-knowing Google? Wink

Offline sproingie
« Reply #2 - Posted 2012-11-16 01:52:15 »

The term that you might find handy to google for is "ray picking".  A "simple" way to get that ray is with a couple calls to GLU.gluUnproject, using your near and far planes as Z values.  There's probably better ways to do it now though.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Agro
« Reply #3 - Posted 2012-11-16 03:08:38 »

It is actually really easy. this function will return a vector in the direction you are pointing in, assuming you have a yaw and pitch value(you can't just get it from a single coordinate, you need some sense of direction too):

1  
2  
3  
Vector3D getDirectionVector() {
   return new Vector3D(-Math.cos(pitch * Math.PI / 180.0) * Math.sin(yaw * Math.PI / 180.0), Math.sin(pitch * Math.PI / 180.0), -Math.cos(pitch * Math.PI / 180.0) * Math.cos(yaw * Math.PI / 180.0));
}

Note: This is assuming yaw and pitch are in degrees.

Tell me if it didn't work, might've messed up something there.

Then after that, you can use ray tracing or some other good  method to find the object(s) you're pointing at. Smiley

Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #4 - Posted 2012-11-16 03:13:32 »

Try to keep pitch and yaw as radians to save performance.

/nitpicky

Online Agro
« Reply #5 - Posted 2012-11-16 03:14:25 »

Oh yeah, always use radians. Degrees is just better for debugging/presentation honestly, but personally I like it better. >Smiley

Offline Varkas

JGO Knight


Medals: 14
Projects: 5


iDream


« Reply #6 - Posted 2012-11-16 14:08:21 »

Have you tried asking the all-knowing Google? Wink

I guess I was rather feeling for a forum posting yesterday. But it seems easy enough, I think I can find a way. If I remember right I already had solved that problem in an very old 3D game attempt of mine, and maybe I can revive that code (the vector from eye position to mouse-on-projection-surface position should be just the ray I was looking for. It guess it was more obvious when I did the projection in my own code ...)

Thanks for the responses, and sorry, next time I'll try to google first.

if (error) throw new Brick();
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (135 views)
2013-05-17 21:29:12

alaslipknot (144 views)
2013-05-16 21:24:48

gouessej (174 views)
2013-05-16 00:53:38

gouessej (168 views)
2013-05-16 00:17:58

theagentd (178 views)
2013-05-15 15:01:13

theagentd (162 views)
2013-05-15 15:00:54

StreetDoggy (207 views)
2013-05-14 15:56:26

kutucuk (231 views)
2013-05-12 17:10:36

kutucuk (230 views)
2013-05-12 15:36:09

UnluckyDevil (235 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.09 seconds with 21 queries.