Hey guys, I (again) have a problem with my shooting code. Ok so I know whats causing the problem in my code, but not how to fix it. So heres my dilema: say I have a spot where i am and I touch the screen. Thats all well and good but in my game, I want the bullet to travel off-screen. So what I know is the position (x, y) of the tank and I know the position of where the user touched, but I don't know how to calculate that off-screen point where the bullet should go. I've drawn up a diagram to better explain. The gray dot is the position that the tank is at. the yellow where you touched and the blue dot is the one I want to get. Here it is:
Shooting Problem
(7 posts) (4 voices)-
Posted 1 year ago #
-
Somebody posted this in your previous thread, but I'll post it here anyway. It tells you how to do just what you're trying to do. Best of luck.
http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial
-Nick Homme
Posted 1 year ago # -
thanks, but I've already gone through that tutorial, problem is, it doesn't work with my game. The problem is that in his version, the character stays the same, so his realX variable can stay set at
realX = _winSize/2 + _bullet.contentSize/2;which implies that the x will always stay at 485 in the case of my game. but, if you touch the other side of the screen, it does the calculation, but it always shoots towards the right. the y calculation is right, but the x is always 485. when I made it set like this:
if (location.x < _tankGood.x) { realX = -5; } else { realX = 485; }when you touched it farther left than the tank, it shot to the left, but the y calculation wasn't right. Here's my code right now:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; location = [[CCDirector sharedDirector] convertToGL:location]; //setup initial location of bullet _bullet = [CCSprite spriteWithFile:@"bullet.png" rect:CGRectMake(0, 0, 10, 10)]; _bullet.position = ccp(_tankGood.x + 15.0, _tankGood.y); int offX = location.x - _tankGood.x; int offY = location.y - _tankGood.y; [self addChild:_bullet]; // Determine where we wish to shoot the projectile to ratio = (float) offY / (float) offX; if (location.x < _tankGood.x) { realX = _winSize.width - _winSize.width - (_bullet.contentSize.width/2); realY = -((realX * ratio) + _bullet.position.y); } else if (location.x > _tankGood.x) { realX = _winSize.width + (_bullet.contentSize.width/2); realY = (realX * ratio) + _bullet.position.y; } CGPoint realDest = ccp(realX, realY); // Determine the length of how far we're shooting int offRealX = realX - _bullet.position.x; int offRealY = realY - _bullet.position.y; float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY)); float realMoveDuration = length/kBS; [_bullet runAction:[CCSequence actions: [CCMoveTo actionWithDuration:realMoveDuration position:realDest], [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)], nil]]; _bullet.tag = 2; [_bullets addObject:_bullet]; }Posted 1 year ago # -
Caleb Wren wrote a comment on Ray Wenderlich's post, which might help you with your problem:
http://www.raywenderlich.com/692/rotating-turrets#comment-345
According to Ray the helper functions Caleb suggested will allow shooting in any direction.Posted 1 year ago # -
@termhn,
CGSize sz = [[CCDirector sharedDirector] winSize]; CGPoint diff = ccpSub(touchLocation, yourTank); float rads = atan2f( diff.y, diff.x); CGPoint norm = ccpForAngle(rads); norm = ccpMult(norm, 1.5f * ((sz.width > sz.height)?sz.width:sz.height )); norm = ccpAdd(norm, touchLocation);Edit: .. Well, the clamping I had didn't work as expected. I posted before I tested, heh.
I've removed the incorrect clamping I had..but this will get a point off screen definitely, just not right at the boundary. It's definitely the right angle though.
HTH,
-MarkPosted 1 year ago # -
@Mark
thanks for the post, but it would be nice, just cause I'm curious, what does the
norm = ccpMult(norm, 1.5f * ((sz.width > sz.height)?sz.width:sz.height ));do?
@purplelilgirl
Thanks, I shall try this.Posted 1 year ago # -
Hi,
norm (normalized vector) is of length 1, and I'm just making sure I have a point off screen by multiplying the length of the vector by (1.5 times either the length or height of the screen, whichever is greater.)
-Mark
Posted 1 year ago #
Reply
You must log in to post.