# Detect surface using VEX

## Recommended Posts

Suppose I have a position (e.g. [100,200,300]) and directional vector (e.g. [1,0,0]), is there a way in VEX to fire a line trace/ray from that point along that vector and get the position of the first surface primitive that's hit?

Edited by localstarlight

##### Share on other sites
9 minutes ago, localstarlight said:

Suppose I have a position (e.g. [100,200,300]) and directional vector (e.g. [1,0,0]), is there a way in VEX to fire a line trace/ray from that point along that vector and get the position of the first surface primitive that's hit?

in SOPs: intersect()

in surface: rayhittest() or trace()

Just a friendly remainder, that it is generally recommended to use docs page before asking basic questions on a forum

• 1

##### Share on other sites

Great, thanks!

I actually did various searches trying to find the answer, but clearly I was not phrasing my question right to turn it up. I only ever post on forums as a last resort, but thanks for the reminder nonetheless

• 1

##### Share on other sites

I'm trying to use the first example you gave (SOPs: intersect) but can't get it to work for some reason.

Here's the VEX I'm using:

```vector direction = @N * -100;

vector new_position;

float u,v;

i@intersected_prim = intersect(0, v@P, direction, new_position, u, v);

@P = new_position;```

I'm trying to shoot a trace in the opposite direction to the point normal. Essentially I have a tube mesh surrounding a more complicated shape, and I'm trying to project the points of the mesh inwards onto the more complicated geometry.

The attribute @intersected_prim is returning a prim number (which I assume must be the id of a prim on the more complicated inner geo), but new_position doesn't seem to be getting the location of the intersection, and so @P remains unchanged. This is my first time using a VEX function that requires passing a variable by reference, so perhaps I'm doing something wrong with that?

##### Share on other sites

Since you're tracing over second input's geometry (?), tiny correction is needed (note first argument);

`i@intersected_prim = intersect(1, v@P, direction, new_position, u, v);`

I would also recommend initializing new_position, u, v for extra safeness (it used to be mandatory, not sure now).

hth,

ps sorry also for previous criticism, I didn't notice that naming convention isn't actually very clear in that case.

##### Share on other sites

take out the v in v@P when calling intersect() ?

##### Share on other sites

Hey symek, that did the trick. I had the input listed wrong. Thanks so much for your help!

It works without initialising those variables, but good to know that's something to look out for.

No apology necessary, I understand it's a bit frustrating when people treat forums as Google  For some reason my searching only turned up trace(), which doesn't return the position of intersection, so I wasn't sure it was something VEX actually had. Thanks again for your help.

• 1

##### Share on other sites

slightly off-topic:

On 8/8/2017 at 1:41 AM, Noobini said:

take out the v in v@P when calling intersect() ?

No keep it! casting all the variables for the win  (and you are forced to cast it when using v@opinput1_P),

so I just make it a habit to cast everything, it also avoids creating float attributes, when you really only need integers.

• 1

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account