Transform from relative to world space in Processing
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 rend开发者_StackOverflower
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.
}
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;
}
精彩评论