Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I know this has been asked before and Ive looked through peoples questions and answers all over the internet and I just cant get it to work. Im sure its down to my inexperience. Ive got an arrow attached to a sprite, this sprite appears when touched but I want the arrow to point where the sprite will launch. I have had the arrow moving but it was very erratic and not pointing as it should. Its not moving at the moment but this is what I have.

- (void)update:(ccTime)delta
{
    arrow.rotation = dialRotation;
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
    pt1 = [self convertTouchToNodeSpace:touch];

    CGPoint firstVector = ccpSub(pt, arrow.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(pt1, arrow.position);
    CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    dialRotation += currentTouch - previousTouch;
    }

I have got pt from ccTouchBegan the same way as pt1

Thanks

share|improve this question
    
This seems to localized or at least the problem is presented in such way. –  Markus von Broady Oct 17 '12 at 7:46
1  
I don't understand why you keep track of the previous angle and current angle? Also why do you increment dialRotation? Just point the bloody sprite to the current touch location (eg. sprite.rotation = currentTouch). –  bummzack Oct 17 '12 at 9:19
add comment

closed as too localized by Trevor Powell, bummzack, Jonathan Hobbs, Byte56, Nate Nov 9 '12 at 18:41

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

try updating arrow.rotation from ccTouchMoved and not in the update.

-(void)update:(ccTime)delta
{
    (empty)
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
    ...

    dialRotation += currentTouch - previousTouch;
    arrow.rotation = dialRotation;
}
share|improve this answer
    
That makes the arrow move when the touch moves. But it just spins wildly! Where have I gone wrong? –  Marc Oct 17 '12 at 7:07
add comment
up vote 0 down vote accepted

Ok I figured it with another method.

-(void) calcAngleAndRotateObjectStartingAtPoint:(CGPoint)start endingAtPoint:(CGPoint) end
{
    CGPoint diff = ccpSub(end, start);
    float rads = atan2f( diff.y, diff.x);
    float degs = -CC_RADIANS_TO_DEGREES(rads);
    degs += 90.0f; // 0 is pointing right, so offset so our angle is pointing out of the top of the sprite
    [aim setRotation:degs];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self ccTouchesMoved:touches withEvent:event];
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if( touch ) {
        CGPoint touchPoint = [touch locationInView: [touch view]];
        touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
        [self calcAngleAndRotateObjectStartingAtPoint:aim.position endingAtPoint:touchPoint];
    }
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{}
@end

Thanks for all your answers but this did it for me.

share|improve this answer
    
I would avoid the 90-degree offset, and instead, rotate your image 90 degrees. It can bite you later. –  ashes999 Oct 18 '12 at 20:07
    
Thanks for that I'll change it. –  Marc Oct 19 '12 at 21:19
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.