In search of a ray pick example!
Knight Chat XHey All, Anyone got a working VB.Net example of Ray Picking or 3D mesh object intersection? Need to know how to perform intersection testing on a mesh object to enable mouse interaction to know when an object has been clicked on, such as for first person shooter, so I can know where to fire a projectile, or to click on objects and interact with items in a world. It is for 3D world. I've found one C++ example here: [url]http://www.mvps.org/directx/articles/improved_ray_picking.htm[/url] There is also an example in a book called Introduction to 3D Game Programming which is specific to ray picking using a bounding sphere, so far my attempts at converting those samples to VB.Net failed. [:(]
Almar JolingWell, the above one + the C++ one included in the SDK, are the best you can get, just make sure you transform your ray to the object "world matrix" of the current/previous frame so you can check for a hit. That's the real important thing. I only have C++ code, can't help you on it in VB :). I think someone might have done it too on the www.gpwiki.org site.
Eric ColemanI have some old VB code here http://www.strategon.com/dark/index.asp?page=mouseray
Knight Chat X Thanks guys, I'm looking at both links now. If I still can't get it going I might post a follow up with source code on the failed conversions incase there's anything that's obvious someone could point out that's causing it to not work.
Knight Chat X[B)] Well guys, I couldn't get any of the method's to work in VB.Net and DirectX 9.0c at all but they do work with VB6 and DirectX 8.1, so I'm posting a zip file that contains the main code snippets I've found online and tried converting using the different method's here: Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/Knight Chat X/200412322415_5 Ray Intersection Code Snippets.zip"][img]icon_paperclip.gif[/img]5 Ray Intersection Code Snippets.zip[/url]<br>9.02 KB Forgot to add this one, this method uses Unprojection and IntersectionInformation with a mesh object. Download Attachment: [url="http://www.vbgamer.com/msgboard/uploaded/Knight Chat X/2004123225129_Ray Intersection Unprojected.txt"][img]icon_paperclip.gif[/img]Ray Intersection Unprojected.txt[/url]<br>1.76 KB Maybe someone can see why none of those examples are working and post a solution because I doubt I'm the onlyone running into problems like this.
Eric ColemanWell, if they work in VB6/DX8 and not in VB.NET/DX9, then I'd say the problem isn't with this ray picking code, so the bug must be somewhere else in the VB.NET and DX9 code. I know I'm stating the obvious, but I can't see why the algorithms would work in one case and nother the other. The idea is pretty much the same, you simply take a line segment extending through the view frustum and do the inverse transformations to get the line segment into the coordinate system of whatever object you're trying to intersect. The only tip that I can think of right now is to check to make sure you're using the correct matrices.
cbx
quote:
Originally posted by Knight Chat X
Hey All, Anyone got a working VB.Net example of Ray Picking or 3D mesh object intersection? Need to know how to perform intersection testing on a mesh object to enable mouse interaction to know when an object has been clicked on, such as for first person shooter, so I can know where to fire a projectile, or to click on objects and interact with items in a world. It is for 3D world. I've found one C++ example here: [url]http://www.mvps.org/directx/articles/improved_ray_picking.htm[/url] There is also an example in a book called Introduction to 3D Game Programming which is specific to ray picking using a bounding sphere, so far my attempts at converting those samples to VB.Net failed. [:(]
Have you tried using the Mesh.Intersect method for D3D Mesh object in DX9? one of the overloaded intersect methods is Public Function Intersect( _ ByVal rayPos As Vector3, _ ByVal rayDir As Vector3, _ ByRef closestHit As IntersectInformation _ ) As Boolean This method will return the falowing information ... Dist: Retrieves or sets the distance along the ray where the intersection occurred. FaceIndex: Retrieves or sets the index of the triangle that intersected the ray. U: Retrieves or sets the barycentric coordinate within the triangle where the ray intersects. V: Retrieves or sets the barycentric coordinate within the triangle where the ray intersects. There is also a number of usfull methods contained in Direct3D.Geometry .... BoxBoundProbe: Determines whether a ray intersects the volume of a bounding box. ComputeBoundingBox: Computes a coordinate axis-oriented bounding box. ComputeBoundingSphere: Computes a bounding sphere for a mesh. DegreeToRadian: Converts degrees to radians. FresnelTerm: Calculates the Fresnel term. IntersectTri: Computes the intersection of a ray and a triangle. OptimizeFaces: Optimizes a set of faces. ptimizeVertices: Optimizes a set of vertices. RadianToDegree: Converts radians to degrees. SphereBoundProbe: Determines whether a ray intersects the volume of a sphere's bounding box.