touslecoq Posted March 4, 2016 Share Posted March 4, 2016 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 Quote Link to comment Share on other sites More sharing options...
touslecoq Posted March 4, 2016 Author Share Posted March 4, 2016 Example to highlight nearest whole box to the box being moved: http://www.babylonjs-playground.com/#2ILHWS#3 Note that contrary to my original post, the selected box (ie the one that is being moved around) is "highlighted" until mouse up rather than just when in range. The other box is only highlighted whilst in range. Quote Link to comment Share on other sites More sharing options...
adam Posted March 4, 2016 Share Posted March 4, 2016 My first thought is to cast a ray between each of the objects. If there is a hit, I believe you can easily get the face from the hit info. Quote Link to comment Share on other sites More sharing options...
adam Posted March 4, 2016 Share Posted March 4, 2016 Here is a PG that uses BABYLON.Ray: http://www.babylonjs-playground.com/#QM57B Quote Link to comment Share on other sites More sharing options...
touslecoq Posted March 4, 2016 Author Share Posted March 4, 2016 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. Quote Link to comment Share on other sites More sharing options...
touslecoq Posted March 4, 2016 Author Share Posted March 4, 2016 Thanks again - that will save me some hunting! Quote Link to comment Share on other sites More sharing options...
NasimiAsl Posted March 7, 2016 Share Posted March 7, 2016 i think you can find that with other way(no picking) with render target and depth material this is possible and i think it faster than picking Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.