• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Mesh Picking Accuracy Problem

1 post in this topic


I'm having some accuracy issues with my mesh picking code. The problem im getting is that sometimes a mesh behind another mesh will be selected, instead of the front-most mesh.

Just some quick background as to how my meshes have been created and rendered. My application essentially renders a bunch of beams and columns in a 3D model (CAD type application). The beams and columns are my meshes. The mesh is created at the world original with a standard length of 1000mm. I store a transformation matrix for each beam mesh (that i call the world transform matrix) that is passed to the shader. The shader uses the world transformation matrix to scale, rotate and translate the mesh into the correct world position.

Since my meshes vertices are not defined in the correct world positions (remember that each mesh is created at the origin with a standard length), I had to do something a bit different for my picking calculations. I essenitally multiplied my beam world transform matrix by the camera world-view-projection (WVP) matrix. I then use this combined matrix in my Vector3.Unproject method to determien the ray position and direction.

For the most part, my code works really well. However, there are a few beams that are not selected correctly. I have uploaded some images showing the problem. The image showing the picking working as it should can be found here ([url="http://img209.imageshack.us/img209/5971/correct.png"]correctIMG[/url]), the image showing incorrect behaviour can be found here ([url="http://img18.imageshack.us/img18/2397/incorrectm.png"]incorrectIMG[/url]). A beam that has been highlighted/selected is drawn in white. The white cross-hair denotes my mouse cursor location. Also see my picking code below (VB.Net).

Any help would be great. Thanks.

Private Sub HighlightBeam(ByVal e As Point)

If D3Ddev Is Nothing Then
End If

'local variables
Dim beam As elemental.Beam = Nothing
Dim prop As elemental.BeamProperty = Nothing
Dim selectedBeamID As Integer = Nothing
Dim IsHit As Boolean = False
Dim hitSuccess As Boolean = False
Dim WVP As Matrix = Nothing
Dim vp As Viewport = Me.Model.Camera.Viewport
Dim octNode As elemental.OctNode = Nothing
Dim beamMesh As Mesh = Nothing
Dim hitDistance As Single = 0.0F
Dim prevHitDistance As Single = Single.MaxValue

'perform hit tests for all objects contained in the model bounding box
For Each octNodeID In Me.Model.Octree.SelectedOctNodeList

'select the current octnode
octNode = Me.Model.Octree.OctNodeTable(octNodeID)

'do nothing if the octnode contains no beams
If octNode.BeamIDList Is Nothing OrElse octNode.BeamIDList.Count = 0 Then
Continue For
End If

'loop through beams contained in this octnode
For Each beamID In octNode.BeamIDList

'retrieve beam
beam = Me.Model.BeamTable(beamID)
prop = Me.Model.BeamPropertyTable(beam.BeamPropertyID)

'perform no action if beam is hidden
If Not beam.IsVisible Then
Continue For
End If

'get the beam mesh template.
If prop.SectionType = elemental.BeamSectionType.ISection Then
beamMesh = Me.Model.GetISectionMesh(prop.ID, 1)
ElseIf prop.SectionType = elemental.BeamSectionType.CircularHollow Then
beamMesh = Me.Model.GetCHSMesh(prop.ID, Me.Model.Settings.Beams.CircleFacets, Me.Model.Settings.Beams.CurveSegments)
ElseIf prop.SectionType = elemental.BeamSectionType.LipChannel Then
beamMesh = Me.Model.GetLipChannelMesh(prop.ID, 1)
ElseIf prop.SectionType = elemental.BeamSectionType.LSection Then
beamMesh = Me.Model.GetLSectionMesh(prop.ID, 1)
ElseIf prop.SectionType = elemental.BeamSectionType.CircularSolid Then
beamMesh = Me.Model.GetCircularSolidMesh(prop.ID, Me.Model.Settings.Beams.CircleFacets, Me.Model.Settings.Beams.CurveSegments)
'dispose unmanaged resources
End If

'direction of the ray.
Dim rayPos As New Vector3(e.X, e.Y, 0.0F)
Dim rayDir As New Vector3(e.X, e.Y, 1.0F)

'world-view-projection transform
WVP = beam.WorldTransform * Me.Model.Camera.WVP

'ray data
rayPos = Vector3.Unproject(rayPos, vp.X, vp.Y, vp.Width, vp.Height, vp.MinZ, vp.MaxZ, WVP)
rayDir = Vector3.Unproject(rayDir, vp.X, vp.Y, vp.Width, vp.Height, vp.MinZ, vp.MaxZ, WVP)
rayDir = Vector3.Subtract(rayDir, rayPos)
rayDir = Vector3.Normalize(rayDir)

'hit test
IsHit = beamMesh.Intersects(New Ray(rayPos, rayDir), hitDistance)
'on hit occurence, record the selected beam ID
If IsHit Then

'record that a hit was observed
hitSuccess = True

'if beam is closer than the previous hit success, select this beam instead
If hitDistance < prevHitDistance Then

'track hit distance for all selected members to determine which one is in front of the other
prevHitDistance = hitDistance
selectedBeamID = beam.ID
End If
End If

'dispose unmanaged resources

Next beamID

Next octNodeID

'on hit success, update selected beams
If hitSuccess Then

'unhighlight the last highlighted beam
If Highlight_LastHighlightedBeamID <> 0 Then
Me.Model.BeamTable(Highlight_LastHighlightedBeamID).IsHighlighted = False
End If

'update selected beam table
If Not Me.Model.BeamTable(selectedBeamID).IsHighlighted Then
Highlight_LastHighlightedBeamID = selectedBeamID
Me.Model.BeamTable(selectedBeamID).IsHighlighted = True
End If

'unhighlight the last highlighted beam
If Highlight_LastHighlightedBeamID <> 0 Then
Me.Model.BeamTable(Highlight_LastHighlightedBeamID).IsHighlighted = False
Highlight_LastHighlightedBeamID = 0
End If
End If
End Sub

Share this post

Link to post
Share on other sites
Found the problem, although i don't really understand why it was a problem.

Basically i deleted the line : rayDir = Vector3.Normalize(rayDir)

So i don't normalized the ray direction at all anymore.

Now it works perfectly. Go figure.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0