Jump to content

Closest face between meshes


touslecoq
 Share

Recommended Posts

Hey guys I am trying to figure something out.

I have a scene with a handful of identically sized boxes that can be moved around and rotated independently.

When I move a box I can check which other box is closest to it and whether it is "in range" (ie distance is less than a set value) by checking distance between currentBox.position and each boxes.position.  If they are in range then I can highlight it somehow (currently I set .visibility on both boxes to 0.5).  This works quite well.  When boxes are moved out of range again they become unhighlighted (ie .visibility=1 is set to 1);

However I want to make it more specific and highlight the specific face on each box that are closest.  Context: this is a first step to creating a kind of "snap-to" function that will allow boxes to be snapped together without needing user to accurately rotate and align the boxes.  So user moves box A within range of box B, the closest faces are highlighted and then when mouse button is released they snap together with Box A translating automatically to align with Box B.

I started by adapting the boxes at point of creation to contain subMeshes for each side.  The intention here was to use the method above but instead of iterating through each box in my boxes array, was to iterate through each subMesh in each box in my array and apply the same logic to determine their distance.  However the snag ... subMesh has no ".position" member so comparing currentBox.subMesh.position with boxes[j].subMesh[k].position does not work.

I have a sinking feeling I am going to have to compare the sets of vertices that make up each face/side of each  box.  Not only is this more comparisons to perform on each move of the mouse and therefore a potential performance hit, but it also leads to some pretty complicated logic.  In other words what is the criteria for nearest face .. I can't say "if the four vertices outlining a box side are all closes to the four vertices on a particular side of my currentBox" as if the box faces are not parallel you could find that the "nearest face" actually has some of its vertices that are further away from currentBox face than some other faces.

I'll try and work up a quick playground to demonstrate what I mean but while I do, has anyone done this before?  I cannot find anything on search her or google but it seems that it must have been thought about before?

Thanks in advance,

Richard

Link to comment
Share on other sites

Adam, thanks for quick response.

That could work although would need to work out where to cast the ray from/to.

If you cast from currentBox.position (ie centre of selected box) and it intersects another box and find the shortest distance to point of intersection (and choose that face) would that always be the "nearest face" from a snap-to perspective. For example if 2 boxes were aligned on 2 axis but offset on the third.  The 2 parallel axis would be the obvious ones to snap to from the users point of view yet the ray could cast through the perpendicular face instead.  It needs to be intuitive to the user, but I will play with that idea though.

My other thought was to write a function to calculate the centre position of each face/submesh based on its vertices and do the comparisons that way.  Compare distance between each face on currentBox (or cast the ray from that centre point).  Its just in the interest of the app remaining fluid I want to limit the number of calculations its performing on mouse move as much as possible.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...