turtle-graphics - 如何在 maxscript 中使用海龟图形来对齐弯曲的盒子
问题描述
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 为零的事实,并切换盒子和弯曲 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
)
推荐阅读
- python - Pytorch 和数据增强:如何通过模糊、旋转等来增强数据
- javascript - svg中文本的字体样式在下载时不会传播到png
- php - 在php中获取只有名字和姓氏的第一个字母
- node.js - 如何使用 NodeJS 从另一个 Docker 容器访问 sqlite3 数据库?
- sql - 如何修复在 ssrs 中单击两次查看报告的问题
- android - 使用 Kotlin for Android 动态生成带有按钮的表格
- c++ - 即使函数 f1 没有返回任何值并且它的返回类型为 null,这段代码是如何工作的?
- webpack - Webpack:发出的块不创建主入口点
- python - 如何搜索文件并检查是否有
- javascript - 有条件的 Vuelidate 验证器