I am creating a game where I need to use this code to shoot 360 degrees. Here is what I have so far:
UITouch *myTouch = [touches anyObject];
CGPoint touchPoint = [myTouch locationInView:[myTouch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
CCSprite *projectile = [CCSprite spriteWithFile:@"bullet.png" rect:CGRectMake(0, 0, 37, 37)];
projectile.position = ccp(240, 160);
[self addChild:projectile];
projectile.scale = .2;
int offX = touchPoint.x - projectile.position.x;
int offY = touchPoint.y - projectile.position.y;
int realX = 498;
if (offX <= 0) {
realX = -10;
}
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + projectile.position.y;
if (realX == -10) {
realY = (-realX * -ratio) + projectile.position.y;
}
CGPoint realDest = ccp(realX, realY);
NSLog(@"%0.2f %0.2f", realDest.x, realDest.y);
// Determine the length of how far we're shooting
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
[projectile runAction: [CCMoveTo actionWithDuration:realMoveDuration position:realDest]];
So this is my problem. It works just fine if the touch point is to the right of bullet, but if it is too the left, it shoots straight left. How do I fix it so I can shoot a full 360 degrees.