Calc 3 for RB?

DarkOcean
Unregistered
 
Post: #1
Blink Em... yeah. You see that I have one post under my belt. Please be nice to me... I have a 3D geometry problem in RB4.5... RB3D to be more specific.

Peoblem is that I'm an idiot. Five tutors in including the head of the math department at my college could not solve this problem:
I have a 3D vector (the camera vector) and a 2D coordinate (mouse click in the FOV). How do I get a new 3D vector for a ray that intersects an object the player clicked on?

RB4.5's built inCross function looks like it might be something that I might use... but I don't understand. I got a D in Algebra in high school. I'm a tad dyslexic... ADD and all that shiznit... mostly an artist but I have a game development contract for someone too important to even talk to me and I kinda need to figure this out. Sad Can anybody help me? Please? I need a simple, compact function but I don't expect anybody to just hand it to me... I'm willing to work for it. Any ideas?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
You need a bit more information than that.

You'll need to do the reverse projection from 2D to 3D space yourself, by multiplying by an inverse matrix.

If you're happy looking at C source code, I can provide OpenGL code that does it, and the source for gluUnProject (which contains the math) is available online.
Quote this message in a reply
DarkOcean
Unregistered
 
Post: #3
Please. Anything helps. I don't rightly understand the math... but I might be able to convert some code. I took some computer science/cpp classes. I just understand that one blob of math does something... not sure why. I already jammed two functions I did not fully understand together to make a camera rig. It orbits a POI on a fixed distance sphere. I did not add constraints yet. Please. Any code is good. thankyou.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Code:
GLdouble modelViewMatrix[16];
    glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);

    GLdouble projectionMatrix[16];
    glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);

    GLdouble nearX, nearY, nearZ;
    GLdouble farX,  farY,  farZ;

    gluUnProject(locationInMe.x, locationInMe.y, 0.0f,
                 modelViewMatrix, projectionMatrix, viewport,
                 &nearX, &nearY, &nearZ);

    gluUnProject(locationInMe.x, locationInMe.y, 1.0f,
                 modelViewMatrix, projectionMatrix, viewport,
                 &farX, &farY, &farZ);

You'll need the modelview & projection matrices from RB, as well as the viewport (which may just be the dimensions of the window).

The two gluUnProject calls convert points on the near and far clipping planes back into 3D space, defining the two endpoints of the line segment defined by the mouse location. You'll need to google for gluUnProject source, should be available from Mesa's CVS.
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #5
The easiest way is definitely to use gluUnproject().

However, you can calculate the angular offset of the click location from the camera's direction in the horizontal and vertical planes (A = (camera angle in that plane) + (FOV angle in that axis)*(click coordinate/view size) - (FOV angle)/2). From that, you can extend a vector from the camera location at those angles. That will probably break down at large vertical angles though, I haven't really tried it (I use the unproject method myself in GL).

Try figuring it out on a piece of paper, even if you're as bad at drawing perspective as I am it will help you think about what's going on.

(edit) OK that took too long to write Rasp I also don't know what 3D services RB provides. It can access OpenGL? Neat...
Quote this message in a reply
DarkOcean
Unregistered
 
Post: #6
Quote:Originally posted by Mark Levin
The easiest way is definitely to use gluUnproject().

However, you can calculate the angular offset of the click location from the camera's direction in the horizontal and vertical planes (A = (FOV angle in that axis)*(click coordinate/view dimension) - (FOV angle)/2). From that, you can extend a vector from the camera location at those angles. That will probably break down at large vertical angles though, I haven't really tried it (I use the unproject method myself in GL).

Try figuring it out on a piece of paper, even if you're as bad at drawing perspective as I am it will help you think about what's going on.

(edit) OK that took too long to write Rasp I also don't know what 3D services RB provides. It can access OpenGL? Neat...


Okay... that helps. I think. FOV angle is 50 degrees and always will be. It's weird because every other measurement in RB3D is in radians. I AM using RB3D/Quesa and not raw, creepy, low level OpenGL. I'm not that smart... and don't make fun of me. Quesa is gettin' freakin' cool quickly. That is extremely helpfull... I think. I'll see if I can get it to work, then I'll let you know. Keep em' commin' this forum is awesome.
Quote this message in a reply
DarkOcean
Unregistered
 
Post: #7
Well... I got:
[SOURCECODE]
Function clickVector(clickX as integer, clickY as integer, cameraVector as vector3D) as vector3D
dim clickVector as vector3D
dim X,Y,Z as double
//FOV angle always 50. resolution always 800x600...for now.
X = 50*(clickX/800) - (50)/2)
Y = 50*(clickY/600) - (50)/2)
Z = 42 // life, the universe, and everything
clickVector = new vector3D(X,Y,Z)
return clickVector
end function
[/SOURCECODE]
and realised that I had no idea what I'm doing. I'm sorry. You can really tell I'm a noob, cantcha'? I made games before... all 2D. This is a whole different animal. I guess the ironic part would be that I'm forced to learn 3D to design a 2D board game. Can anybody modify this so it makes sense?
Quote this message in a reply
DarkOcean
Unregistered
 
Post: #8
nevamind. I hacked it.
Quote this message in a reply
Post Reply