mathematical functions for scripting in LDCad


RE: mathematical functions for scripting in LDCad
#6
(2018-03-14, 22:15)Joscha Wrote: Hi Roland

Yes, all I need is vector and matrix math. My problem is, I would like to rotate a part and I would like to know (or use it in the script) the new position of a specific point of this part. This would usually be a simple coordinate system rotation multiplying a vector with the rotation matrix. The most suitable command seems to be 'transform'. Only somehow it does not behave fully like this. It seems to flip my part by 180°.

Furthermore, is there any way to access the location of the parts center? I mean the rotation matrix always rotates a part around its center. If I like to rotate it around another center during a script I would have to rotate and translate it. This seems to be confusing.

Keep in mind the LDraw coordinate system uses a negative Y while OpenGL does not. I correct for this by using a base model view rotation of 180 degrees.

To rotate around another center you do indeed need to apply a translate, like so:

Code:
local center=ldc.vector(10, 0, 10) --some alternative center.
local rotation=ldc.matrix()
rotation:setRotate(45, 0, 1, 0) --e.g 45 deg rotate around y

--build transformation matrix
local matrix=ldc.matrix()
matrix:setTranslate(center:getInverted())
matrix:mulAB(rotation)
matrix:mulAB(center)

--Apply it to brick(s), e.g the first one in the model
local ref=ldc.subfile():getRef(1)
local refMatrix=ref:getPosOri()
refMatrix:mulAB(matrix)
ref:setPosOri(refMatrix)

I know it's not very efficient but, if it would need to be really fast you wouldn't be using script Smile
You could wrap the matrix construction in a function if you worried about readability etc.

To find the abs position of point in the part you need to use
Code:
someVectorWithPositionRelativeToBrickRefIsLinkedTo:transform(refMatrix)
Reply
« Next Oldest | Next Newest »



Messages In This Thread
RE: mathematical functions for scripting in LDCad - by Roland Melkert - 2018-03-14, 23:55

Forum Jump:


Users browsing this thread: 1 Guest(s)