【问题标题】:Transform from relative to world space in Processing在处理中从相对空间转换到世界空间
【发布时间】:2011-03-29 10:03:51
【问题描述】:

在处理中将局部相对点转换为世界(屏幕)空间的好方法是什么?

例如,以 Processing PDE 附带的Flocking example 为例。我将如何在Boid 类中实现relativeToWorld 方法和worldToRelative 方法。这些方法会考虑到在render 方法中完成的所有转换。

我想我想要转换 PVector 对象,所以方法签名可能看起来像:

PVector relativeToWorld(PVector relative) {
    // Take a relative PVector and return a world PVector.
}

PVector worldToRelative(PVector world) {
    // Take a world PVector and return a relative PVector.
}

【问题讨论】:

标签: java transformation processing coordinate-transformation


【解决方案1】:

不幸的是,Processing 并没有让这些事情变得轻松。它没有让我们访问转换矩阵,所以我相信我们必须使用手动转换矩阵/向量乘法。 (如果你好奇,我在齐次表示中使用了帧到规范的转换矩阵)。

警告: 这是一个在 java 中快速解释的 Mathematica 结果,假设您只有 一个旋转一个平移(如在渲染方法中)。翻译仅由 loc PVector 给出。

PVector relativeToWorld(PVector relative) {
  float theta = vel.heading2D() + PI/2;
  float r00 = cos(theta);
  float r01 = -sin(theta);
  float r10 = -r01;
  float r11 = r00;
  PVector world = new PVector();
  world.x = relative.x * r00 + relative.y*r01 + loc.x;
  world.y = relative.x * r10 + relative.y*r11 + loc.y;
  return world;
}

PVector worldToRelative(PVector world) {
  float theta = vel.heading2D() + PI/2;
  float r00 = cos(theta);
  float r01 = sin(theta);
  float r10 = -r01;
  float r11 = r00;
  PVector relative = new PVector();
  relative.x = world.x*r00 + world.y*r10 - loc.x*r00 - loc.y*r10;
  relative.y = world.x*r01 + world.y*r11 - loc.x*r01 - loc.y*r11;
  return relative;
}

【讨论】:

  • 您的代码中的所有 x 和 y 都正确吗?而且我认为您在最后几行中设置 relative 的位置,应该设置 x 和 y 属性?
  • 更正了 relative.x/y。对你起作用吗?我现在测试了它,它似乎做得很好,不是吗?同样,这不是最佳解决方案,因为它仅限于一次平移和旋转。
  • 完美运行。我只是有时间来测试它 :) 感谢您的测试和编辑。
  • 我相信您在 worldToRelative 函数中遗漏了一个减号。应该是 float r01 = -sin(theta);花了一段时间试图弄清楚。
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2016-01-07
  • 2013-04-22
相关资源
最近更新 更多