Domanda

Is there an easy way to simulate currents within a scene? I know they offer gravity, mass, density, drag and other attributes within spritekit, but what I am not seeing, is how would I simulate a current affecting objects on the scene?

IE say I have an object dropping using gravity and density, from the top of the screen to the bottom as though it was falling through watter, setting this up should be fairly straight forward. However, lets say I want to add an current flowing left to right through the middle of the scene... so as the object falls it begins to encounter the influence of the current and the fall begins to move with the current, and as it gets closer to the center of the current it increases its left/right movement and as it passes through the center of the current and moves away from its center the left/right movement slows down.

Is this possible? Do I need to create invisibe/transparent nodes moving left to right with gravity to simulate this? Or is there a different approach i should use?

È stato utile?

Soluzione

This can be handled in your -update method. You can check whether a certain node is located within a certain region and apply a force on it, as if it were being affected by a current.

EDIT: I have posted a way you can differentiate the force based on the center of the current.

-(void)update:(CFTimeInterval)currentTime
{

        for (SKNode *node in self.children)
        {
            if (node.position.y > 300 && node.position.y < 500) //Let's say the current is between these values, modify to your situation.
            {
                float diff = ABS (node.position.y - 400);//Difference from center of current.
                CGVector force = CGVectorMake(2*(100 - diff), 0);//Variable force
                [node.physicsBody applyForce:force];
            }
        }
}  
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top