Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit: How to create Basic Physics Joints

I am trying to create simple Joints between two SKPhysicsBodies. But, they are acting weirdly. I am well aware of the fact the anchor points should be on scene coordinate. Please have a look at the Source code attached.

For Example this is how a fixed Joint results after attaching a small square on a rectangle.

-(void)createFixedJointOnScene:(SKScene*)scene

{

//Adding Rectangle

    SKSpriteNode* backBone = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20, 200)];
backBone.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
backBone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:backBone.size];
backBone.physicsBody.categoryBitMask = GFPhysicsCategoryRectangle;
backBone.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:backBone];

//Adding Square
SKSpriteNode* head = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(40, 40)];
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x, backBone.position.y-40);
head.physicsBody.categoryBitMask = GFPhysicsCategorySquare;
head.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:head];

//Pinning Rectangle and Square
NSLog(@"Head position %@", NSStringFromCGPoint(head.position));
SKPhysicsJointFixed* pin =[SKPhysicsJointFixed jointWithBodyA:backBone.physicsBody bodyB:head.physicsBody anchor:head.position];
[self.physicsWorld addJoint:pin];

}

enter image description here

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest.zip

Thank you.

like image 574
Bavan Avatar asked Oct 13 '13 09:10

Bavan


People also ask

What is SpriteKit and SceneKit?

SpriteKit and SceneKit are iOS frameworks designed to make it easy for developers to create 2D and 3D assets in casual games.

Which units are used internally by the physics engine in SpriteKit?

SpriteKit uses the International System of Units, also known as SI, or the meter-kilogram-second system.

What is a node in SpriteKit?

SpriteKit is a framework that helps you create animated 2D scenes and effects. Every scene in SpriteKit is made up of nodes. The scene object (SKScene) is the root node and any additional content in the scene are nodes (SKSpriteNote) built off of the root node.

What is a SpriteKit?

SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps.


1 Answers

Thank you Smick.. After Comparing Smick's code with mine I found out the order of these two lines are causing the issue.

head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x, backBone.position.y-40);

When I set the position of the Sprite before Setting its physics body, everything started to work correctly.

head.position = CGPointMake(backBone.position.x, backBone.position.y-40);
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];

Now I have attached Smick's code also to the full code and attached the link down here. Enjoy.

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest_Final_Working.zip

like image 59
Bavan Avatar answered Sep 28 '22 06:09

Bavan