Lessons Learned: – Cocos2d-x: Positions, Anchors and Cross Platform coordinates

A few points on positions, anchors and Cross Platform coordinates.

DrawNode weirdness:

The position which you set in a init phase is not the same what it will be after the init is performed and added to a parent. This is because of the cross platform issues where you need to take into consideration areas which are part of the screen but not part of your game area which your user will use to interact. This is main in Android devices where you home button, back buttons etc might take screen area(although I ran into this problem also on Windows).

Object Position and Anchor point:

Remember that your objects center is the anchor point. Also your object will be moved from and rotated  around the anchor point.

http://www.cocos2d-x.org/wiki/Coordinate_System

Transforming coordinates for cross platform using Visible Origin:

Use cases:

  • Convert touch or object coordinates from the entire area screen area to the visible area(game area).

In this case you need to subtract the Visible Origin values from the coordinates:

SetMoverTarget(const Vec2 &target)
{
Point origin = Director::getInstance()->getVisibleOrigin();
this->SetTarget(target – origin);
// do something with target
}

  • Convert coordinate from visible area(game area) to entire screen area.

Point origin = Director::getInstance()->getVisibleOrigin();
Vec2 newPosition = this->GetCurentPosition() + origin;

// Do something with new position

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s