Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (111)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  RayCasting Explained - source samples included  (Read 2321 times)
0 Members and 1 Guest are viewing this topic.
Offline jmguillemette
« Posted 2013-12-09 07:21:13 »

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 Smiley


This video walks through the theory and specifics of raycasting.
<a href="http://www.youtube.com/v/mCSvtUvlt5I?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/mCSvtUvlt5I?version=3&amp;hl=en_US&amp;start=</a>


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 Smiley
EDIT corrected typo Smiley


<a href="http://www.youtube.com/v/wVh0NMVP-uQ?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/wVh0NMVP-uQ?version=3&amp;hl=en_US&amp;start=</a>



Revised code
http://pastebin.com/XeVwn0UQ


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


http://pastebin.com/fpADUAiW

-=Like a post.. give the author a medal!=-
Offline jmguillemette
« Reply #1 - Posted 2013-12-09 13:16:00 »

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

-=Like a post.. give the author a medal!=-
Offline Simn
« Reply #2 - Posted 2013-12-09 16:43:41 »

Freaking awesome stuff man!  Grin

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!

- Simn
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-12-09 23:01:28 »

Awesome, I'll take a look at this later tonight! Thank you! Smiley

Offline tom_mai78101
« Reply #4 - Posted 2013-12-31 02:01:41 »

Replying just for the sake of bookmarking.

Probably might help me understand the logic behind Notch's Prelude of the Chambered.
Offline kpars

JGO Wizard


Medals: 77
Projects: 4
Exp: 3 years


Extreme Typist.


« Reply #5 - Posted 2013-12-31 10:21:07 »

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

Offline tom_mai78101
« Reply #6 - Posted 2013-12-31 12:28:18 »

EDIT: Really? Well, awkward...
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-12-31 14:29:34 »

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

Offline tom_mai78101
« Reply #8 - Posted 2013-12-31 14:46:50 »

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.
Offline jmguillemette
« Reply #9 - Posted 2013-12-31 17:10:01 »

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

-=Like a post.. give the author a medal!=-
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jmguillemette
« Reply #10 - Posted 2014-01-07 16:15:41 »

[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.

-=Like a post.. give the author a medal!=-
Offline jmguillemette
« Reply #11 - Posted 2014-01-09 00:56:24 »

updated the paste bin with new sample code.

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


-=Like a post.. give the author a medal!=-
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #12 - Posted 2014-01-09 01:26:11 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!