• Optimization help
    2 replies, posted
I want to compare a direction given as (yaw, pitch), from the point of view of object A (vector From) to the line that goes from object A to object B. I came up with a few ways to do this and below is one of them. I only use the return value for one comparison, to determine whether object A is looking at object B. But as you can see, it's really costly. Any ideas how to optimize it, or any other approaches to the problem? [code] function DirectionDifference(From, To: Vector; Pitch, Yaw: Single): Single; Local X: Vector; XDir: Vector; Dir: Vector; D: Single; { X = To - From; D = sqrt( X.x*X.x + X.y*X.y + X.z*X.z ); XDir.x = X.x/D; XDir.y = X.y/D; XDir.z = X.z/D; Dir.x = sin(Yaw); Dir.y = cos(Yaw); Dir.z = sin(Pitch) X = XDir - Dir; D = X.x*X.x + X.y*X.y + X.z*X.z; return D; } [/code] Functionality of the algorithm isn't confirmed but you should get the general idea. Using unit spheres, I'm making the aiming vectors (XDir being the line from A-B and Dir being the looking direction of A) equally long and then calculate the distance between the points on the sphere. The return value is 2 if there's a 90 degree difference and 4 if there's a 180 degree difference between the angles.
Yes. It's called the dot-product. The dot product is equal to the sum of the products of respective components of 2 vectors: dot(A, B) = A.x*B.x + A.y*B.y + A.z*B.z But geometrically, it is also equal to the product of the magnitudes and the cosine of the angle between them. dot(A, B) = |A| * |B| * cos(angle) You can find the dot product easy. You are trying to get the angle, so solve for that. angle = acos(dot(A, B) / (|A| * |B|)) You can optimize this further if you can assume that either or both of the vectors are of unit length. [editline]03:01AM[/editline] You need to fix this bit of code: [code] Dir.x = sin(Yaw); Dir.y = cos(Yaw); Dir.z = sin(Pitch) [/code] In order for Dir to be a representative unit vector, it needs to be: [code] Dir.x = cos(Pitch)cos(Yaw); Dir.y = cos(Pitch)sin(Yaw); Dir.z = sin(Pitch); [/code]
[QUOTE=Cathbadh;17174144]Yes. It's called the dot-product. The dot product is equal to the sum of the products of respective components of 2 vectors: dot(A, B) = A.x*B.x + A.y*B.y + A.z*B.z But geometrically, it is also equal to the product of the magnitudes and the cosine of the angle between them. dot(A, B) = |A| * |B| * cos(angle) You can find the dot product easy. You are trying to get the angle, so solve for that. angle = acos(dot(A, B) / (|A| * |B|)) You can optimize this further if you can assume that either or both of the vectors are of unit length. [editline]03:01AM[/editline] You need to fix this bit of code: [code] Dir.x = sin(Yaw); Dir.y = cos(Yaw); Dir.z = sin(Pitch) [/code] In order for Dir to be a representative unit vector, it needs to be: [code] Dir.x = cos(Pitch)cos(Yaw); Dir.y = cos(Pitch)sin(Yaw); Dir.z = sin(Pitch); [/code][/QUOTE] Thanks. I'm supposed to know this stuff already, but I don't know why I didn't think of the scalar product.
Sorry, you need to Log In to post a reply to this thread.