Search code examples
objective-cxcodesprite-kitskphysicsbodyskphysicsjoint

How To Enforce A Pendulum Path


Okay, so I have a sprite, and I have a platform above it. The sprite starts at the point (0,scene.frame.size.height/2) and the platform at (scene.frame.size.width/2,scene.frame.size.height-10). So the sprite is on the left, and the platform is centred at the top of the screen.

I am going to apply a powerful impulse onto the sprite. Vector of the impulse will have an X but no Y. Basically I'm going to throw it right.

It should go underneath the platform, and it will. What I want it to do, is to follow a pendulum path with the pivot point being the platforms centre. More so, I want to force this path. Regardless of the objects x momentum I want it to have to swing along that pendulum path without just passing straight underneath it

If it helps picture swinging from a vine. I would like it to take into account the sprites speed before grabbing onto the vine, but unlike in the real world, I want it to force the sprite to plunge down on an angle and back up on an angle, through a pendulum like path.

I am having a hard time figuring out how I could go about something like this so any help would be greatly appreciated. I know that I will probably need a joint to connect the two, and that if it is all Y momentum I am worried about I could probably get away with just a limit joint, but its the enforcing the pendulum path and converting the momentum part that I am stuck on.

Thank you in advance for all of your help.


Solution

  • Here's an example of how to implement an SKPhysicsJointPin:

    -(id)initWithSize:(CGSize)size {
    
        if (self = [super initWithSize:size]) {
    
            CGPoint location = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    
            // Create the pendulum's pivot
            SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:size];
            sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
            sprite.physicsBody.dynamic = NO;
    
            sprite.position = location;
            sprite.size = CGSizeMake(8, 8);
    
            [self addChild:sprite];
    
            location = CGPointMake(self.frame.size.width/2-100, self.frame.size.height/2);
            size = CGSizeMake(50, 200);
    
            // Create the pendulum's bob
            SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:size];
            sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.size];
            sprite2.physicsBody.dynamic = YES;
            sprite2.physicsBody.affectedByGravity = YES;
            sprite2.physicsBody.linearDamping = 0;
    
            sprite2.physicsBody.mass = 0.1;
    
            sprite2.size = CGSizeMake(32, 32);
            sprite2.position = location;
            [self addChild:sprite2];
    
            CGPoint point = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    
            // Create a joint that connects the pivot and the bob
            SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:sprite.physicsBody
                                                                      bodyB:sprite2.physicsBody anchor:point];
    
            // Add the joint to the world
            [self.physicsWorld addJoint:pinJoint];
        }
        return self;
    }