RayCasting Explained - source samples included

Ok… so i only recently got this working and im sure there is some optomization that can be done. but I promised on another thread to post a vid and source on this once it was working :slight_smile:

This video walks through the theory and specifics of raycasting.

mCSvtUvlt5I

This is the code utils i created. all code is using LWJGL.

UnprojectUtil
http://pastebin.com/CLduy9gk

RayCastUtil
http://pastebin.com/AkdX4hba

there are three main steps in raycasting

Unproject the mouse coords for the 0 and 1 depth values
Calculate intersects with a plane for the surface you want to test against
Calculate the area of the surface (as a triangle) and then of the intersect point using the same triangle points… if the sum of the triangles - the original area of the full triangle == 0 (or < 0.00001) then you are inside the triangle.

EDIT updated to include video link :slight_smile:
EDIT corrected typo :slight_smile:

wVh0NMVP-uQ

Revised code
http://pastebin.com/XeVwn0UQ

UPDATED SAMPLE CODE.
This paste bin has my fully working Raycastingutil

http://pastebin.com/fpADUAiW

looks like the vid for this post is finally available via youtube.

Freaking awesome stuff man! ;D

The video was very clear and you learned away the theory on a great way! The only thing I would pick about is that not everybody is using GLU (or deprecated functionality of OpenGL).

Other than that, great work!

Awesome, I’ll take a look at this later tonight! Thank you! :slight_smile:

Replying just for the sake of bookmarking.

Probably might help me understand the logic behind Notch’s Prelude of the Chambered.

Prelude of the Chambered doesn’t use RayCasting, but rather Perspective-Based Projection.

EDIT: Really? Well, awkward…

Off topic, but I’m getting sick of people saying welp. Please, no more. Its not funny.

Ah, I hate it when simple typos like this can make one angry, especially when viewing/typing on phone. I’ll go ahead and fix it, for the sake of it.

Just a heads up to anyone following this code.

I’ve been evolving my implementation to support “what if” situations… ie… what if the camera is rotated, what if the model is rotated… ect…
This has not resulted in a large change to the original code i submitted but a few minor tweaks in places to add this support.

Once the revised version of the utility is done I will post it again here for anyone to pick a part and use as they will.

cheers.
j

[Update]

I finally got the full functionality of my raycast util working last night.

Expect an updated video and sample code later this evening.

j.

updated the paste bin with new sample code.

new code adjusts for model rotation and normal correction in the math calculation.

Great job man!

I remember pondering on this, and coming up with the first step, but I never knew enough about collisions to find the second and third step! Thanks for solidifying it for me!

Will help for when I venture into 3D!

-wes