Transform from relative to world space in Processing

匆匆过客 提交于 2019-12-03 22:07:23

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