cocos2dx inverse scaling a layer

I have a CCLayer which holds all my game objects and i have implemented scaling and scrolling. To make sure that you can't scroll out of bounds i am calculating a rect that represents the screen and use it to check if the rect is within bounds. The problem is that my calculations are wrong. The layer is scaled by a scaleFactor like so:

 world->setScale(scaleFactor);

and then i calculate the scroll rect:

float scrollWidth =  winSize.width * ( 1 / scaleFactor); // winSize is the size of the screen (1280x
float scrollHeight =  winSize.height * ( 1 / scaleFactor);
if(scrollRect.l < 0) scrollRect.l = 0;
if(scrollRect.l + scrollWidth > levelWidth) scrollRect.l -= (scrollRect.l + scrollWidth - levelWidth);
scrollRect.r = scrollRect.l + scrollWidth;

world->setPosition(-scrollRect.l, -scrollRect.b);

(the scale factor value is between 1.0 and 0.5)

This sort of works but only when the layer is zoomed out to the max or zoomed in to the minimum but when scaleFactor isn't MAX/MIN it is wrong (there is some left space). What am i doing wrong? I've also tried changing the anchor points (they are currently set to 0,0) but without any success.

Answers


You can do this whatever your scale factor... here _tileMap is your world

//Code for getting difference b/w two position

CCTouch *fingerOne = (CCTouch*)touchArray->objectAtIndex(0);

CCPoint newTouchLocation = fingerOne->getLocationInView();
newTouchLocation = CCDirector::sharedDirector()->convertToGL(newTouchLocation);
newTouchLocation=_tileMap->convertToNodeSpace(newTouchLocation);

CCPoint oldTouchLocation = fingerOne->getPreviousLocationInView();
oldTouchLocation = CCDirector::sharedDirector()->convertToGL(oldTouchLocation);
oldTouchLocation = _tileMap->convertToNodeSpace(oldTouchLocation);

//get the difference in the finger touches when the player was dragging
CCPoint difference = ccpSub(newTouchLocation, oldTouchLocation);
CCPoint ASD=ccpAdd(_tileMap->getPosition(), ccpMult(difference, _tileMap->getScale()));
CCPoint bottomLeft =ASD;

// Bounding Box....
if (bottomLeft.x >0) {
    bottomLeft.x = 0;
}
if (bottomLeft.y>0) {
    bottomLeft.y = 0;
}
if (bottomLeft.x < -(mapWidth*_tileMap->getScale() - _screenSize.width)) {
    bottomLeft.x = -(mapWidth*_tileMap->getScale()- _screenSize.width);
}
if (bottomLeft.y <-(mapHieght*_tileMap->getScale() - _screenSize.height)) {
    bottomLeft.y = - (mapHieght*_tileMap->getScale() - _screenSize.height);
}
_tileMap->setPosition(bottomLeft);

I hope this may help you..


Need Your Help

send gcm message in groups of 1000 users to all 10000 users

php android google-cloud-messaging

i am using following code to send gcm message using php and mysqldb i have successfully send the gcm message and received it on the device however the android guidelines state that gcm message shou...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.