Notifications
Clear all

[Closed] Offsetting a vector by another vector?

My maths isn’t great, and I’m not even doing a good job at trying to explain my problem, but here we go:

The problem is this; I have a surface normal and point in space. I can calculate the vector between the two, but what I want to do is use this same offset relative to another surface normal.

So, say I have a point floating in an arbitrary position above a polygon, and I have another polygon elsewhere, how can I compute the position of that point relative to second polygon?

Thanks

2 Replies

This is very simple to solve using transform matrices.

First, you want to construct a matrix from the first triangle. Then you want to get coordinates of the point in space local to that matrix. Then, you want to construct a matrix from the second triangle. Then multiply the local coordinates you calculated from the first triangle, by the matrix of the second triangle. You will be left with the world-space coordinates of the point with the same offset from the second triangle that it had from the first one.

To calculate a matrix, you need 3 vectors (up, right and forward). You can use the normalized vector between vertex 1 and vertex 2 of the triangle as your forward vector, and the cross product between the surface normal and forward vector as your right vector, and then the surface normal as the up vector. Use the center of the triangle as the position value of the matrix. Repeat for the second triangle to get both of the necessary matrices.

To get the local coordinates of a world space point relative to your first triangle, simply multiply the world-space coordinate of your point by the inverse of your first triangle’s transform matrix.

Here’s some code to help you you. If you have an empty scene with a point (named: Point001) and 2 triangles (named: Triangle001 and Triangle002, both editable meshes) and then run the script, you’ll see a new point created relative to Triangle002 that has the same offset that Point001 has to Triangle001:



(
	tri1 = $Triangle001
	tri2 = $Triangle002
	p = $Point001

	t1_v1 = getvert tri1 1
	t1_v2 = getvert tri1 2
	t1_v3 = getvert tri1 3

	t2_v1 = getvert tri2 1
	t2_v2 = getvert tri2 2
	t2_v3 = getvert tri2 3

	t1_n1 = getfacenormal tri1 1
	t2_n1 = getfacenormal tri2 1
	
	t1_center = (t1_v1 + t1_v2 + t1_v3)/3.0
	t2_center = (t2_v1 + t2_v2 + t2_v3)/3.0

	t1_forward = normalize (t1_v2 - t1_v1)
	t1_right = (cross t1_n1 t1_forward)
	
	t2_forward = normalize (t2_v2 - t2_v1)
	t2_right = (cross t2_n1 t2_forward)
	
	t1_mtx = matrix3 t1_forward t1_right t1_n1 t1_center
	t2_mtx = matrix3 t2_forward t2_right t2_n1 t2_center
	
	p_local = p.transform * (inverse t1_mtx)
	p2 = point name:"Point002" wirecolor:yellow	
	p2.transform = p_local * t2_mtx
	
	select p2
)

This code calculates the offset using the full transform of the point. If you just want to calculate position offset (not scale or rotation), simply do the matrix multiplications on the position value instead of a full node transform.

The same principles can be applied to quads or n-gons too.

Hey thanks, I reached much the same conclusion in the end.

I was struggling because I was using matrixfromnormal, which was giving unpredictable results. I guess that’s because Max must use some sort of internal method for creating the additional vectors, to base the matrix on. As soon as I specified the other vertices it all fell into place.

Incidentally, you can use meshop.getfacecenter for your face center instead of computing it yourself…

Thanks.