[Python] Compute the Angle of an Edge Relative to World Space


#1

Hi,

Given 2 points in 3D Space (hence an edge), how do I compute its angle relative to the world space?
You can see an illustration of the problem here:

I think this is some sort of trigonometry voodoo but I can’t really figure it out. The cos theta gives a different angle.

Is there a way around this?

Thank you for looking at my problem.


#2

I think u should check out the dot product and how to find the angle between two vectors using that. it should be pretty simple


#3

#4

Hi @cur10us

Thanks for the response. But correct me if I’m wrong the dot product way is the cos (theta) way I mentioned earlier. Sorry if this is confusing, but is what I meant by the cos (theta) way - https://www.youtube.com/watch?v=TTom8n3FFCw

That will lead to the answers on the right side, which is not what I am after.

I am after the left side.

Is this still possible?


#5

can we see your code ?


#6

The picture on the left of your diagram is also the angle between two vectors, it’s just that one of the vectors is the difference between the two points, and the other vector is the x axis.


#7

If you calculate where the the planes of your points intersect, you can calculate the lengths of the other sides. After which you can use cos(theta) to calculate your angle, because you should now have the hyptohenus and the adjacent.


#8

@cur10us
I don’t really have a working code but here is from the video posted above with the cos (theta)

To be upfront, I am a beginner in coding. So, apologies in advance if the lines will burn your eyes.

import math

point1= (875.906, 116.319, 666.301)
point2= (1148.838, 257.817, 922.204)

#magnitude of point1
mp1 = math.sqrt((point1[0]*point1[0])+(point1[1]*point1[1])+(point1[2]*point1[2]))
#magnitude of point2
mp2 = math.sqrt((point2[0]*point2[0])+(point2[1]*point2[1])+(point2[2]*point2[2]))

#dot product of point1 and point2
dp = ((point1[0]*point2[0])+(point1[1]*point2[1])+(point1[2]*point2[2]))


angle = 1/(math.cos(mp1/mp2))
print (math.degrees(angle))

#result to 77.584 degrees

# The intended resulting angle is -46.844 ° at least on the X rotation and 20.717 ° on the Y rotation.
# The values are retrieved from the manually rotating the edge

@Munkybutt @mje11even @JesseFK

Thanks for the response. I am ashamed to say that I don’t really understand so far the concept of your approach (although JesseFK’s seems to make sense to me. Just not sure how to implement it) . I’m not that good with trigonometry, but I’ll try to search around and will get back to you if get something.

Thanks again!


#9

import math
import maya.api.OpenMaya as apiOM

p0 = apiOM.MVector(cmds.xform("p0", q=True, ws=True, translation=True))
p1 = apiOM.MVector(cmds.xform("p1", q=True, ws=True, translation=True))

math.degrees(math.acos((p1-p0).normal() * apiOM.MVector(1, 0, 0)))