Reputation: 1
I'm trying to make a turret that turns to the location thats touched and then shoots the bullet So far there is no problem but I couldn't figure out why the turret does not face the direction I touched NOTE: I forgot to tell that it turns but not to the specific location that I touched. I looked to this sources but they didn't helped http://www.cocos2d-iphone.org/forum/topic/1823 http://stackoverflow.com/questions/3018688/rotating-and-moving-a-uiimageview-cocoatouch
here is my code that does the rotation. What am I missing?
-(void)update:(UITapGestureRecognizer *)recognizer
{
double dx,dy;
CGPoint location = [recognizer locationInView:self.view];
turret.transform=CGAffineTransformIdentity;
bullet.transform=CGAffineTransformIdentity;
bullet.center=turret.center;
if ( location.x < 160 )
{
dx=turret.center.x-location.x;
dy=turret.center.y-location.y;
//if user touched greater than 180
}
else if ( location.x > 160 )
{
dx=location.x-turret.center.x;
dy=location.y-turret.center.y;
//if user touched less than 180
}
CGAffineTransform transform = CGAffineTransformMakeRotation((atan2(dy,dx)*180/M_PI));
NSLog(@"%f",atan2(dy,dx)*180/M_PI);
bullet.transform=transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
turret.transform = transform;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
bullet.center=location;
[UIView commitAnimations];
Upvotes: 0
Views: 174
Reputation: 8536
I believe CGAffineTransformMakeRotation uses radians not degrees, so don't multiply the output of atan2(dy, dx) by 180/pi (although you can when you NSLog it if you want it logged in degrees).
Upvotes: 1