lundi 15 février 2016

Add Points to CGPath Loop

I am developing a game that requires a character SKNode *character to "randomly" move around an SKScene by following a randomly generated path. My question, is how can I effectively and efficiently edit or extend this path so that the character is always following a never ending path?

Here is an outline with some code snippets of how I am approaching this:

1. Create a CGMutablePathRef that will represent the path for the character to follow.

Here is the method that I am using to generate the path:

 - (void)createPath {
    // cgpath declared outside of this method to be retained
    cgpath = CGPathCreateMutable();

    // For simplicity, I am only dealing with positive random numbers for now 
    CGPoint s = CGPointMake(character.position.x, character.position.y);
    CGPoint e = CGPointMake([self randomNumber] + character.position.x, [self randomNumber] + character.position.y);
    CGPoint cp1 = CGPointMake([self randomNumber] + character.position.x, [self randomNumber] + character.position.y);
    CGPoint cp2 = CGPointMake([self randomNumber] + character.position.x, [self randomNumber] + character.position.y);
    CGPathMoveToPoint(cgpath, NULL, s.x, s.y);
    CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);


    SKAction *planeDestroy = [SKAction followPath:cgpath asOffset:NO orientToPath:NO duration:5];
    [character runAction:planeDestroy];

    CGPathRelease(cgpath);

}

- (int)randomNumber {
    return (int)(arc4random_uniform(100) + randPointRad);
}

2. Run an SKAction in the scene to run this method and make the character follow the path.

Here is the code that I am using:

SKAction *wait = [SKAction waitForDuration:5];
SKAction *bla = [SKAction runBlock:^{
    [self createPath];
}];

SKAction *randomMovement = [SKAction sequence:@[wait,bla]];

[self runAction:[SKAction repeatActionForever: randomMovement] withKey:@"randomMovementAction"];

3.* (This is step I am having trouble with) Edit / extend the current cgpath with newly generated random points to continuously move the character randomly around the scene.

I understand that the path that I am creating utilizes the logic shown in the below image enter image description here

Providing Points of Control (POC), I am able to generate a path from defined starting and stopping points.

MY GOAL was to add new random control points to this path, in tern redefining the end point each addition (to some new random point some X distance from the previous point, giving some sense of continuity and coverage), then removing the previous control points (to conserve memory).

Essentially, I am trying to repeat an action that adds a new POC to the already defined path, forcing the path to grow without demonstrating possible sharp turns, velocity changes, or direction changes.

Currently, the character does indeed float around the map randomly and continuously with the provided code I have shown, but at times the character has quite unnatural movements as cgpath(1) ends, and cgpath(2) begins, and so on to cgpath(n).

I know this is a fairly broad question, but I guess my main purpose is asking if there is some way to achieve what I am asking (extend and cgpath without having to stop, then start a completely new path)? OR at least prevent the character from performing any of these sharp unappealing movements. Thanks in advance for any help at all.




Aucun commentaire:

Enregistrer un commentaire