converting the difference in vectors to between 0 and 1

taijifajin :

I'm working with two unit vectors but not sure how to calculate this. I need it so that if they point in the same direction the answer is 1, opposite directions the answer is 0, perpendicular (either up or down) the answer is 0.5, etc.

Examples: For two vectors (1,0) and (-1,0) (so, opposite vectors), the answer I get is 0. For two vectors (1,0) and (1/sqrt(2),1/sqrt(2)) (so, the unit vector pointing at a 45 degree angle) I get 0.25. For two vectors (0,1) and (-1,0) (so, perpendicular vectors) I get 0.5

Thank you for any help with this!

Rabbid76 :

Read about the Dot product In general The dot product of 2 vectors is equal the cosine of the angle between the 2 vectors multiplied by the magnitude (length) of both vectors.

dot( A, B ) == | A | * | B | * cos( angle_A_B ) 

This follows, that the dot product of 2 unit vectors is equal the cosine of the angle between the 2 vectors, because the length of a unit vector is 1.

uA = normalize( A )
uB = normalize( B )
cos( angle_A_B ) == dot( uA, uB )

A dot B

If 2 normalized vectors point in the same direction, then the dot product is 1, if the point in the opposite direction, the dot product is -1 and if the vectors are perpendicular then the dot product is 0.

In pygame the dot product can be computed by math.Vector2.dot(). If A and B are pygame.math.Vector2 objects:

uA = A.normalize()
uB = B.normalize()
AdotB = uA.dot(uB)

In the example above, AdotB is in range [-1.0, 1.0]. AdotB * 0.5 + 0.5 is in range [0.0, 1.0] and math.acos(AdotB) / math.pi + 1 maps the angle between A and B linearly to the range [0.0, 1.0].


Furthermore, pygame.math.Vector2.angle_to() calculates the angle to a given vector in degrees. A value in range [0.0, 2.0] dependent on the angle can be computed by

w = 1 - A.angle_to(B) / 180

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

difference between A[0] and A[0:1] numpy arrays in python

Difference between Semaphore initialized with 1 and 0

Difference between exit(0) and exit(1) in Python

Difference between exit(0) and exit(1) in Python

What is the difference between [0] and [:1] in Go?

Difference between x < 1 and x <= 0

Is there a difference between -1 and ~0?

C++ get the difference between two vectors

Difference between converting List to Array

What is difference between fillRect(0,0,0,1) and clearRect()

difference between "\0" and '\0'

Find the difference between all values of two vectors

Best algorithm for converting real number between 0 and 1 to index

Finding Sequences [gap or difference] between two vectors

"Set Difference" between two vectors with duplicate values

Efficiently Calculate the Difference Between Two Vectors

Finding difference based on interaction between two vectors

What is the difference between 0/1 and true/false in JavaScript

Difference between flex: 0 0 100% and flex: 1 1 100%

What is the performance cost of converting between seqs and vectors?

R programming: Difference between vectors

What is the difference between >0 and >=1?

waitpid - difference between first parameter pid=-1 and pid=0

What is the difference between :0 and :1 of MAM protocols in ejabberd?

For the Mongo database profiler, is there any difference between levels 0 and 1?

Difference between x[:1] and x[0]

Difference between converting and loading in java?

Casting the difference between two vectors into a specified format

Difference between tensor axis -1 , 1 and 0 while expanding