0
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()

我正在尝试使用海龟图形创建一条路径,但因为一旦你弯曲盒子就不能正确排列我无法让它们对齐。我设法通过更改每个形状的枢轴点使其暂时工作,但它是硬编码的,因此只能在某些情况下工作。我怎样才能让每个盒子对齐?

4

1 回答 1

0

我会利用盒子的枢轴在局部 Z 为零的事实,并切换盒子和弯曲 Gizmo 的方向,使其围绕其底部而不是围绕中心弯曲。由于您正在弯曲盒子,因此您也不能简单地按盒子长度移动,而是必须计算沿弯曲弧线的偏移量。总而言之,这就是我的做法:

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
)
于 2020-06-29T16:40:01.413 回答