Reputation: 45
i know how to get Angles with atan2 between 2 Points in 2D, but how does this work in 3D?: lets say i have 3 Points A,B,C (all are SCNVector3 with x,y,z coordinates First Line Endpoints A and B 2nd Line Endpoints B and C Now i want to get the angle between the 2 Lines... (in ios Swift) I read something about the dot product and acos but somehow it does not work...
With i=0:
var vector1 = SCNVector3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
var vector2 = SCNVector3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
var dotProduct = vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z
var theta = acos(dotProduct)
var tmp_winkel = GLKMathRadiansToDegrees(theta)
Upvotes: 2
Views: 3003
Reputation: 13462
the dot product takes the norm (magnitude) of the vectors into account. Make sure you deal with unit vectors, or divide by the product of their norms.
import SceneKit
import simd
var vector1 = float3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
var vector2 = float3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
var dotProduct = dot(normalize(vector1), normalize(vector2))
var theta = acos(dotProduct)
or
var vector1 = float3((pointArray[i].x - pointArray[i+1].x), (pointArray[i].y - pointArray[i+1].y), (pointArray[i].z - pointArray[i+1].z))
var vector2 = float3((pointArray[i+2].x - pointArray[i+1].x), (pointArray[i+2].y - pointArray[i+1].y), (pointArray[i+2].z - pointArray[i+1].z))
var dotProduct = dot(vector1, vector2)
var theta = acos(dotProduct / (length(vector1) * length(vector2)))
Upvotes: 5
Reputation: 45
so for getting the direction i do now (thanks mnuages for the hint):
var vectorn = cross(normalize(vector1), normalize(vector2))
if (vectorn.y > 0) { //righ hand side }
else { //left hand side}
Upvotes: 1