Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

Get hit face from raycast
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=243
Page 1 of 2

Author:  frezned [ Tue Jul 05, 2011 12:25 pm ]
Post subject:  Get hit face from raycast

Is there a quick way to determine which face of a voxel was hit by a given raycast?

Just getting to grips with polyvox with a simple voxel model editor. If I were in "insert voxel" mode, I'd want to add a voxel adjacent to the clicked face - add a block in (x, y+1, z) if it was the top face, a block at (x-1, y, z) if it was the left face, etc.

Seems like this wouldn't be an unusual problem, so I figured I'd put a quick post up here. See if it was something that's been solved already before I get too deep into the math!

Author:  David Williams [ Tue Jul 05, 2011 7:03 pm ]
Post subject:  Re: Get hit face from raycast

That feature isn't implemented at the moment. Part of the reason is that the raycast should be useful for the smooth volumes as well as the cubic ones, and in this case there is no 'face' as such.

I think a solution would be to keep track of the previous voxel as well as the current voxel when casting the ray. This previous voxel would be also be returned in the result, and by comparing the previous voxel with the current one you could work out which face you had come though.

You probably want to modify the Raycast::doRaycast() function to handle this, but if you can't figure it out then let me know and I can add it for you.

Author:  frezned [ Wed Jul 06, 2011 1:56 am ]
Post subject:  Re: Get hit face from raycast

Oh, great! I was going to use the face to determine the previous voxel anyway, so this is an even better approach.

Thanks!

Author:  beyzend [ Wed Jul 06, 2011 2:34 am ]
Post subject:  Re: Get hit face from raycast

If you use another library which returns only the intersecting point, you can do something like figure out the direction of the ray, then compare against the normal of the two faces for the two cases of facing the ray casting point or facing away from it. How do get the normal of the faces? You need to hash the intersecting point into a voxel, then get two other points on this voxel to form a face and a normal. We only want the case where the casting ray and the face normal face away from each other. This only works if you assume that you never ray cast from inside a solid voxel.

I do something like this right now but I'm comparing against 6 faces, which is redundant.

BTW: to compute the points on the face, all you need is the intersection point, a vector V from the intersection point to the cube center (hashed cube center from intersection point), and the parallel component of V with respect to the normal of the cube (normal here is simply the Y axis). There are other ways to get the points.

Author:  David Williams [ Thu Jul 07, 2011 9:55 pm ]
Post subject:  Re: Get hit face from raycast

I meant to say, if you do implement this then we'll be happy to add the changes to PolyVox. Let us know of you have any problems.

Author:  Sceptrix [ Mon Sep 19, 2011 1:40 am ]
Post subject:  Re: Get hit face from raycast

I implemented this by adding a "previousVoxel" vector3d to the result, how can I contribute it? Branch git, post patch, attach files? I haven't been on the forums for long, my apologies if there's a well defined way to submit code changes that I missed.

Author:  David Williams [ Mon Sep 19, 2011 5:01 pm ]
Post subject:  Re: Get hit face from raycast

Great, I think a patch would probably be easiest. There may be a Git-specific solution but to be honest I've got a lot to learn about Git. So just create the patch and attach it here in the forum.

Author:  Sceptrix [ Mon Sep 19, 2011 8:17 pm ]
Post subject:  Re: Get hit face from raycast

Here you go. Great job on the library btw! I hope to have a demo for the showcase soon :D

Attachments:
Raycast.inl.patch [132 Bytes]
Downloaded 226 times
Raycast.h.patch [41 Bytes]
Downloaded 238 times

Author:  David Williams [ Mon Sep 19, 2011 10:20 pm ]
Post subject:  Re: Get hit face from raycast

I'm sorry, but it turns out I don't know enough about Git to apply these. Were they created using the 'git format-patch' command? I placed them in the same folder as Raycast.h and Raycast.inl and tried 'git apply Raycast.h.patch' but it just tells me 'error: no changes'. Do you know if that's the right procedure?

Otherwise just give me the complete files and I'll overwrite my local copy.

Author:  Sceptrix [ Tue Sep 20, 2011 6:28 am ]
Post subject:  Re: Get hit face from raycast

Um I just used winmerge to create the patch as I don't know much about git's patching system either. I've attached the full files, there's only 3 lines changed.

Attachments:
Raycast.h [4.97 KiB]
Downloaded 228 times

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/