Hi,
I'm trying to implement a scale/zoom option on my program. I currently scale a layer that my sprite images sit on using
layer.scale = layer.scale * (a scale factor)
when a user performs a typical two touch pinch movement on the screen.
This works find and changes the scale of my layer (and therefore sprite images) using the point of the users initial touch (which on the simulator is the screen's centre) as an anchor point.
I am now trying to match the box2d bodies to this scale movement (which I need to do so that some other option - rotate - works if the view has been scaled). I have managed to scale my fixtures by redrawing them at the appropriate scale as it changes. However, my problem is that the fixtures are scaled individually using their centre as an anchor point and so, while the scaling matches the scaling of the sprite images, the positioning doesn't.
Does anyone know what I could do about this? Is there a way of scaling my fixtures using the users initial touch position as the anchor point? Or, do I need to do something like redrawing all the bodies and placing them in the right position? Though I'm not sure how to do this or get the position as the the sprite position hasn't actually changed (I think).
b2Fixture *fixOld;
b2Fixture *fixNew;
b2Body *bod;
b2Shape *shap;
My code in the tick method changes the radius of the circular sprite as the user changes the scale (it's probably rather ugly as I'm new to all this but it works):
for(bod = _world->GetBodyList(); bod; bod=bod->GetNext()) {
if (bod->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) bod->GetUserData();
for (fixOld = bod->GetFixtureList(); fixOld; fixOld = fixOld->GetNext())
{
shap = (b2Shape *)fixOld->GetShape();
if (shap->GetType() == 0){
b2CircleShape circShap;
circShap.m_radius = ((shap->m_radius)*0.98); //Just work on reducing scale to start with
b2FixtureDef fixdef;
fixdef.shape = &circShap;
fixNew = fixOld;
bod->DestroyFixture(fixOld);
fixNew = bod->CreateFixture(&fixdef);
}
}
}
}
In the ccTouchesMoved method i have:
gameLayer.scale = scaleFactor * 1.02;
gameLayer.scale = scaleFactor * 0.98;
which changes the layer scale based on whether the pinch is inwards or outwards.
(gameLayer is a CCLayer added to _world)
Any help much appreciated. Thanks.
I'm trying to implement a scale/zoom option on my program. I currently scale a layer that my sprite images sit on using
layer.scale = layer.scale * (a scale factor)
when a user performs a typical two touch pinch movement on the screen.
This works find and changes the scale of my layer (and therefore sprite images) using the point of the users initial touch (which on the simulator is the screen's centre) as an anchor point.
I am now trying to match the box2d bodies to this scale movement (which I need to do so that some other option - rotate - works if the view has been scaled). I have managed to scale my fixtures by redrawing them at the appropriate scale as it changes. However, my problem is that the fixtures are scaled individually using their centre as an anchor point and so, while the scaling matches the scaling of the sprite images, the positioning doesn't.
Does anyone know what I could do about this? Is there a way of scaling my fixtures using the users initial touch position as the anchor point? Or, do I need to do something like redrawing all the bodies and placing them in the right position? Though I'm not sure how to do this or get the position as the the sprite position hasn't actually changed (I think).
b2Fixture *fixOld;
b2Fixture *fixNew;
b2Body *bod;
b2Shape *shap;
My code in the tick method changes the radius of the circular sprite as the user changes the scale (it's probably rather ugly as I'm new to all this but it works):
for(bod = _world->GetBodyList(); bod; bod=bod->GetNext()) {
if (bod->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) bod->GetUserData();
for (fixOld = bod->GetFixtureList(); fixOld; fixOld = fixOld->GetNext())
{
shap = (b2Shape *)fixOld->GetShape();
if (shap->GetType() == 0){
b2CircleShape circShap;
circShap.m_radius = ((shap->m_radius)*0.98); //Just work on reducing scale to start with
b2FixtureDef fixdef;
fixdef.shape = &circShap;
fixNew = fixOld;
bod->DestroyFixture(fixOld);
fixNew = bod->CreateFixture(&fixdef);
}
}
}
}
In the ccTouchesMoved method i have:
gameLayer.scale = scaleFactor * 1.02;
gameLayer.scale = scaleFactor * 0.98;
which changes the layer scale based on whether the pinch is inwards or outwards.
(gameLayer is a CCLayer added to _world)
Any help much appreciated. Thanks.