4

Using spritekit physics joints how could you make an object orbit another at a fixed distance?

Object-A, should not move Object-B, should orbit Object-A when a force is applied to it

To achieve this I tried attaching a SKPhysicsJointLimit limit between the two physics bodies and then apply a force to Object-B. I consider the behaviour that results to be odd, but it may not be... What results is that Object-B jitters about but continues off for ever and doesn't seem to be constrained by Object-A which has not moved at all.

nacross
  • 2,013
  • 2
  • 25
  • 37
  • I have some ideas how to implement this using other methods, I am mainly interested in answers about SKPhysicsJoints to gain a better understanding of how they work. – nacross Feb 11 '14 at 10:01
  • For those looking for alternative approaches to SKPhysicsJoints. I ended up using followPath:duration: action. This also seems to be another valid approach - http://stackoverflow.com/questions/19045067/is-it-possible-to-rotate-a-node-around-an-arbitrary-point-in-spritekit?rq=1 – nacross Feb 25 '14 at 03:21
  • Did you get any spring like effects using that implementation? – Steven Ritchie Apr 08 '14 at 05:18
  • Using follow path, to orbit the object produces a precise orbit, even when the node you are orbiting is moving, without any jittering. I still have not had the free time to try @TheGreenToaster approach, it might be worth a look in. – nacross Apr 08 '14 at 06:31

1 Answers1

0

If you look at this question you will see it is the opposite of what you are asking.

SKSpriteNode * ObjectA = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size: CGSizeMake(5,5)];
ObjectA.position = CGPointMake(size.width/2,size.height/2);
ObjectA.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ObjectA.size];
ObjectA.physicsBody.affectedByGravity = NO;
ObjectA.physicsBody.dynamic = NO;


SKSpriteNode * ObjectB = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size: CGSizeMake(5,5)];
ObjectB.position = CGPointMake(size.width/2,size.height/4);
ObjectB.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ObjectB.size];
ObjectB.physicsBody.affectedByGravity = YES;
ObjectB.physicsBody.dynamic = YES;


SKPhysicsJointPin *centerPin = [SKPhysicsJointPin jointWithBodyA: ObjectA.physicsBody bodyB: ObjectB.physicsBody anchor: ObjectA.position];


[self addChild: ObjectA];
[self addChild: ObjectB];
[self.scene.physicsWorld addJoint:centerPin];

This article helped a lot with setting up the size.

Hope this helps.

Community
  • 1
  • 1
TheGreenToaster
  • 147
  • 1
  • 3
  • 11