Scale and follow, bug?


I have followed the above to enable tracking according to scaling.

But there was one problem. Started trace at a scaled position rather than half the screen.

So I multiplied the scale value of the _followNode position. Then started at exactly half. But the longer the task, the bigger the value difference.

Is this because it is a float operation? Is there any way to normalize it a bit more?

I am currently using Box2d.

scale and follow code

bool TestWorld::createBox2dWorld(bool debug)
{
	b2Vec2 gravity = b2Vec2(0.0f, 0.0f);

	this->setScale(1.2f);

	_world = new b2World(gravity);
	_world->SetAllowSleeping(true);
	_world->SetContinuousPhysics(true);

	this->setAnchorPoint(Vec2::ZERO);

	if (debug)
	{
		m_debugDraw = new GLESDebugDraw(PTM_RATIO);
		_world->SetDebugDraw(m_debugDraw);

		uint32 flags = 0;
		flags += b2Draw::e_shapeBit;
		m_debugDraw->SetFlags(flags);
	}

	return true;
}

void TestWorld::setBox2dWorld()
{
	auto bgSprite = Sprite::create("inner village(PNG).png");
	bgSprite->setScale(0.32f);
	bgSprite->setOpacity(100);
	bgSprite->setAnchorPoint(Vec2(0, 0));
	this->addChild(bgSprite);

	b2BodyDef bodyDef;
	bodyDef = STAGE_MANAGER->getStageBodyDef();
	bodyDef.position.Set(0, 0);
	bodyDef.userData = bgSprite;
	background = _world->CreateBody(&bodyDef);

	std::string fileName = STAGE_MANAGER->getFilename();
	std::string fullPath = FileUtils::sharedFileUtils()->fullPathForFilename(fileName.c_str());

	b2dJson b2d_json;
	std::string errorMsg;
	ssize_t fileSize = 0;
	unsigned char* fileData = FileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &fileSize);
	std::string jsonContent;
	jsonContent.assign(reinterpret_cast<const char*>(fileData), fileSize);

	b2d_json.readFromFileReturnFixtureDef(jsonContent, errorMsg, background);

	myBall = this->addNewSprite(Vec2(400, 200), Size(50, 50), b2_dynamicBody, "test", 0);
	Sprite* myBallSprite = (Sprite*)myBall->GetUserData();
	Rect Boundary = Rect(0, 0, 4500, 3200);

	auto FollowCam = Follow::create(myBallSprite, Boundary);
	this->runAction(FollowCam);
}

and follow code

void Follow::step(float /*dt*/)
{
	if (_boundarySet)
	{
		// whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
		if (_boundaryFullyCovered)
		{
			return;
		}

		Vec2 tempPos = _halfScreenSize - _followedNode->getPosition() * _target->getScale();

		log("tempPos [%f] [%f]", tempPos.x, tempPos.y);

		_target->setPosition(clampf(tempPos.x, _leftBoundary, _rightBoundary) * _target->getScaleX(),
			clampf(tempPos.y, _bottomBoundary, _topBoundary)  * _target->getScaleY());

		log("target [%f] [%f]", _target->getPosition().x, _target->getPosition().y);
	}
	else
	{
		_target->setPosition(_halfScreenSize - _followedNode->getPosition() * _target->getScale());
	}
}

It was my lack of effort. It was a very simple math mistake… I solved this.

Ok whew. I was looking at this trying to see the error.

Consider sharing the solution if it’s something you can put in the public domain, as I’m sure it would help others who face similar issues and happen to come across this post.