### How to snap an object to another precedurally?

1483   4   0
Member
299 posts
Joined: Aug. 2019
Offline
Say I have a cone and a sphere:

I'd like to snap the cone to the closest point on the sphere, along the direction {0, -1, 0}. Like this:

I know Ray SOP, but it snaps every point to the sphere:

I'd like to keep the cone as a cone. Is there a node to do this procedurally?

Attachments:
Screenshot 2022-01-01 092845.png (369.2 KB)
Screenshot 2022-01-01 092857.png (316.5 KB)
Screenshot 2022-01-01 092911.png (575.5 KB)

Member
814 posts
Joined: Feb. 2016
Offline
Check "Match Size" Sop
Edited by Andr - Jan. 1, 2022 02:00:27
Member
299 posts
Joined: Aug. 2019
Offline
Andr
Check "Match Size" Sop

In this case Match Size SOP works, but I'd like to know how to make it more generic. For example when the ray I cast doesn't align with the world axes (e.g. {0.811, 0.32, 0.48}). I'm trying to make something just like Ray but only ray the closest point, and translate the rest of the geo accordingly. Is it possible?
Edited by raincole - Jan. 2, 2022 03:22:21
Member
3791 posts
Joined: Feb. 2012
Offline
Hi,

You can do it generically like this:

Find nearest point wrangle code:

```int pt = nearpoint ( 1, @P );
vector p = point ( 1, "P", pt );

@dist = distance2 ( @P, p );
i@nearpt = pt;
```

Sort by dist attribute.

Move wrangle code:

```int pt = point ( 1, "nearpt", 0 );

vector p0 = point ( 1, "P", 0 );
vector p1 = point ( 2, "P", pt );

@P += p1 - p0;
```
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]

patreon.com/animatrix | vimeo.com/animatrix3d
Member
299 posts
Joined: Aug. 2019
Offline
animatrix_
Hi,

You can do it generically like this:

It's really informative. Thank you very much!