Pivot Points

Since Sparrow 1.2, display objects contain two additional properties: pivotX and pivotY. The pivot point of an object (also know as “origin”, “root” or “anchor”) defines the root of its coordinate system.

Per default, the pivot point is at (0, 0); in an image, that is the top left position. Most of the time, this is what you want. Sometimes, however, you want to have that root at a different position — e.g. when you want to rotate an image around its center. Until now, you had to move the object to a container sprite to be able to do that:

SPImage *image = [SPImage imageWithContentsOfFile:@"texture.png"];
 
SPSprite *sprite = [SPSprite sprite];
image.x = -image.width / 2.0f;
image.y = -image.height / 2.0f;
[sprite addChild:image];
 
sprite.rotation = SP_D2R(45.0f); // -> rotate around center

Not a big deal either, Flash users do that all the time. But one might argue that it's a lot of code for such a simple thing. With the pivot point, the code is reduced to the following:

SPImage *image = [SPImage imageWithContentsOfFile:@"texture.png"];
image.pivotX = image.width / 2.0f;
image.pivotY = image.height / 2.0f;
image.rotation = SP_D2R(45.0f); // -> rotate around center

No more container sprite needed! To stick with the analogy used in previous chapters: the pivot point defines the position where you stab the pin through the object when you attach it to its parent. The code above moves the pivot point to the center of the object.

Note how moving the pivot point changes how the object rotates!


Next section: Dynamic Textures

  manual_v1/pivot_points.txt · Last modified: 2013/03/05 10:19 by 127.0.0.1
 
Powered by DokuWiki