Advertisement

Tracing rays from perspective view

Started by June 23, 2003 02:02 PM
0 comments, last by aleks_1661 21 years, 8 months ago
I am writing a 3d editor resembling the one used for command and conquer generals, i have a terrain object on to which i want to place objects. The object placement is to be achieved by clicking the mouse over the desired position on the terrain. The problem i am having is how to calculate the 3d position from the 2d mouse co-ordinates. i assume i need to trace a ray from the rear clipping plane to the terrain, the intersection giving me the correct location but i am unsure of how to do this, if anyone knows how this can be achieved i would be grateful. Thanks :-) "Very funny, Scotty. Now beam down my clothes."

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

well, you got the camera position, the FOV on both X and Y axis, the near plane Z value, the orientation of the camera, the resolution of the display... should not be too hard.

first, you need to work out the virtual display, like the coordinate of the 4 corners of your monitor, but transfered in the virtual world.

once you have the virtual display dimensions, you work out the coordinates of the pixel on the display (a simple linear interpolation).

once you got that, you position the display relative to the camera orientation and posiiton. You apply the transformations to the point, and presto. You have the coordinates of the point in your world.

From there, you ray-cast a ray from your camera position to the point in space, and see what intersects.


objects
-------

camera(Vector pos, Matrix orientation, float near_plane, float fovy, float aspect_ratio).

display(int width, int height)

pixel (int x, int y)

Vector P (pixel, but transformed in the 3D world).

Ray (Vector Start, Vector Dir)

//------------------------------------------------------// work out the dimensions of the ''virtual'' display at the near plane.//------------------------------------------------------float ymax =  tan(fovy / 2) * (camera.near_plane);float ymin = -tan(fovy / 2) * (camera.near_plane);floay xmax = ymax * camera.aspect_ratio;floay xmin = ymin * camera.aspect_ratio;//------------------------------------------------------// work out the pixel coord on the virtual display (inear interpolation)//------------------------------------------------------P.x = xmin + (pixel.x / (float) display.width ) * (xmax - xmin);P.y = ymin + (pixel.y / (float) display.height) * (ymax - ymin);P.z = camera.near_plane;//------------------------------------------------------// transform the pixel into the camera positioning//------------------------------------------------------P *= camera.orientation;P += camera.position;Ray.start = camera.position;Ray.dir   = (P - Ray.start).UnitVector();



Everything is better with Metal.

This topic is closed to new replies.

Advertisement