Processing's PShape can store vertices. Transformations (translate, rotate, scale) are applied to the PShape as a whole, not directly to the vertices. PShape's getVertex() method will always return the original vertex position and not take into account any transformations. The problem is getting the actual vertex positions (world space coordinates) instead of the original vertex positions (model space coordinates). The example below shows two shapes. The first shape is constructed with simple model coordinates and subsequently translated and scaled. The second shape is constructed with coordinates which have already incorporated both transformations. The net result is two identical shapes, where the second is a 'simulation' of the world space coordinates of the first.
It would be very useful to have a method, for example getVertexInWorldspace(), that would return the actual vertex coordinates with all transformations (translate, rotate, scale) applied. That way you could get the final vertex positions, which are useful in many cases. For example when exchanging or exporting shape data.
For comparison, the different results for the current and proposed methods are:
- model.getVertex(0) would return [ -1.0, -1.0, 0.0 ]
- model.getVertexInWorldspace(0) would return [ 220.0, 80.0, 0.0 ]
PShape model, world;
void setup() {
size(640, 360, P2D);
model = createShape();
model.beginShape();
model.noStroke();
model.vertex(-1, -1);
model.vertex(+1, -1);
model.vertex(+1, +1);
model.vertex(-1, +1);
model.endShape(CLOSE);
model.translate(width/2, height/2);
model.scale(100);
printVertices(model);
// v0 | [ -1.0, -1.0, 0.0 ]
// v1 | [ 1.0, -1.0, 0.0 ]
// v2 | [ 1.0, 1.0, 0.0 ]
// v3 | [ -1.0, 1.0, 0.0 ]
world = createShape();
world.beginShape();
world.noFill();
world.stroke(255, 0, 0);
world.strokeWeight(2);
world.vertex(width/2-100, height/2-100); // simulated transform
world.vertex(width/2+100, height/2-100); // simulated transform
world.vertex(width/2+100, height/2+100); // simulated transform
world.vertex(width/2-100, height/2+100); // simulated transform
world.endShape(CLOSE);
printVertices(world);
// v0 | [ 220.0, 80.0, 0.0 ]
// v1 | [ 420.0, 80.0, 0.0 ]
// v2 | [ 420.0, 280.0, 0.0 ]
// v3 | [ 220.0, 280.0, 0.0 ]
}
void draw() {
background(200);
shape(model);
shape(world);
}
void printVertices(PShape shape) {
for (int i = 0; i < shape.getVertexCount(); i++) {
PVector v = shape.getVertex(i);
println("v"+ i + " | " + v);
}
}
Processing's PShape can store vertices. Transformations (translate, rotate, scale) are applied to the PShape as a whole, not directly to the vertices. PShape's getVertex() method will always return the original vertex position and not take into account any transformations. The problem is getting the actual vertex positions (world space coordinates) instead of the original vertex positions (model space coordinates). The example below shows two shapes. The first shape is constructed with simple model coordinates and subsequently translated and scaled. The second shape is constructed with coordinates which have already incorporated both transformations. The net result is two identical shapes, where the second is a 'simulation' of the world space coordinates of the first.
It would be very useful to have a method, for example getVertexInWorldspace(), that would return the actual vertex coordinates with all transformations (translate, rotate, scale) applied. That way you could get the final vertex positions, which are useful in many cases. For example when exchanging or exporting shape data.
For comparison, the different results for the current and proposed methods are: