On Tue, Aug 19, 2008 at 10:11 PM, Kao Cardoso Felix <[EMAIL PROTECTED]> wrote:
> On Tue, Aug 19, 2008 at 10:02 PM, Lucio Torre <[EMAIL PROTECTED]> wrote:
>> As you are using your own images, you have to set the image anchor
>> yourself at the center. when you create sprites using:
>>
>> sprite = Sprite("myimage.png")
Ok, so I read the code in cocos/sprite.py and when I pass an image
this is what cocos do:
if isinstance(image, str):
image = pyglet.resource.image(image)
well, it's exactly what I did in my code and after that the image
anchor is never touched again until this:
if anchor is None:
if isinstance(self.image, pyglet.image.Animation):
anchor = (image.frames[0].image.width / 2,
image.frames[0].image.height / 2)
else:
anchor = image.width / 2, image.height / 2
wich sets my anchor independent of I loading the image myself or
passing a string to cocos.Sprite(). So it really doesn't matter if I
load the image myself.
Well, I read a good part of the cocos source to understand what was
going on on my simple program. Well I found the root of the strange
behaviour at the CocosNode transform method code. I made some
simplifications (ommited those optimization tests and camera
transform) just fore the sake of this discussion and the code does
something like this:
glTranslatef(
self.position[0] + self.transform_anchor_x,
self.position[1] + self.transform_anchor_y,
0
)
glScalef( self.scale, self.scale, 1)
glRotatef( -self.rotation, 0, 0, 1)
glTranslatef(
self.children_anchor_x - self.transform_anchor_x,
self.children_anchor_y - self.transform_anchor_y,
0
)
This is moving the child node by the vector that points from
transform_anchor to child_anchor. Then it's doing the rotation and
scaling on the node. Last, it moves the child by the offset of the
parent position plus the transform_anchor.
Well the thing is that my sprite's transform_anchor and
children_anchor coincide (because they are set by the anchor property)
and are in the middle of the sprite. In the last transformation I only
revert the transform_anchor offset applied before and not the
children_anchor offset, thus my sprite is moved exactly by the
positive value of children_anchor wich, in my case, is my sprite
center, so it ends up in the top right corner of the parent sprite.
Please, note that I described the transformations in their order of
ocurrence wich would be from the bottom to the top since we are
dealing with OpenGL.
Well, I found out that including the negative children anchor offset
on the last transformation corrects the strange behaviour I have seem
and, aparently, don't break any of the demos. I written the code like
this:
glTranslatef(
self.position[0] - (self.children_anchor_x -
self.transform_anchor_x),
self.position[1] - (self.children_anchor_y -
self.transform_anchor_y),
0
)
glScalef( self.scale, self.scale, 1)
glRotatef( -self.rotation, 0, 0, 1)
glTranslatef(
self.children_anchor_x - self.transform_anchor_x,
self.children_anchor_y - self.transform_anchor_y,
0
)
Of course the optimization tests are still missing, but I found it
much easier to reason about the code this way.
BUT, there is a problem with my solution above. The children_anchor
property isn't working exactly right. For instance, when I change only
the children_anchor of my sprite to (0,0) and mantain the
transform_anchor in the middle (w/2, h/2) the child still is on the
center of the parent, but rotations happens around a point that is
offset by the difference between the children_anchor and
transform_anchor. Based on the names of the two properties it seems
that their roles are swaped.
I hope I made myself clear enough. I prepared a minimalistic test code
that causes the bug. The only thing I couldn't provide with it are the
two images I used to test, but anyone would to the trick. The code is
here:
http://pastebin.com/m25cd6671
Many thanks for your attention and sorry for this very long email.
--
Kao Cardoso Félix
Página pessoal: http://www.inf.ufrgs.br/~kcfelix
Blog: http://kaofelix.blogspot.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cocos-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---