Actual Position

This topic contains 14 replies, has 4 voices, and was last updated by  riq 4 years, 9 months ago.

Viewing 15 posts - 1 through 15 (of 15 total)
Author Posts
Author Posts
July 6, 2009 at 11:46 pm #215960

urip
@urip

I’ve just discovered the ‘position’ property of the CocosNode class doesn’t take into consideration neither scale, rotation nor anchorPoint. Is there a way to get the ‘real’ position which I’m not aware of? or do I need to implement that myself?

thanks

July 7, 2009 at 12:07 am #252019

riq
Keymaster
@admin
CGPoint absolutePosition = [sprite convertToWorldSpace:CGPointZero];

Could you add this tip here:

http://www.cocos2d-iphone.org/wiki/doku.php/tips:generic

?

thanks.

July 7, 2009 at 3:40 am #252020

emalamisura
Participant
@emalamisura

wow I spent the entire day yesterday trying to figure this out only to fudge my numbers, glad I stumbled on this post! This should seriously be in the tips page!

July 7, 2009 at 9:35 am #252021

Steffen Altwiese
Moderator
@steve-oldmeadow

Added to the Wiki.

http://www.cocos2d-iphone.org/wiki/doku.php/tips:generic#get_actual_position_of_sprite_taking_into_account_scale_rotation_and_anchor

July 7, 2009 at 11:30 am #252022

urip
@urip

I tested this method a bit, and I can’t quite understand the values I get as I change the anchor point.

(0,0) anchor point is top left, right? (1,0) top right, (1,0) bottom left, and (1,1) bottom right. so far so good?

I have a sprite, 40 px width and 30 px height. I set the sprite’s position to (100,100) (that means the distance of the sprite’s center point from the bottom left corner of the scene is (100, 100), right?), and observed the points returned by [sprite convertToWorldSpace:CGPointZero]

anchorPoint: (0, 0) point returned: (100, 100)

anchorPoint: (1, 0) point returned: (60, 100)

anchorPoint: (0, 1) point returned: (100, 70)

anchorPoint: (1, 1) point returned: (60, 70)

anchorPoint: (0.5, 0.5) point returned: (80, 85)

I can’t find the logic in those coordinates. If I assume those coordinates are indeed relative to the bottom left corner of the scene, than (0,0) anchor point is actually top right, and (1,1) bottom left. Also, why isn’t the center point (0.5, 0.5) returns the regular position I’ve set? Isn’t the sprite’s position relative to its center?

Help anybody???

July 7, 2009 at 8:46 pm #252023

urip
@urip

has anyone else used this method with the anchorPoint? is it a bug in cocos2d or my own individual application?

July 7, 2009 at 9:47 pm #252024

riq
Keymaster
@admin

@urip: perhaps it’s a bug. but:

(0,0) means bottom-left

(1,1) means top-right

cocos2d uses OpenGL coordiantes.

July 7, 2009 at 10:06 pm #252025

urip
@urip

oh, OK. However that still doesn’t explain why I get (100, 100) for the sprite’s bottom left corner and (60, 70) for its top right corner.

but I guess I can live with that, so never mind…

thanks riq

July 7, 2009 at 10:55 pm #252026

urip
@urip

just to let you know

I discovered not only the coordinates are disordered, but also they are actually incorrect. I placed a sprite (40 width, 30 height) in the position (20, 15) so its bottom left corner touches the scene’s bottom left corner.

I assumed (0,0) anchor point is bottom left and (1,1) is the top right.

Those are the values returned.

Seems like the only correct coordinate is the one corresponding to (0.5, 0.5) anchor point, and is actually the left bottom point of the sprite.

I can find all the actual points by the bottom left one and some simple math calculations so that’s not a fatal problem, but I thought you should know about that anyway.

July 7, 2009 at 11:14 pm #252027

riq
Keymaster
@admin

@urip: thanks. could you open a bug ?

July 8, 2009 at 4:48 pm #252028

urip
@urip

yes, but I’m just trying to figure out more information about this bug before I post a bug report.

Seems like there is a problem with the anchor point not only with the convertToWorldSpace method, but also with effects (such as scale). If I set the anchorPoint to (0,0) and scale the sprite to 0.5, the sprite sticks to its top right corner. (1,1) anchor point makes the sprite stick to its bottom left corner. Is this the way it is supposed to be? It think it’s supposed to be the other way around, isn’t it? I checked it in two different projects so it’s not a unique problem in my project.

Assuming this is really a bug, I think we discovered two different bugs here. One is a quite a minor one (anchor point is up side down). The second one is that the convertToWorldSpace method doesn’t work properly, but that might be a result of the previous bug, although as I said before, the coordinates returned by that method are not only turned up side down like the anchor point, but they are also incorrect.

What do you think? Do you want me to report those bugs in one report or two?

July 8, 2009 at 5:48 pm #252029

riq
Keymaster
@admin

the anchorPoint / scale is working Ok.

Please, see the tests/cocosnodeTest.m. It shows how the anchor point should work.

regarding the convertToWorldSpace, I think it’s a bug.

July 8, 2009 at 8:50 pm #252030

urip
@urip

yea you’re right, my mistake. The problem was I didn’t realize anchor point effects scales and rotations even if it’s being set after the scale and rotation commands.

I will open a bug soon about the convertToWorldSpace method.

July 8, 2009 at 11:48 pm #252031

urip
@urip

another scale/anchor point related question:

I’ve placed a sprite at (100, 100), scaled it to 0.5, and then changed the anchor point to (1, 1). After the change the sprite actually moved on screen.

Is this the way it’s supposed to be? changing the anchor point actually moves the sprite?

changing the anchor point doesn’t effect the sprite’s appearance if it isn’t scaled, so I can’t find the logic here.

July 9, 2009 at 4:34 am #252032

riq
Keymaster
@admin

yes, if you change the anchorPoint the sprite is moved.

please, see the cocosnodeTest.m (and run it!) to understand the meaning of anchorPoint.

whether your sprite is scaled or not, it will be moved if you change the anchorpoint.

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.