Hi there,
I am very new in sparrow framework and need help to rotate an object by touching with fingers on screen.
What I tried to do was the using of geometry formula: cos phi = |ri *r2| /|r1|*|r2|
Let's show:
- (void)onTouch:(SPTouchEvent*)event
{
SPTouch *touch = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject];
if (touch)
{
SPPoint *touchPosition = [touch locationInSpace:self];
NSLog(@"Touched position (%f, %f)",touchPosition.x, touchPosition.y);
// calculate the new direction vector: Init of lastPosition: x=120, y=200
CGPoint touchDirVector = CGPointMake(touchPosition.x-lastPosition.x, touchPosition.y-lastPosition.y);
// geometry theory: cos phi = |ri *r2| /|r1|*|r2|
float formularUP = (touchDirVector.x*lastDirVector.x)+(touchDirVector.y*lastDirVector.y);
float formularDown = sqrt(pow(touchDirVector.x,2.0)+pow(touchDirVector.y,2.0)) *
sqrt(pow(lastDirVector.x,2.0)+pow(lastDirVector.y,2.0));
float cosPhi = acos(formularUP/formularDown);
// Store the new direction vector
lastDirVector =CGPointMake(touchDirVector.x, touchDirVector.y);
[self.stage.juggler removeTweensWithTarget:sprite];
// create a new juggler
tween = [SPTween tweenWithTarget:sprite time:2.5f transition:SP_TRANSITION_EASE_IN];
[tween animateProperty:@"rotation" targetValue:cosPhi]
[self.stage.juggler addObject:tween];
That's all. the object do not rotate correctly. It turns left although it have to turn right, and opposite.
Have anybody an idea, where the error fits???
Thank you.
isicom