Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Hey, i'm stuck in Sprite Kit with this problem.

Basically I have an Asteroids type game. But instead of firing from the front of my ship at the rock, I am firing a missile from the side of my ship that arcs towards the rock. Since the rock can be anywhere on the screen (above below, side) I am having a hard time creating the arc.

I can get the CGPoints of both objects and can calculate the distance between them. But generating the arc is hard.

Here is my code

Code:
-(CGMutablePathRef)createCGPath:(CGPoint)missleStartPoint and:(CGPoint)targetEndPoint{
    CGMutablePathRef pathRef = CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, missleStartPoint.x, missleStartPoint.y);
    CGFloat distance = [self getDistanceSquared:missleStartPoint and:targetEndPoint]; // distance between 2 objects
    
    CGPathAddQuadCurveToPoint(pathRef, nil, 600, 300, targetEndPoint.x, targetEndPoint.y);

    return pathRef;
}

- (float)getDistanceSquared:(CGPoint)p1 and:(CGPoint)p2 { // calculate distance between 2 points
    return hypotf(p2.x - p1.x, p2.y - p1.y);
}

Right now I am just using the the center of the screen at x:600 y:300. If I divided the distance of the 2 points I can get where the center would be, or the highest point of where the arc should be.

Can anyone give me a push in the right direction in solving this problem?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.