1
\$\begingroup\$

I am trying to make a modular vehicle editor similar to Kerbal Space Program. The user selects an initial vehicle part which is spawned in the center of the "work area". To add another part to the initial one, the user clicks and drags a part from a list onto the "work area" and then drags that part towards the initial one. When a node on the new part is close enough to a node on the initial part, the new part snaps onto the initial one. The user can then click to confirm the connection or drag it somewhere else. For a visual example.

The spawning of the parts I have no problem with.

For moving the parts in the environment I have an invisible plane that is parallel to the camera and rotates to always face the camera. I then draw a ray from the mouse to the plane and update the parts position to the location where the ray hits the plane.

When the new part is in the correct position I will add I make it the child object of the part I want to connect it to.

The problem occurs with positioning the new part relative to the part I want to attach it to. I'd like to make it like Kerbal Space Program where when the connecting node of the new part is within range of the connecting node of the part I'm attaching to, the new part's position snaps to the part I'm attaching to.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ So, when the two nodes spheres collide, you want to snap them together? If the sphere nodes are placed on the edges, couldn't you just move the part in motion so that its sphere was in the same position as the stationary sphere? \$\endgroup\$ – MichaelHouse Jul 28 '13 at 1:26
  • \$\begingroup\$ @Byte56 The part should snap into position when the red node is within snapping range of the orange node. The green arc is that range and wouldn't be visible to the user. The problem with setting the orange node's position to that of the red one is the orange node is the child of the New Part. As a child it moves relative to it's parent, the New Part, so if it moves the parent stays put where as if the parent moves the child moves with it. \$\endgroup\$ – wolfadex Jul 28 '13 at 3:27
1
\$\begingroup\$

If your nodes are placed on the edges of your objects, that makes this a little easier. It's simple vector math for aligning the two objects.

enter image description here

You have two points, A and B. These points are the positions of your nodes, or points on the edges you want to snap together.

Simply calculate the difference in them. This difference is the amount you want to move your part so that A and B are the same point.

As you can see in the image above, the yellow line is the distance between the points and the red line is the amount you need to move the dark grey part to align them. These are the same lines.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.