From: Ben S. <bs...@ia...> - 2003-05-27 05:34:03
|
Ian Overton wrote: > Thanks for all the info, but I haven't done this kind of stuff so long I > still don't understand. Allow I do see what I need to do. I don't understand > what some of the things are that you are doing. No worries. Sometimes I forget how long I've been doing this and gloss over some of the more important details. Hell, I'll probably just confuse you with this response by trying to go into more detail. ;) Keep pinging the list until you've got it all worked out though! > >>From what I understand I should make a ray where the starting point is > transformedx, transformedy, 0 and the ending point is > transformedx, -10(where my plane is), and some point z where transformedx > and y are what they are in world coordinates provided by the gluproject and > I can figure out the z coord by figuring out the slope from the two z coords > I put in. That is what I understood I was supose to do, but I really have no > idea how to do some of these. Sort of. Imagine that your screen is simply a plane in the virtual world. Your mouse cursor is somewhere on that plane. You want to cast a ray straight out of that screen and see where it hits the plane. A whiteboard would be great here. ;) What you have is a point on that plane (mx, my, 0) in a coordinate system where +X heads to the right, +Y heads up, and +Z goes into the screen. You rightfully want to transform that from view(screen) coordinate space to world coordinate space. That's what gluUnProject is for. Lets say that gives us the transformed coord (tx, ty, tz). Now this new position is relative to the world origin rather than the upper left corner of the screen. However, you want to cast a ray out into the world starting from the screen. A ray is a starting point (origin) and a direction vector. Remember that a ray has no end point. The transformed mouse position would work well as the ray's origin, but you still need to figure out what direction in which to cast the ray. That's where that extra transformed point comes in handy. If you get the position of the mouse not on the screen, but slightly into it (say (mx, my, 0.02) you have two points that lie on the line you want your ray to be on. You can then calculate the direction of the ray by taking difference between the two points and then making that direction vector of unit length. Now that you have the ray (assuming you already have knowledge about your plane), you can compute the intersection between them. That intersection is the point at which the mouse would project to the plane. > >>// Unproject the point of the mouse to form a ray into the screen >>Point beg = // gluUnProject on the point (x,y,0.0) >>Point end = // gluUnProject on the point (x,y,0,02) >> >>// Create the ray >>Vector direction = end - beg; > > > why are you taking end-beg again I thought you needed end for the > magnatuide, but end-beg is easy enough to understand. To create a vector in cartesian coordinates (x,y,z) given two points in space, you need to subtract the point at the beginning of the vector from the end of it. That will give you a vector from beg to end. > > direction.normalize(); > I'm not sure what this does, but from I understand normalize just gives a > right angle of whatever the plane or line is and it looks to me like > direction would be just a point. I've found some info on normalizing > something with three points, because it makes up a plane, but I haven't > found one with two points, but I think you can do it. I'm not sure though If you normalize a vector, you change its length to be of unit length (1.0). To do this, you simply divide each component by the length of the vector. In other words, length = direction.length(); direction.x = direction.x / length; direction.y = direction.y / length; direction.z = direction.z / length; > > Ray ray(beg, direction); > This makes a ray where beg is the starting point and direction is a vector > that makes the slope and direction for the ray. Although I'm not sure how we Right! > made it. Also to get a ray like that do I just add beg to the direction > vector, so beg is where 0,0,0 was? By having the beginning and the direction, you already have enough knowledge to represent the ray. You can find any point along the ray by doing: pt = beg + direction * t Where t is the distance along the direction vector to travel to get to pt. > > >>// Create the plane >>Point plane_position = // Some point on the plane >>Vector plane_normal = // The plane's normal >>Plane plane(plane_normal, plane_position); > > > Not really sure what this does, does it create my grid? what is point > plane_position supose to be -10? and I'm not sure what the other two do > either. That part was really me just trying to represent your grid in some simple fashion. I assumed that you already have a point that lies on the grid (plane_position). I also assumed that you have (or can calculate) a vector of unit length pointing up from the grid (plane_normal). Lets say that your grid is defined as running from (-1, -1, -10) to (1, 1, -10) on the Z-plane where z=-10. That would be two opposite corners on the grid. Given that information, either point could be plane_position. > > >>// Intersect the ray with the plane >>dist = plane.intersect(ray); >>if (dist < 0.0) >>{ >> // No intersection >>} > > > plane.intersect(ray) returns how long the ray is when it hits my grid? Right. It would return the t part of the ray equation from above. > > >>// Find the point of intersection >>Point pos_on_plane = ray.getOrigin() + ray.getDirection() * dist; > > ok ray.getOrigin is beg + the direction vector I have no idea how we got and > then times the distance of the ra That's the ray equation from above. pt = beg + direction * t ray.getOrigin() is beg. ray.getDirection is direction. dist is t. > > I'm not sure if I broke it down right. Could you please explain it a little > more? It has been a long time since I've messed with math like this and I've > just started learning opengl a few days ago. I really want to learn how to > do this. I've been trying to figure it out for the passed 4 days and it's > driving me nuts. Thanks so much for the help I hope that helped more. Let me know if I answered your questions or if you have any more. I can probably come up with the plane/ray intersection algorithm if you need it. Happy coding! cheers, -Ben Scott bs...@ia... > > Ian Overton > > ----- Original Message ----- > From: "Ben Scott" <bs...@ia...> > To: "Chad Austin" <ae...@vr...> > Cc: "Ian Overton" <ove...@ia...>; "gdd" > <isu...@li...> > Sent: Monday, May 26, 2003 4:48 PM > Subject: Re: [isugamedev-devel] Re: Problem > > > >>Unfortunately, as far as I can tell, gluUnProject is not as useful as it >>probably should be. What you can get out of it is a the position of your >>(x,y,z) screen coord in object/world coordinates. While that sounds >>useful in and of itself, it's really not. >> >>It sounds like you're trying to project the mouse position onto a plane >>(your grid). What has worked for me in the past is to get two points >>using gluUnProject, one at the screen position (z=0) and one slightly >>into the screen (z=0.02) [Yes, that is positive 0.02. In view >>coordinates Z points into the screen]. With those two points you can >>create a ray starting at the screen position cast in the direction of >>your plane. From there you can find the intersection point of the ray >>and the plane. With all that you can "click" on the grid. >> >>Some fun code (based on yours). This is more pseudocode than anything >>with the assumption of some basic classes defined (Point, Vector, ...). >> >>//--------------------------------------------------- >> >>// Unproject the point of the mouse to form a ray into the screen >>Point beg = // gluUnProject on the point (x,y,0.0) >>Point end = // gluUnProject on the point (x,y,0,02) >> >>// Create the ray >>Vector direction = end - beg; >>direction.normalize(); >>Ray ray(beg, direction); >> >>// Create the plane >>Point plane_position = // Some point on the plane >>Vector plane_normal = // The plane's normal >>Plane plane(plane_normal, plane_position); >> >>// Intersect the ray with the plane >>dist = plane.intersect(ray); >>if (dist < 0.0) >>{ >> // No intersection >>} >> >>// Find the point of intersection >>Point pos_on_plane = ray.getOrigin() + ray.getDirection() * dist; >> >>// Jump up and down and celebrate our achievement >>doTheHappyDance(); >> >>//--------------------------------------------------- >> >>I hope that helps. Good luck! >> >>cheers, >>-Ben Scott >>bs...@ia... |