Scaling bodies to match sprite scaling and positioning

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 have found that if instead of updating a particular layer's scale, I update self.scale the the sprite images and bodies will change scale together and visually this is exactly what I want. However, I still have a problem. Something doesn't seem to be changing scale but I can't see what it is.

In ccTouchesMoved, i can do the following:

if ([touches count] == 2) {
self.scale = self.scale * 0.98;
}

This will scale the box2d bodies (by 98%) and I assume the sprites which are sitting on a layer called gameLayer follow the bodies and scale themselves to the same size. Visually this is what happens, which is good. (I'm using debug draw to see where the bodies go). However, having scaled the bodies and their sprites, if I go to rotate one sprite around another, using a revoluteJoint (which works perfectly when no scaling is involved), the joint takes the location of the z axis (pivot point) to be the location of where the sprite (or body) used to be before there was scaling, yet visually the sprite rotates around another sprite in the correct place.

Basically, imagine you have a clock with arms, and with your finger you rotate the arm around the clock on a central pivot point. Now, imagine the clock has shrunk, everything looks correct the clock image has shrunk with its body, but to get it to rotate in the same way you have to rotate your finger where the clock arms used to be. If you rotate it around its shrunk central point (assuming btw that it shrinks to one side and not its centre ie. the centre point has changed) at some point it will rotate in the opposite direction to your finger.

By scaling down self.scale and then scaling up gameLayer.scale by the opposite amount I can get my sprite image to stay put and their bodies to shrink and prove visually that the above is what happens. ie.:

self.scale = self.scale * 0.98;
gameLayer.scale = gameLayer.scale / 0.98;

So as far as I can work out, something is being left behind when I just do self.scale, but I don't know what. Does any one have any ideas? Thanks in advance...
So, continuing on in trying to find an answer, I think that everything is scaling except whatever it is that picks up the position of the users touch (on iphone/ ipod touch/ or simulator). When I log what is happening with the positioning of my sprites and bodies, I find whatever the scale, they are staying in the same position so it is working like a camera is zooming in and out on the objects. The problem is if, for example, the user has zoomed out (the objects are tiny spots on the screen say around the area of coordinates [100, 100] ) and then the user touches the screen say to the far right of the objects, looking at the screen you'd expect the coordinates of the touch to be around [500, 100] or even [1,000, 100] but because the thing that picks up the touch location doesn't scale too, the coordinates are around the [100, 100] area ie. like there has been no scaling.

So, I think I need to find a way scale the touch position inversely proportionally to the object scaling to trick the program into thinking the touch is in a different location to where it is. But I have no idea how to do this or where to do it and it seems like an awkward answer. Either that, or if there is a way of scaling the touch position in a touch.scale like fashion, or a completely different approach... Any ideas?
Anytime you scale or scroll your world/view, you need to adjust your touch coordinates as well. You could convert the touch coords manually, or, i f you're using cocos, take a look at the convertToNodeSpace and convertToWorldSpace methods. Where you do this conversion depends on where you interpret the touch coords...a good place to start would be in the touch handler methods: touchesBegan/moved/ended.
WOW!!
Thanks so much. I've only done a quick test on the code I've put in based on your suggestion, but it think it has sorted it. 😀 It's so nice when something that's been bugging you for days just disappears!

So, I looked into convertToNodeSpace etc. and believe i need to use convertTouchToNodeSpace. In my touches moved and began I am doing:

UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CGPoint locationZoomed = [self convertTouchToNodeSpace:myTouch];

location is my old way of getting the touch location when there is no zooming involved, which I can probably now get rid of. locationZoomed gives me the location of a touch on the screen converted to the point it represents on my layer at whatever scale/zoomed level it may be.

Then using:

b2Vec2 locationWorldZoomed = b2Vec2(locationZoomed.x/PTM_RATIO, locationZoomed.y/PTM_RATIO);

I can set up joints using locationWorldZoomed as a SetTarget point.
Great. Thanks once again JavaJoe.
Hopefully I'm not being too hasty with my joy!

Original thread: https://box2d.org/forum/viewtopic.php?t=5893