将一个局部相对点转换为处理中的世界(屏幕)空间有什么好方法?
例如,选择Processing PDE附带的Flocking example。如何在relativeToWorld
类中实现worldToRelative
方法和Boid
方法。这些方法将考虑所有变换在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.
}
答案 0 :(得分:2)
不幸的是,Processing并没有让这些东西变得简单。它不能让我们访问转换矩阵,所以我认为我们必须使用手动转换矩阵/向量乘法。 (如果你很好奇,我在均匀表示中使用了帧到规范的变换矩阵。)
警告强>: 这是在java中快速解释的Mathematica结果,假设您只有一次旋转和一次翻译(如在render方法中)。翻译仅由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;
}