struct Turtle (
position_ = [0, 0, 0],
heading_= [0, 1, 0],
rotationQuat_ = quat 0 0 0 1,
amount = 200,
turnAngle = 45,
fn forward = (
c = box pos: position_ wirecolor: red width: 40 length: amount height: 2 lengthSegs: 10
rotate c rotationQuat_
position_ = position_ + amount * heading_
),
fn left = (
q = quat -turnAngle [0, 0, 1]
rotationQuat_ = q * rotationQuat_
invq = inverse q
heading_ = heading_ * invq
c = box pos: position_ wirecolor: red width: 40 length: amount height: 2 lengthSegs: 10
addmodifier c (bend())
c.bend.bendAngle = 45
c.bend.bendAxis = 1
rotate c rotationQuat_
position_ = position_ + amount * heading_
),
fn right = (
q = quat turnAngle [0, 0, 1]
rotationQuat_ = q * rotationQuat_
invq = inverse q
heading_ = heading_ * invq
c = box pos: position_ wirecolor: red width: 40 length: amount height: 2 lengthSegs: 10
addmodifier c (bend())
c.bend.bendangle = -45
c.bend.bendAxis = 1
rotate c rotationQuat_
position_ = position_ + amount * heading_
)
)
fn main = (
delete objects
t = Turtle()
t.left()
t.left()
t.left()
t.left()
t.left()
t.left()
t.left()
t.right()
t.forward()
t.forward()
t.right()
)
main()
我正在尝试使用海龟图形创建一条路径,但由于一旦弯曲框,框就无法正确排列,因此我无法将它们对齐。我设法通过更改每个形状上的枢轴点来暂时工作,但它是硬编码的,因此仅适用于某些场景。如何使每个框对齐?
我会利用盒子的枢轴在局部 Z 中为零的事实,并切换盒子和弯曲小工具的方向,使其围绕其底部而不是围绕中心弯曲。由于您要弯曲盒子,您也不能简单地按盒子长度移动,而必须计算沿弯曲弧的偏移。总而言之,这就是我的做法:
struct Turtle
(
private transform = arbAxis y_axis,
public stepLength = 200,
public turnAngle = 45,
private fn updateTransform angle distance =
if angle == 0 then transform *= transMatrix (transform.row3 * distance)
else transform = rotateYMatrix angle * transMatrix (distance / degToRad angle * [1 - cos angle, 0, sin angle]) * transform,
private fn stride distance bendAngle =
(
local stripe = Box width:40 length:2 height:stepLength widthSegs:1 lengthSegs:1 heightSegs:10 transform:transform wirecolor:red
if bendAngle != 0 do addModifier stripe (Bend bendAxis:2 bendAngle:bendAngle)
updateTransform bendAngle distance
),
public fn forward = stride stepLength 0,
public fn left = stride stepLength turnAngle,
public fn right = stride stepLength -turnAngle
)