Another option is to apply a little maths and do the rotation manually. You may want to do this if knowing the locations of the points of the triangle in the sketch window is important (e.g. you need to use them in collision detection). TBH I could be making life difficult for myself with this approach (and for all I know it may be less efficient), but I like knowing that all the point coordinates for my objects are in relation to the sketch window.
Anyway - less of the justification; here's some code:
Code:// method for calculating rotated point coordinates
PVector rotatePoint(float x, float y, float rotation) {
PVector rotatedPoint = new PVector();
float angle = radians(rotation);
rotatedPoint.x = cos(angle) * x - sin(angle) * y;
rotatedPoint.y = cos(angle) * y + sin(angle) * x;
return rotatedPoint;
}
// snipped example of use:
tip = space.rotatePoint(10,0,rotation);
leftCorner = space.rotatePoint(-10,-5,rotation);
rightCorner = space.rotatePoint(-10,5,rotation);
beginShape();
vertex(x + tip.x,y + tip.y);
vertex(x + leftCorner.x, y + leftCorner.y);
vertex(x + rightCorner.x, y + rightCorner.y);
vertex(x + tip.x,y + tip.y);
endShape();
This is taken from my
asteroids game. The x and y coordinates in the vertex calls represent the 'centre' of the ship, from which the points are offset. (Note that these, as well as 'tip', 'leftCorner', 'rightCorner', etc. are class properties, but could be locally declared variables.) The nice thing here is I can fire a bullet from the location of the 'tip' without having to do any extra work to take account of transformations that could have been made with push/popMatrix; though as I said, that might not be so complex after all... I'm just wary of relying on helper methods that I might not be able to use in other languages.