Creating a spring between two points #84
-
I guess this is one of the questions I will regret asking some day, based on how easy it feels, and how this may not be the ideal place to post, but I spent so much time trying to get this working and still do not see an end in sight. So please, I am trying to solve the following simple problem: I have two coordinates ( As the coilspring_tension does not have any placement arguments, it is clear that I should use the
However, I do not manage to get the rotation to be correct.
I tried the naïve way:
I tried the Unity way:
I tried following various StackOverflow questions to compute the rotation matrix from one vector to another, but (admittedly also due to a lack of understanding, potentially) all of them fail. May I ask for the actual, ideal way of how to span a spring between two points given their coordinates as vec3 here in pymadcad? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello these was few mistakes in your previous code samples, but of course such thing is easily doable in madcad ;) best approachThe best way (most readable and efficient) in madcad is using a special constructor of quat(dir1, dir2) # compute the quaternion rotating from direction 1 to direction 2 As the spring generated by for instance orient = quat(Z, b-a) As the spring is generated centered around zero, we need to translate it to the midpoint between pos = (b + a) / 2 Now, sprint.transform(
translate((b + a) / 2) # translate to midpoint with an homogeneous translation matrix
* mat4(quat(Z, b - a)) ) # reorient from Z to b-a with a quaternion casted to an homogeneous matrix what was wrong
# this will compute an orientation matrix
orient = ( rotatearound(
anglebt(Z, b - a), # angle between Z (current spring direction) and b-a (desired spring direction)
Axis(O,Y)) # this will rotate around axis (O,Y) because the spring center is currently in O
# now we have final spring orientation
# resposition the spring on the midpoint between a and b
pos = translate((b + a) / 2)
final = spring.transform(pos * orient) # matrix product to compose transformations
|
Beta Was this translation helpful? Give feedback.
Hello these was few mistakes in your previous code samples, but of course such thing is easily doable in madcad ;)
best approach
The best way (most readable and efficient) in madcad is using a special constructor of
quat
:As the spring generated by for instance
coilspring_tension
is oriented toward Z, we need to rotate it towardb-a
As the spring is generated centered around zero, we need to translate it to the midpoint between
a
andb
Now,
orient
andpos
cannot be combined together (quat
andvec3
), so we need to cast them to transformation matrices so we can use the…