projecting a vector onto a plane....

578 views
Skip to first unread message

s...@weacceptyou.com

unread,
Mar 21, 2016, 1:16:47 PM3/21/16
to Python Programming for Autodesk Maya
Hi there,

i am struggling to understand exactly how this works, but i think i have the building blocks for figuring this thing out.

so i have two vectors which effectively represent a plane that i want to project a third vector onto.

i have the perpendicular vector using cross product also. Can anyone tell me how easy it is to project the third vector onto the plane defined by the perpendicular vector.

is there a simple way to do this?

thanks alot for your help,
Sam

Joe Weidenbach

unread,
Mar 21, 2016, 2:57:17 PM3/21/16
to python_in...@googlegroups.com
Hi Sam,

If your three vectors are all perpendicular to each other, you can just take the dot product of your vector with each of the two vectors on the plane to get local coordinates, then multiply by a rotation matrix built from the axes.  Here's the method I use (If I'm starting with two vectors that aren't perpendicular).

We'll call our first two vectors v1 and v2, and the vector we want to project vProj. Also, I'm using pseudocode (I've been working in Eigen lately, so it's probably more similar to that than to pymel, but since I don't have a interpreter handy, this is the general idea for the math :)

zAxis = v1.cross( v2 ).normalized() # Perpendicular to both original vectors
yAxis = normal.cross( v1 ).normalized() # Perpendicular to the first vector and the normal
xAxis = v1.normalized()

# Now we can just project against x and y
projectedX = vProj.dot(xAxis)
projectedY = vProj.dot(yAxis)

localProjection= Vector( projectedX, projectedY, 0 )

matrix = [ xAxis[0], xAxis[1], xAxis[2], 0,
               yAxis[0], yAxis[1], yAxis[2], 0,
               zAxis[0], zAxis[1], zAxis[2], 0,
               0,           0,            0,           1 ]

finalProjection = matrix * localProjection


That should get you most of the way :)

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1dcdb40b-cb07-418c-be8d-9e85da3cad83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Mar 21, 2016, 4:03:04 PM3/21/16
to Python Programming for Autodesk Maya
thanks Joe for the help!

for some reason i thought it would be easier than this, maths isnt my strong suit but im gonna give this a go!

cheers,
Sam

Joe Weidenbach

unread,
Mar 21, 2016, 4:10:28 PM3/21/16
to python_in...@googlegroups.com
Now that I'm more awake, there's a potentially easier option as well.

if you dot your vector you want to project against that perpendicular vector, then add the negative perpendicular vector normalized multiplied by that dot product, you'd get the same result.

magnitudeFromPlane = vec.dot(perpVec)
toPlane = -(perpVec.normalized() * magnitudeFromPlane)
projectedVec = vec + toPlane

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
Mar 21, 2016, 4:54:13 PM3/21/16
to Python Programming for Autodesk Maya
cool thanks alot Joe !

Joe Weidenbach

unread,
Mar 21, 2016, 4:59:09 PM3/21/16
to Python Programming for Autodesk Maya
No problem sir!
On Tue, 22 Mar 2016 at 9:54 AM <s...@weacceptyou.com> wrote:
cool thanks alot Joe !


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

s...@weacceptyou.com

unread,
Mar 23, 2016, 5:50:36 PM3/23/16
to Python Programming for Autodesk Maya
sorry Joe there is one thing i wanted to ask you about this.

this works fine when the vectors originate from the origin (0,0,0) but if shift everthing to a new location, it projects the vector onto the plane as if it were at the origin.

do i need to do add the position of the vector origin to the vectors at the beginning, or is there maybe a way to make the vectors 'relative'

thanks,
Sam
Reply all
Reply to author
Forward
0 new messages