在Processing中从相对世界空间转换 [英] Transform from relative to world space in Processing

查看:225
本文介绍了在Processing中从相对世界空间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is a good way of transforming a local relative point, into the world (screen) space in Processing?

例如,取植绒示例。我如何在 Boid relativeToWorld 方法和 worldToRelative 方法$ c>上课。这些方法会考虑到,所有变换都是在渲染方法中完成的。

For example, take the Flocking example that comes with the Processing PDE. How would I implement a relativeToWorld method and a worldToRelative method in the Boid class. These methods would take into consideration, all the transforms done in the render method.

我以为我想要的转换PVector对象,因此方法签名可能类似于:

I was thinking I would want to transform PVector objects, so the method signatures might look something like:

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.
}


推荐答案

不幸的是,Processing没有用这样的东西让生活变得轻松。它不能让我们访问转换矩阵,所以我认为我们必须使用手动转换矩阵/向量乘法。 (如果你很好奇,我在同构表示中使用了帧到规范的转换矩阵。)

Unfortunately, Processing doesn't make life easy with such things. It doesn't provide us access to transformation matrices, so I believe we have to use a manual transformation matrix/vector multiplication. (If you're curious, I used frame-to-canonical transformation matrices in homogeneous representation).

警告
这个是一个在java中快速解释的Mathematica结果,假设您只有一次旋转一次翻译(如在render方法中)。翻译仅由本地PVector提供。

WARNING: This is a Mathematica result quickly interpreted in java which is assuming you only have one rotation and one translation (as in the render method). The translation is only given by the 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;
}

这篇关于在Processing中从相对世界空间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆