Revick
Revick

Reputation: 25

Move sprite along an arc using UIBezierPath

I am trying to move a Sprite from a 'shooting' node on the scene to a dynamic but known point on the scene using an arc.

For context it is a tower defence game and this is the 'mortar' tower.

I am using SpriteKit and Objective-C. I am relatively new to coding but I'm pretty far down the line so I have a fair grip on things, it has just been very long since I have used trigonometry and I'm suffering with the UIBezierPath.

Here is a snippet of my code:

CGPoint cannonPointOnScene = [self.scene convertPoint:self.position fromNode:self.parent];
CGPoint originPoint = CGPointMake(realDest.x - cannonPointOnScene.x, realDest.y - cannonPointOnScene.y);
float angle = atan2f(originPoint.y, originPoint.x);

UIBezierPath *testPath = [[UIBezierPath alloc] init];
[testPath addArcWithCenter:CGPointMake(self.position.x , self.position.y) radius:10.0 startAngle:0.0 endAngle:angle clockwise:YES];

SKAction *actionMove = [SKAction followPath:[testPath CGPath] speed:self.projectileSpeed];
        SKAction * actionMoveDone = [SKAction removeFromParent];
SKAction* fire = [SKAction sequence:@[actionMove, actionMoveDone]];

Using that code, I just get my projectile appearing in one spot, quite far from the tower node, then removed from the scene.

Thanks in advance!

Upvotes: 0

Views: 568

Answers (1)

Nightly
Nightly

Reputation: 581

I can't comment yet so I can't ask for more information before attempting to provide an answer, but I still think I can help.

I can't tell from your variables how exactly you're constructing you path relative to your sprites but the first thing I'd suggest is to increase your radius to 100.0 or 200.0 just to see what's happening. 10.0 is a pretty small radius. The boilerplate gameScene is 1024 points wide, 10.0 points is less than 1% of that.

The path you're creating is a portion of a circle centred around "self.position" with a radius of 10 points.

  • self.position is "one spot, quite far from the tower node". It could also be affected by your coordinate space, but it looks like you took that into account with convert point.
  • A radius of 10.0 and a start angle of 0.0 means your projectile starts 10 points to the right of self.position
  • I don't know what your end angle is, but with clockwise set to YES you're drawing the bottom half of the circle. 3 o'clock to endAngle.

Given your description and assuming we just use the scenes coordinate space, I think:

  • center should be a point exactly between the mortar and it's target.
  • radius should be half the distance between the mortar and it's target.
  • startAngle should be M_PI if you're shooting left to right and 0.0 if you're shooting right to left.
  • endAngle should be between 0.0 and M_PI.
  • clockwise should be YES if you're shooting left to right and NO if you're shooting right to left.

The above suggestions presume a lot about how your game functions and it's very unlikely any of it works, but hopefully it gives you a good idea of where to start.

Upvotes: 1

Related Questions