Jump to content

Align Mesh/Snap-to


touslecoq
 Share

Recommended Posts

Hi guys

So I've been wrestling with a problem for a little while and hoping to cast it out there for your thoughts.

As part of an app I am developing i wanted to introduce snap-to functionality such that when a mesh was dragged within certain distance of another, the dragged mesh would snap into same orientation as the other - basically 2 "allowed" faces would point at each other and the mesh would be positioned so these faces were adjacent.

As a pre-cursor to this I created the playground scene below to prove the concept.  Basically this creates 2 static boxes, the second rotated randomnly in relation to the first.  When you click the mouse anywhere in the scene it should rotate the first box to match the orientation of the second and translate it into same position for easy validation that the alignment it has worked.

The approach is a function that takes 4 parameters (ignore the fifth for now); mesh1, mesh 1 face number, mesh2, mesh 2 face number.  The function calculates the normal of those faces, the angle between the normals and the rotation axis and rotates mesh1 so that the faces align and then moves the mesh1 to mesh2.position.  So far so good and this seems to be working ok.  Click the scene and the box faces align and box 1 moves to same position as box 2.  

Obviously although the faces are now pointing in the same direction they are likely to be skewed around their now common normal/axis.  So the intention was to call the function a second time this time specifying 2 new faces, perpendicular to the original 2 faces that are now pointing in same direction.  This should calculate the angle between those 2 new faces and the axis around which to rotate (which should be the normal to the original 2 faces).

 This is where it goes wrong - when line 177 is uncommented to call the function a second time it causes the boxes to go out of alignment and repeated clicking causes it to randomnly point in different directions.  In theory it should correct itself on each click but it does not.

Any thoughts on whether the logic is sound and on the execution?

I am re-rendering the scene after the first alignment call, but I do wonder whether it is not fully re-computed the vertex data and therefore the normals used to calculate the new angle of rotation/axis of rotation are wrong.

Thoughts?

http://www.babylonjs-playground.com/#1JX3ZX#83

 

 

Link to comment
Share on other sites

Hi it is worth checking out the rotationFromAxis method http://doc.babylonjs.com/overviews/How_Rotations_and_Translations_Work and doing a search in the forum and playgrounds for this term.

Seems like an interesting problem. What are you thinking of in your actual use case?

From

  1. all my meshes will be boxes    to
  2. my meshes can be any type of mesh and for any two meshes an arbitrary face (F) will be chosen on  mesh A and arbitrary face (G) on B. Lying on face (F) there will be a vector V1 and on face (G) there will be a vector V2. Mesh A will be rotated so faces (F) and (G) will be parallel and vectors V1 and V2 have the same orientation.
  3. Somewhere between 1 and 2.

 

 

Link to comment
Share on other sites

Hi John

I've just been playing with rotation from Axis actually and struggled to get it to work how I want.  Will keep plugging.

I did find that setting mesh1.rotation = mesh2.rotation actually meets the brief of this proof of concept (in a lot less code than my playground too).  However next stage is to be able to get them to face each other rather than face the same way - which basically means rotating 180 degrees around an axis perpendicular to the faces I want to mate - however that was the bit I couldn't get to work in my version, so not out of the woods yet. 

The end use case is probably closer to 2.

Think lego - the idea is different shaped building blocks can be mated together but only in a certain way and to ease the process of doing so I want some snap-to function.  The blocks will not all be the same size but their mateable faces will be.

I have proto-typed various "proximity" approaches to varying degrees of succes - with a nearest faces changing colour or border changing color when in range of the dragged object.

Like lego, only certain faces will be "mateable" with other faces - ie like male/female, plug/socket.  Haven't quite decided on how to build those rules in yet but have a few ideas.  I also want to build in some collision detection so that meshes cannot overlap, but not full physics.

Then finally once objects are mated I need to decide whether they should be merged or not - ideally they can be un-mated later and if there are several components making an assembly, then unmating two would end up with 2 assemblies.

Anyway I am rambling - lots to do but this re-orientation has proven to be the most difficult so far - although feels like it shouldn't be!

Link to comment
Share on other sites

Hi guys!  Hey T... do you know about this topic:  http://www.html5gamedevs.com/topic/20751-aabb-snapping-in-3d/

?  Might be applicable.  @jacquesr has done quite a bit of snapping work... but if you are enjoying yourself, keep on truckin'.  I sure don't want to ruin any fun.  Still, I thought you should know about Jacques' work. 

I love hearing about your adventures in this, @touslecoq.  I happen to like your rambling.  I was really reluctant to show you that other thread.  I hope it doesn't take away your aligning fun.  Jacques' bounding-box snapping might be more "coarse" than face snapping, so perhaps it is a different subject.

Link to comment
Share on other sites

Hey Wingy thanks - I hope I'm at least giving you guys some amusement :).

Thanks for the signpost to @jacquesr's work - there is a lot in there that will help.  At the moment in my demo I just set box1.position=box2.position in order to verify whether the rotational alignment has actually worked.  Jacque's snap to face will be much better for the finished product.

However sadly it is the the rotational snap that still gives me a headache and I couldn't see that in Jacque's thread.  

Tous

Link to comment
Share on other sites

Oh, it's far more than amusement.  For myself, at least, I'm watching and learning (and doing some drooling, too.)  Excellent code.  You're pretty good at vertices/normals operations.  Your experiments and narratives are helping me and many others.  Good stuff!

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...