Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / [How-To] 3D Raycasting/Picking From Screen Cursor

Author
Message
Clonkex
Forum Vice President
16
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 3rd Oct 2013 14:01 Edited at: 3rd Oct 2013 14:05
Hi all,

On Lee's FPSC Reloaded blog, he asked for some help regarding picking a point on a terrain with the cursor, and since I also wanted to know this, I started experimenting.

At first I tried using the camera's FOV and calculating the angle, which theoretically should work but I must have been doing something wrong because I just couldn't get it to work. I'm sure it's a valid method, though...

So I starting researching it on the internet. In the end I decided it would be simplest to use the 3D Maths commands, however complicated. And it took literally a day and half of more-or-less constant work and a Stack Overflow question before I finally got it to work. Sure, I could have asked on these forums and someone would probably have been able to give me code right off the bat, but I wanted to do it myself.

And I did. It works. And I have a much better understanding of how things are rendered.

Following is the source code, in case anyone wants it. It requires Sparky's DLL (I did NOT know Sparky is Paul Johnston...very cool that he released the source code) if you want to run it as it is, but it can be used with any raycasting plugin (such as Dark Dynamix):



If anyone doesn't understand it and really wants to use it I'm happy to provide detailed tutorials, or if you prefer just show you which bits to use

Oh, and here's the model file that it loads as a test. It's .x format, so just stick it in a text editor (notepad) and save as .x



Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2013 15:11
I swear you can just use Pick Object, multiply the pick vector by 2 and add camera position and this is your mouse cursor position for the raycast.

"Get in the Van!" - Van B
LeeBamber
TGC Lead Developer
26
Years of Service
User Offline
Joined: 21st Jan 2000
Location: England
Posted: 3rd Oct 2013 16:55 Edited at: 3rd Oct 2013 16:56
Care to prove it

Hogging the awesome since 1999
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 3rd Oct 2013 17:10 Edited at: 3rd Oct 2013 17:26
Quote: "Care to prove it"


Proved


Note: just replace object 1 with whatever coz I used his mesh

"Get in the Van!" - Van B
Clonkex
Forum Vice President
16
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 4th Oct 2013 04:18 Edited at: 4th Oct 2013 05:01
No, your method will not work. If you move your camera inside the bounding box of the terrain it will not hit anything, since pick object picks the bounding box.

Hooray my method is still better

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2013 11:15
Oh... We're dealing with terrain. Well I've got another way then... give me a moment

"Get in the Van!" - Van B
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Oct 2013 11:35 Edited at: 4th Oct 2013 11:43
I've already done this in my engine last year.

You would not be able to run this straight off the bat, due to it requiring my trigger system, but the picking procedure highlighted below.

It is not as fancy as Clonex's solution nor the most elaborate feature given much attention yet as I reminded myself in the code comment ('Not pretty'), but it works:



It simply uses something as a projectile that moves until it is underneath the terrain by a given measurement, say a few inches.

When I get round to optimizing it, I will use a reverse-factorial recursion loop to reduce the number of iterations required for long distance picking.

It is a bit more OOP because I've treated terrain picking as something entities and AI can perform aswell.

Full terrain picking library:



Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2013 14:29 Edited at: 4th Oct 2013 16:08
Aha I'm back with my results

@Clonkex

This is the math equivalent of Pick Screen:


So all of that can be replaced with:


Which btw is 8 times as fast

Though what's interesting is they produce nearly the same results, so I have no clue which is correct. I'm gonna do some testing with math to see where the inaccuracies are with Pick Screen and you code.

"Get in the Van!" - Van B
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2013 15:44 Edited at: 4th Oct 2013 16:05
I'm back again

After some playing I reworked your method to accurately emulated Pick Screen:


Or... (only slightly faster)


And actually using it:


Quote: "Hooray my method is still better "


Not anymore

Note: Pick Screen method I posted above is still the fastest method

"Get in the Van!" - Van B
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 4th Oct 2013 22:35
Excellent! I have no idea how it works, but it works. It looks like a more mathematical equivalent of the Sparky's Collision example. Now all that would need to be done is convert the Blitz Terrain into the 3D object, which is a simple task.

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Oct 2013 23:56 Edited at: 5th Oct 2013 00:26
Well I tested Pick Screen (the method above) on Blitz Terrain + Sparky's Collision and everything is working fine. Success

Also note: You can use Pick Object on a Blitz Terrain Object

P.S. I've never used Blitz Terrain till just now

"Get in the Van!" - Van B
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 5th Oct 2013 03:04 Edited at: 5th Oct 2013 03:06
I do not know how fantastic pick object is going to be on a dynamic terrain frequently updated by terraform events and effects; with all of those vertices to work with. FPSCR will have dynamic terrains. Pick object would require a new DBPRO object to be generated every time something happens to the terrain.

But it all depends how often the terrain will change. Perhaps breaking down the terrain into various objects might be more optimal; but this is all a wild guess; in the best case scenario the object generation would be swift; and pick object or your or Clonex's snippet would be fast.

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 5th Oct 2013 03:35
If that's the case use Pick Screen which is the fastest method to work out where there ray cast should go. The main collision is done by Sparky's so I would imagine you just update the object terrain (SC_UpdateObject) if it's been altered though I imagine you'd have to recreate the terrain object to do this anyway. If I had the full version I would look into this more to make it more effiecent. I can only guess at this moment.

"Get in the Van!" - Van B
Clonkex
Forum Vice President
16
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 5th Oct 2013 05:38 Edited at: 5th Oct 2013 05:56
Quote: "Not anymore"


Dammit!

Quote: "Which btw is 8 times as fast"


DAMMIT! Hhhh.....NOW what am I gonna do...I tested your pick screen method and it does work just as well as mine...hmm...how to get back at you for invalidating my work...

Can't believe I didn't look into Pick Screen to begin with...

Quote: "After some playing I reworked your method to accurately emulated Pick Screen"


Complicated....

Login to post a reply

Server time is: 2026-07-06 10:35:57
Your offset time is: 2026-07-06 10:35:57