I am trying to programmatically build a pendulum using iOS SpriteKit using the built in physics.
Currently, I have the pendulum pivot, a weight, and a limited joint that allows the weight to swing... But, I have no idea on how to code a line (rod) between the pivot and weight.
I assume that drawing a line with SKShapeNode would be a start...?
-(void)setupPendulum
{
pivot = [SKSpriteNode spriteNodeWithImageNamed:#"pivot.png"];
pivot.position = CGPointMake(self.size.width / 2, self.size.height / 2);
pivot.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1.0];
pivot.physicsBody.dynamic = NO;
pivot.physicsBody.affectedByGravity = NO;
pivot.xScale = 0.25;
pivot.yScale = 0.25;
[self addChild:pivot];
weight = [SKSpriteNode spriteNodeWithImageNamed:#"weight.png"];
weight.position = CGPointMake(150, 512);
weight.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:100.0];
weight.physicsBody.dynamic = YES;
weight.physicsBody.affectedByGravity = YES;
weight.physicsBody.mass = 0.5;
weight.xScale = 0.75;
weight.yScale = 0.75;
[self addChild:weight];
SKPhysicsBody *ab = pivot.physicsBody;
SKPhysicsBody *bb = weight.physicsBody;
CGPoint ap = pivot.position;
CGPoint bp = weight.position;
SKPhysicsJointLimit *joints = [SKPhysicsJointLimit jointWithBodyA:ab
bodyB:bb
anchorA:ap
anchorB:bp];
[self.physicsWorld addJoint:joints];
}
My project required the SKPhysicsJointLimit instead of the pin so after a lot of trial and error I found the following solution. The line is removed and drawn in didSimulatePhysics. The line connects the "satelliteShip" as it orbits around the "motherShip". I'm new to all this so I appreciate any feedback on this approach.
Setup variable first:
SKShapeNode *_lineNode;
Now draw the line in didSimulatePhysics:
- (void)didSimulatePhysics {
if (_lineNode){
[_lineNode removeFromParent];
}
SKNode *satelliteShip = [self childNodeWithName:kSatelliteShipName];
CGMutablePathRef pathToDraw;
pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, motherShip.position.x, motherShip.position.y);
CGPathAddLineToPoint(pathToDraw, NULL, satelliteShip.position.x, satelliteShip.position.y);
CGPathCloseSubpath(pathToDraw);
_lineNode = [SKShapeNode node];
_lineNode.path = pathToDraw;
CGPathRelease(pathToDraw);
_lineNode.strokeColor = [SKColor grayColor];
[self addChild:_lineNode];
}
The correct direction was to look at SKPhysicsJointPin, which can use a SKShapeNode or SKSpriteNode (etc.).
https://developer.apple.com/library/iOS/documentation/SpriteKit/Reference/SKPhysicsJointPin_Ref/Reference/Reference.html
Note About my question and the comments on it:
Since there isn't very much documentation (now) on SpriteKit beyond the (small book sized) reference by Apple and a grip of single-class-app examples/tutorials online -- most peoples responses are to, "just look it up + Cocos2d in google" and just re-do that. That's nice en all, but I'm not a copy-paste kinda of person. I like to learn the best way which is sustainable and reusable. This is why I asked what the best way to do it vs. posting busted Cocos2d ported copy-paste code and letting someone fill in the blanks like I see happen too often.
Related
I'm programming a little Game. For this I need some Walls. Therefor I have used:
Wall[w] = [[SKShapeNode alloc] init];
Wall[w].path = WallPos[w];
Wall[w].lineWidth = 4;
Wall[w].strokeColor = [UIColor whiteColor];
Wall[w].zPosition = 3;
[self addChild: Wall[w]];
Wall is an Array of SKShapeNodes and is set in #interface, so I can use it in every method. WallPos contains CGMutablePathRefs.
In TouchesBegan and TouchesMoved I'm calling a method which should check if you have touched one of the walls.
I have also some SKShapeNodes which are Rectangles, and to check if they are touched, I have used
if ([SomeShape containsPoint: Position] {
//Do some stuff
}
But with a line it's not working. Sometimes I'm touching on the line and nothing happens. Then I've seen this: Detecting Touch on SKShapeNode that is a Line and I have tried to do it on that way:
for (int i = 0; i < NrWalls; i++) {
if (CGRectContainsPoint(Wall[i].frame, Position)) {
[self GameOver];
}
}
But now every Point I touch sets a "Game Over" to me!!
Has anyone an Idea, how could I check if the line is touched?
Thanks for your help!
DXXL
Not sure why you want to use SKShapeNodes for walls and rectangles. To answer your question, you can attach a physics body to your shape node and use the contact methods to check for possible contacts. However, assigning a physics body to a shape node could be a tricky undertaking due to the anchor points and getting a desired alignment.
Seeing that you are really only drawing rectangles for your walls, I suggest you use a SKSpriteNode with a physics body instead. Something like this:
SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(5, 100)];
myNode.position = CGPointMake(100, 100);
myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:myNode.size];
myNode.physicsBody.dynamic = NO;
myNode.physicsBody.categoryBitMask = CategoryWall;
[self addObject:myNode];
If you need, you can read up on SKPhysicsBody here.
So for the App I'm working on, I have two cubes colliding. I check for this in the standard way. The app tells me when they collide in my "didBeginContact" method.
-(void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == WALL_CATEGORY && contact.bodyB.categoryBitMask == CHARACTER_CATEGORY) {
CGPoint point = contact.contactPoint;
}
}
So i know where the collision takes place, but because it is two squares it can be at any point along the side including the corners. So how would I check if the collision on the left/right/top/bottom exclusively?
Edit: Correct Answer: Might not be the cleanest way to do it but it works. Hopefully it'll help someone in the future.
m_lNode = [SKNode node];
m_lNode.position = CGPointMake(-(CHARACTER_SIZE / 2), 0);
m_lNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)];
m_lNode.physicsBody.allowsRotation = NO;
m_lNode.physicsBody.usesPreciseCollisionDetection = YES;
m_lNode.physicsBody.categoryBitMask = CHARACTER_L_CATEGORY;
m_rNode = [SKNode node];
m_rNode.position = CGPointMake((CHARACTER_SIZE / 2), 0);
m_rNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)];
m_rNode.physicsBody.allowsRotation = NO;
m_rNode.physicsBody.usesPreciseCollisionDetection = YES;
m_rNode.physicsBody.categoryBitMask = CHARACTER_R_CATEGORY;
m_tNode = [SKNode node];
m_tNode.position = CGPointMake(0, (CHARACTER_SIZE / 2));
m_tNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width , 1)];
m_tNode.physicsBody.allowsRotation = NO;
m_tNode.physicsBody.usesPreciseCollisionDetection = YES;
m_tNode.physicsBody.categoryBitMask = CHARACTER_T_CATEGORY;
m_bNode = [SKNode node];
m_bNode.position = CGPointMake(0, -(CHARACTER_SIZE / 2));
m_bNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width, 1)];
m_bNode.physicsBody.allowsRotation = NO;
m_bNode.physicsBody.usesPreciseCollisionDetection = YES;
m_bNode.physicsBody.categoryBitMask = CHARACTER_B_CATEGORY;
[m_character addChild:m_tNode];
[m_character addChild:m_bNode];
[m_character addChild:m_lNode];
[m_character addChild:m_rNode];
-(void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == WALL_CATEGORY) {
switch (contact.bodyB.categoryBitMask) {
case CHARACTER_T_CATEGORY:
NSLog(#"Top");
m_isHitTop = true;
break;
case CHARACTER_B_CATEGORY:
NSLog(#"Bottom");
m_isHitBottom = true;
break;
case CHARACTER_L_CATEGORY:
NSLog(#"Left");
m_isHitLeft = true;
break;
case CHARACTER_R_CATEGORY:
NSLog(#"Right");
m_isHitRight = true;
break;
}
}
}
Added some relevant code. It's my code so there is variables amongst other things but you should be able to figure it out.
I think the best way to determine what side is involved in your contact (top,left,bottom,right side) is to:
calculate the points leaved by a centering cross for up, down, left and right sides (for example if you have a squared sprite..)
let halfWidth = self.frame.width/2
let halfHeight = self.frame.height/2
let down = CGPointMake(self.frame.origin.x+halfWidth,self.frame.origin.y)
let up = CGPointMake(self.frame.origin.x+halfWidth,self.frame.origin.y+self.frame.size.height)
let left = CGPointMake(self.frame.origin.x,self.frame.origin.y+halfHeight)
let right = CGPointMake(self.frame.origin.x+self.frame.size.width,self.frame.origin.y+halfHeight)
Calculate the distance between the contactPoint and each point sides (up, left , right and down)
This step can be possible with this little function:
func getDistance(p1:CGPoint,p2:CGPoint)->CGFloat {
let xDist = (p2.x - p1.x)
let yDist = (p2.y - p1.y)
return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
}
After, the point involved with the lowest distance calculated is the side point nearest the contactPoint.
What are the advantages of this method:
You don't paint zombies or ghost nodes and relative physic bodies
This method work also inside a dynamic mutable CGPath, not only with a
knowed rectangular boundaries
Is fast, few lines of code and if you add other few lines you can be
able to determine also diagonals and make more precise your
algorithm.
the easiest way is to add child sprites the top, left, right, bottom of your squares. Add physics bodies to those, and then you can tell where things are colliding. I'd recommend trying this method first unless you have many many squares on the screen.
If you have a ton of squares on the screen and youre worried about performance. then maybe use contact.contactPoint and convert that point to square coordinates. given the center point of the square, the angle of the squares rotation and that point, you should be able to tell where the square collided. That would require some math.. and I was afraid writing up that kind of solution when the first one may be all that you really need.
I have two nodes in my scene. The first node is a ball at the top of my view. The second node is a rectangle at the bottom of my view to stop the ball from dropping out of view. I am wanting this ball to have NO bounciness at all. I am wanting it to just completely stop when it hits the rectangle. I don't know what I am doing wrong. Any help would be greatly appreciated!
SKSpriteNode *rectangle = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(self.frame.size.width, 50)];
bottom.position = CGPointMake(self.frame.size.width / 2, 0);
bottom.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(rectangle, bottom.size.height)];
bottom.physicsBody.dynamic = NO;
[self addChild:bottom];
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:#"ball"];
ball.position = CGPointMake(node.position.x, node.position.y);
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: 5];
ball.physicsBody.restitution = 0;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, [self randomX], [self randomY]);
SKAction *move = [SKAction followPath:path asOffset:YES orientToPath:NO duration:3.0];
[ball runAction:move];
[self addChild: ball];
Why is is still bouncing off the rectangle?
In my experience, you should in more cases than not be working with frameworks like they expects you to, hacks just end up with weird quirks like the one you encountered. In this case, as #LearnCocos2D said, you should probably be using [ball.physicsBody applyImpulse:(CGVEctor)] if you need the ball to move along a specific path but still maintain a dynamic physicsBody. If you then set the restitution to 0, it will not bounce. I promise.
Read up on vectors and vector maths if you need the ball to follow a specific path (it's knowledge that is good to have anyways when working with physics). I've found this to be a good resource for that. It's for openFrameworks, hence C++, but the concept remains the same.
And if you still really need to use a CGPath, then I think you should tell us a bit more specific about what you need to accomplish. There might be a better solution, or maybe we should file a bug to Apple. Sprite-Kit is still fairly young, I have myself encounter problems I believe to be bugs.
The documentation indicates that the value of the "restitution" property of SKPHysicsBody is "how much energy the physics body loses when it bounces off another object." (here). By setting the restitution to zero, you make the ball as bouncy as possible - it loses 0 energy. If you set the restitution to 1 - loses all energy - it should stop.
I've found lately a tutorial on how to build a game like "flappy bird" using SpriteKit. Instead of implementing the tap-mechanism, I've used the device accelerometer to move the bird, right and left.
My problem, now, is with generating pipes. The method used in the tutorial creates pipes on the x axe and not the y axe, which I want to do.
-(void)createPipes
{
SKTexture* _pipeTexture1 = [SKTexture textureWithImageNamed:#"Pipe1"];
_pipeTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture* _pipeTexture2 = [SKTexture textureWithImageNamed:#"Pipe2"];
_pipeTexture2.filteringMode = SKTextureFilteringNearest;
SKNode* pipePair = [SKNode node];
pipePair.position = CGPointMake( self.frame.size.width + _pipeTexture1.size.width * 2, 0 );
pipePair.zPosition = -10;
CGFloat y = arc4random() % (NSInteger)( self.frame.size.height / 3 );
SKSpriteNode* pipe1 = [SKSpriteNode spriteNodeWithTexture:_pipeTexture1];
[pipe1 setScale:2];
pipe1.position = CGPointMake( self.frame.size.width/2 -100, self.frame.size.height+250 );
pipe1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe1.size];
pipe1.physicsBody.dynamic = NO;
[pipePair addChild:pipe1];
SKSpriteNode* pipe2 = [SKSpriteNode spriteNodeWithTexture:_pipeTexture2];
[pipe2 setScale:2];
pipe2.position = CGPointMake( self.frame.size.width/2 +100, self.frame.size.height+250 );
pipe2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe2.size];
pipe2.physicsBody.dynamic = NO;
[pipePair addChild:pipe2];
SKAction* movePipes = [SKAction repeatActionForever:[SKAction moveByX:0 y:-2 duration:0.02]];
[pipePair runAction:movePipes];
[self addChild:pipePair];
}
My idea is to generate pipes that fall from the "sky" and the bird has to move between the pipes to keep living.
I hope that the description of my problem was very clear :)
Thanks
Clarification : Pipes do fall from the "sky" but the problem lies with their positioning on the screen. When I run the project, there's no gap between the right pipe or the left one. I only see a giant pipe falling, filling, vertically, a good proportion of the screen.
Thanks for linking the tutorial!
If you want the pipes to fall, then just let them fall! The pipes have physics bodies attached so they should fall down from the sky depending on their vertical position. There's no need to use the SKAction for moving the pipes then. However, you need to change their dynamic flag to YES so that gravitational force is applied. See Xcode reference:
A Boolean value that indicates whether the physics body is moved by the physics simulation.
The default value is YES. If the value is NO, then the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.
I have finally found a solution. By changing the values of pipes position as follow, I've managed to make them appear properly on the screen.
SKSpriteNode* pipe1 = [SKSpriteNode spriteNodeWithTexture:_pipe1];
pipe1.position = CGPointMake( (-0.39*pipe1.size.width), 0 );
SKSpriteNode* pipe2 = [SKSpriteNode spriteNodeWithTexture:_pipe2];
pipe2.position = CGPointMake(self.frame.size.width *1.8, 0 );
The result ==> picture
The big challenge now is find a way to make the position of the pipes somehow random. :)
I clearly don't understand the SKPhysicsJoint very well, but there is so little info on the web yet, other than the Apple docs of course. What is wrong with the following code, which I would think should keep the head and neck permanently joined - my intention is that they act like 2 pieces of paper with a pin, so that they can rotate a bit, but not just totally come apart. When I run this code, they fall to the bottom of the SKScene they're in, hit the ground, then the head falls off the body.
Maybe the joint is not moving WITH them or something, it's just staying in place while they move??
self.head = [SKSpriteNode spriteNodeWithImageNamed:#"head.png"];
self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size];
self.head.physicsBody.mass = 0.05;
self.head.physicsBody.dynamic = YES;
self.chest = [SKSpriteNode spriteNodeWithImageNamed:#"chest_neck"];
self.chest.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.chest.size];
self.chest.physicsBody.mass = 0.05;
self.chest.physicsBody.dynamic = YES;
self.leftLeg = [SKSpriteNode spriteNodeWithImageNamed:#"left_leg"];
self.leftLeg.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.leftLeg.size];
self.leftLeg.physicsBody.mass = 10;
self.leftLeg.physicsBody.dynamic = YES;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.head.position = CGPointMake(282, 220);
self.chest.position = CGPointMake(282, 130);
self.leftLeg.position = CGPointMake(282, 10);
} else {
self.head.position = CGPointMake(512, 380);
self.chest.position = CGPointMake(512, 290);
self.leftLeg.position = CGPointMake(512, 10);
}
[self addChild:self.head];
[self addChild:self.chest];
[self addChild:self.leftLeg];
self.chestJointPinAnchor = CGPointMake(self.chest.position.x, self.chest.position.y+39);
self.chestJointPin = [SKPhysicsJointPin jointWithBodyA:self.head.physicsBody bodyB:self.chest.physicsBody anchor:self.chestJointPinAnchor];
[self.physicsWorld addJoint:self.chestJointPin];
This is because you set sprite's position after you set up its physicsBody property.
I haven't discovered any mention of that in documentation, but I broke my head last weekend trying to figure out why my manually created rope works, but recursively one doesn't.
SpriteNodes' position MUST be set before physicsBody.
So, just reorder your code somehow like that:
self.head = [SKSpriteNode spriteNodeWithImageNamed:#"head.png"];
self.chest = [SKSpriteNode spriteNodeWithImageNamed:#"chest_neck"];
self.leftLeg = [SKSpriteNode spriteNodeWithImageNamed:#"left_leg"];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
self.head.position = CGPointMake(282, 220);
self.chest.position = CGPointMake(282, 130);
self.leftLeg.position = CGPointMake(282, 10);
} else {
self.head.position = CGPointMake(512, 380);
self.chest.position = CGPointMake(512, 290);
self.leftLeg.position = CGPointMake(512, 10);
self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size];
self.head.physicsBody.mass = 0.05;
self.head.physicsBody.dynamic = YES;
self.chest.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.chest.size];
self.chest.physicsBody.mass = 0.05;
self.chest.physicsBody.dynamic = YES;
self.leftLeg.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.leftLeg.size];
self.leftLeg.physicsBody.mass = 10;
self.leftLeg.physicsBody.dynamic = YES;
}
Oh, I've noticed, you've already found an answer by yourself... Could you please mark your question as answered then.
That seems about right.
A pin joint allows both bodies to rotate around the joint's anchor point. A real world example is an analog clock. Or a bicycle's pedals. Or a car wheel's axle.
One thing you have to know is that bodies connected through a joint will not collide with each other. You can use the pin joint limits though to prevent the head from doing a full 360 spin around the pin joint anchor.
Okay, so I found out that this is an actual bug in Sprite Kit. The fix is to set the sprite's position before setting its physicsBody. I did that and it worked perfectly, as expected.
Additionally, not invalidating what was said above, but apparently the physics engine assumes the scene's anchorPoint is left at (0, 0). If you change it to something else, the physics engine will still treat it like (0, 0).