Forum Discussion
fanchois
12 years agoHonored Guest
Raycast trough crosshair
Hi all,
I found a game with a fixed crosshair in the center of the screen. Raycast to clic on object are directly sent trough it,
and I would like to do the same...
http://www.entertherift.fr/gamecenter/fr/game-438-gorgon.html
Is it complicated? Are there some short tutorials?
I ve tried to understand the OVRMainMenu code but I don't really see the way to do it.
If someone could help me it would be nice^^
VRFORVR!
I found a game with a fixed crosshair in the center of the screen. Raycast to clic on object are directly sent trough it,
and I would like to do the same...
http://www.entertherift.fr/gamecenter/fr/game-438-gorgon.html
Is it complicated? Are there some short tutorials?
I ve tried to understand the OVRMainMenu code but I don't really see the way to do it.
If someone could help me it would be nice^^
VRFORVR!
3 Replies
- bluenoteExplorerI was implementing this last week and I also did not found a Rift specific tutorial.
I guess you have two possibilities in general:
1. Determine the z position by reading from the z buffer.
2. Determine the z position manually on the CPU by casting the camera ray against your geometry.
If your geometry is complex and you want to have a visible crosshair for all object in your scene, then I guess the first option is best.
In my case I only want a crosshair for certain GUI elements, which all have a flat surface (or rather I don't want my crosshair to follow all minor z-level changes of the GUI elements). In this case the second method is really simple: My GUI elements all follow the convention to be located at the z=0 plane in (their own) model space. Then I convert the camera position and direction to modelspace (by taking the inverse of the transformation matrix and simply evaluation the points (0,0,0) and (0,0,1) in model space). Now I can simply evaluate the camera ray in model space at z=0 and draw a crosshair at the corresponding (x,y,0) position in model space (or a slightly positive z-value to get the effect of a crosshair hovering slightly in front the GUI). Hope that helps.
Oh, and one thing that you should keep in mind: In most cases you want to cast the ray from the center point between your eyes. So you cannot simply cast from the center of either your left or right eye transformation matrix. You also may have to account for other offsets when using a neck model. - obzenExpert ProtegeUsually two ways of doing things.
1) math ray-cast through your world. using for example, gluUnproject() utility function. This depends on your game engine, and how comfortable you are with maths (vector maths, mostly, ray-tringle intersection, space partitioning, ect...).
2) render your object / surface flat-shaded, no aliasing, no nothing, no transparency, with a unique RGB colour. Then read back the colour and z value from the back buffer / render target to determine what object has been hit, and at what depth.
2) is more generic, but a bit finicky. 2) also can give you a rough 'beam' ray cast, if for example, you reduce the resolution of the back buffer, or get a patch of pixels instead of a single pixel.
1) is generally the method preferred, but requires very good math skills. It's also CPU-bound. - owenwpExpert ProtegeYour option 2 is far more CPU bound, as it requires copying the backbuffer and depth buffer into CPU accessible memory (in directx9 this is not even allowed for the depth buffer), then iterating over every pixel. Unless you want to do it in a compute shader, that is very inefficient, and will cause a tremendous pipeline stall that could increase your latency by as much as a full frame if you are not careful.
Ray casting is not as hard as it sounds, and if your game has any sort of physics simulation you should already have a way of doing it. If not, there are plenty of resources on google for ray-triangle intersection testing. Doing a first pass of testing your ray against axis aligned bounding boxes before testing individual triangles on a given object will give you good performance.
If you are working in Unity, just attach a collider component on anything you want to test and use Physics.Raycast, or just call Raycast on the collider itself if you only want to test one object.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 8 months ago
- 5 months ago
- 3 months ago