[Closed] offset vector
Say I have point A: [0,0,0] and point B:[20,10,5] and from those two points I get a vector pointing towards B which is vec: normalize (pB – pA).
How can I offset this vector 8 units perpendicular to point A. We have the forward facing vector which is mentioned above and we can use the upVec at [0,0,1]. We will have to orthogonalize the vectors probably and from their get the perpendicular vector. Then project along the vector represented in the image as ‘x’ on the point helpers axis-tripod.
The end goal is to have the a point that is 8 units perpendicular from the vec of A-B as You can see in the image.
delete objects
clearlistener()
pA = [0,0,0]
pB = [20,10,5]
vec = normalize (pB - pA)
point pos:pA dir:vec size:5 cross:false box:false axistripod:true centermarker:false wirecolor:green
point pos:pA size:5 cross:false box:false axistripod:false centermarker:true wirecolor:yellow
point pos:pB size:5 cross:false box:false axistripod:false centermarker:true wirecolor:yellow
There are infinite points that fulfill this requirement, they form a circle around A. You need to add another requirement.
True so in this case let’s just randomly generate a third point we will call C. Then to get our up vec we can use the average of all three positions and calculate the upvec pointing towards [0,0,1]
Wouldn’t that help enough.
To find an arbitrarily perpendicular unit vector to a 3d vector you can use the following method:
fn ArbitraryPerpendicularUnitVector vec =
(
if (vec.x == 0 and vec.y == 0) then
(
if (vec.z == 0) then [0,0,0] else [0,1,0]
)
else
(
normalize [-vec.y, vec.x, 0]
)
)
Then multiply by your desired distance.
Here is the final snippet that works.
delete objects
clearlistener()
fn offset_pt_along_vec pt1 vec offset:0 = pt = pt1 - vec * offset
fn getTM pA pB upVec:[0,0,1] =
(
local center = (pA + pB) * 0.5
front = normalize (pB - pA)
side = normalize(cross front upVec) -- orthogonalized side
up = normalize (cross side front) -- orthogonalized up
tm = matrix3 front side up center
)
-- points
pA = [0,0,0]
pB = [20,20,5]
-- tm from points
tm = getTM pA pB
oPt = offset_pt_along_vec pA tm.row2 offset:8
iPt = offset_pt_along_vec pA tm.row2 offset:-8
-- core points
point pos:pA size:5 cross:false box:false axistripod:false centermarker:true wirecolor:yellow
point pos:pB size:5 cross:false box:false axistripod:false centermarker:true wirecolor:yellow
-- tm
point size:5 cross:false box:false axistripod:true centermarker:false wirecolor:green transform:tm
-- offset points
point pos:oPt size:5 cross:false box:false axistripod:false centermarker:true wirecolor:green
point pos:iPt size:5 cross:false box:false axistripod:false centermarker:true wirecolor:green