Notifications
Clear all

[Closed] align vector B to vector A

I’m not entirely clear on how to correct this issue. If i change the green point to be a negative value it doesn’t rotate the results correctly.

If you change a_pt1 = [23,0,0] to [-23,0,0] you’ll see the fail in the code.


clearlistener()
delete objects

a_pt1 = [23,0,0] -- change to [-23,0,0] to see fail
a_pt2 = [0,10,0]
b_pt1 = [0,30,0]
b_pt2 = [0,23,0]

a1 = point pos:a_pt1 wirecolor:green size:5 box:true cross:true
a2 = point pos:a_pt2 wirecolor:green size:4 box:true cross:true
b1 = point pos:b_pt1 wirecolor:red size:5 box:true cross:true
b2 = point pos:b_pt2 wirecolor:red size:4 box:true cross:true

-- New Aligned Nodes
c1 = point pos:b_pt1 wirecolor:red size:4 box:true cross:true
c2 = point pos:b_pt2 wirecolor:red size:3 box:true cross:true

fn GetVectorsAngle v1 v2 =
(
	theAngle = acos(dot (normalize v1) (normalize v2))
)
vecA = a_pt2 - a_pt1
vecB = b_pt2 - b_pt1
ang = GetVectorsAngle vecA vecB

print ang
-- Step 1 - the point to rotate around
pt = a_pt1

-- Step 2 - the vector axis to rotate around
vectorAxis = [0,0,1]

-- Step 3 - getting input matrix
inputMatrix = Matrix3 1

-- Step 4 - to rotate around another point P, you need to translate(-P) , 
-- then rotate(r) , then translate(P)
-- you have a vector to rotate around, and an angle, (v, a)
translate inputMatrix -b_pt1
--translate inputMatrix -pt -- alternate to maintain offset
rotate inputMatrix (quat ang vectorAxis)
translate inputMatrix pt

c1.pos *= inputMatrix
c2.pos *= inputMatrix

2 Replies
 lo1

dot product can only return an angle in the 0-180 range. It treats both cases as the same angle.

Instead, use atan2 to find the angle.

fn GetVectorsAngle v1 v2 =
(	
	(atan2 v2.y v2.x) - (atan2 v1.y v1.x)
)

thank you!