Transform from relative to world space in Processing

…衆ロ難τιáo~ 提交于 2019-12-05 05:56:07

问题


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

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.

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

回答1:


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

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


来源:https://stackoverflow.com/questions/5470819/transform-from-relative-to-world-space-in-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!