FAQ Database Discussion Community


Sprite Kit: check for absence of contact

sprite-kit,skphysicsbody
I'm developing a puzzle platformer game using Sprite Kit (and its physics engine). My player's physics body consists of a large hitbox covering most of the sprite, and a wheel with a pin joint anchored at the bottom of the hitbox. Rotation is added to the wheel to make the...

Keep node in view

ios,sprite-kit,physics,game-physics,skphysicsbody
I have a question: Typically, a node will be removed from the view of its position if it exceeds the device's height. I'm using gravity so it falls back down into the view. How would I make it so the node doesn't remove itself from the view?...

How to stop a sprites physicsBody from moving?

objective-c,sprite-kit,skphysicsbody
In my SpriteKit game i move several sprites using [sprite.physicsBody applyImpulse:vector]; but at a specific point i the game i want them to stop. I already tried to apply the exact opposite impulse.. but that did not worked very well. Thanks ...

applyForce to a node according to its rotation

swift,rotation,sprite-kit,skphysicsbody
I work with Xcode 6, SpriteKit and Swift, I have a SKSpriteNode which is the default Spaceship image of a new Xcode project. This spaceship have a physicbody, but is not affected by the gravity, I want to simulate the space. I want that when I tap the screen, a...

SKPhysicsBody uses only part of SKTexture

swift,sprite-kit,skphysicsbody,sktexture
I'm using an SKTexture with an image and set the SKPhysicsBody. Like that: var moleTexture = SKTexture(imageNamed: "moleTop1") leftPlayer = SKSpriteNode(texture: moleTexture) leftPlayer.physicsBody = SKPhysicsBody(texture: moleTexture, size: leftPlayer.texture!.size()) Now after I start my game, I noticed that the physicsBody is set wrong. I've activated the showPhysics property and my image...

Controlling physicsBody's velocities from exceeding certain values

objective-c,ios8,sprite-kit,skphysicsbody
I have 3 NPC's that each have their own circular physicsBody meant to mimic bouncy balls in zero gravity - more precisely, bouncy balls that never stop moving due to restitution = 1.0; (bounciness 100%) friction = 0.0; (No friction) linearDampening = 0.0; (Collisions don't impact the NPC). If either...

Physics body size not changing with SKSpriteNode size change in Touch moved method

ios,swift,sprite-kit,skphysicsbody
I am trying to create a spring launcher where when touch is moved on scene the spring will coil as much the touch is dragged down and thn launch the item above the spring on touch end method. For that i changed the size and position of SKSpriteNode in touch...

Node colliding with other node when it shouldn't

objective-c,sprite-kit,collision-detection,skphysicsbody,skaction
I have a node that spawns every couple seconds and move across the screen from the right side. I want the score to increase every time the player come in contact with this node but I want the player to go straight through it instead of being blocked by it....

SpriteKit physics: How to make a player sprite follow the (sloped) ground

ios,swift,sprite-kit,collision,skphysicsbody
I am creating a tilemap platform game in SpriteKit. I assigned collision paths to the physics body of all ground tiles and made them non-dynamic. To the player I assigned two collision polygons: a circle on the bottom and a rectangle on the top. The player sprite has a fixed...

Different contactTest bit masks on a single object?

ios,objective-c,sprite-kit,collision-detection,skphysicsbody
Can one physics body object have two different contactTestBitMask? I'm making my first game in Objective-C but I have one object(player) that can collide with two different objects(object1 and object2) and do different things when colliding with one or another, so I have this line: player.physicsBody.contactTestBitMask = object1Category | object2Category;...

Moving an SKPhysicsBody inside a sprite image

iphone,ios8,sprite-kit,skphysicsbody,skaction
I have a bird class that is called inside MyScene (main game scene). This bird is a randomly moving NPC. It has an image for looking left & that image is flipped horizontally when it's flying right. It has no images for flying up or down. The bird class has...

Adding multiple borders to detect nodes on and off screen

swift,ios8,sprite-kit,collision-detection,skphysicsbody
I am using Sprite Kit to add some circle icons to a scene. I have added some code to create a border around the outside of the scene size this is used to detect contact and remove the node from parent. // Outside border collision detection var largeBorder = CGRectMake(0,...

Binary operator '~=' cannot be applied to operands of type 'String' and 'String?'

swift,switch-statement,skphysicsbody
I have a simple switch-statement that is not that simple. switch(bubble?.name){ //bubble is SKPhysicsBody case "largeBubble": // <= error newBubbleSize = "medium" break; default: newBubbleSize = "large" break; } Here I get error that I mentioned in title Binary operator '~=' cannot be applied to operands of type 'String' and...

unwrapping SKPhysicsBody doesn't work

sprite-kit,skphysicsbody,forced-unwrapping
In my project, I create new nodes using a class called RocketMaker. Inside this class, I have a function called applyRecurringForce()... func applyRecurringForce() { var thrust: CGVector = CGVectorMake(100, 100) physicsBody!.applyForce(thrust) } My problem is I cannot access this function from the main scene. override func update(currentTime: NSTimeInterval) { /*...

loosing fps after nodes leave screen

objective-c,sprite-kit,skspritenode,skphysicsbody
I am currently working on a simple game in Sprite Kit. In the game, you are a square that moves around and can shoot at enemies. You shoot "Projectiles" which are just a custom class that inherits from SKSpriteNode. The game works great at first but slowly as you continue...

Stop SKSpriteNode from slowing down

ios,sprite-kit,skspritenode,skphysicsbody,skphysicsworld
I am trying to keep my SKSpriteNodes moving at a constant speed forever, even after collisions. I have set gravity to 0, friction to 0, and linear and angular dampening to zero, but the Sprites still slowly slow down to zero velocity. How can I keep them moving? Based on...

Trying to get Physics Shape That Matches the Graphical Representation

swift,skspritenode,skphysicsbody
what I'm trying to achieve is that the Node would have the same shape as PhysicsBody/texture (fire has a complicated shape), and then I'm trying to make only fireImage touchable. So far when I'm touching outside of the fireImage on the screen and it still makes a sound. It seems...

sprite kit physicsBody around circles

objective-c,sprite-kit,sprite,skphysicsbody
So guy and girls I got this sprite and i was wounding how do i create a circle physics body around it. Maybe using something with alpha. What I currently have ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ball.size]; bodyWithRectangleOfSize just puts a square over the sprite right? ...

How to retain SKSpriteNode velocity after collision with physicsBody?

ios,sprite-kit,velocity,skspritenode,skphysicsbody
In my game i don't want to decrease velocity of my SKSpriteNode after collision with any physicsBody. Currently velocity of my SKSpriteNode is decresing very much after collision with physicsBody. What did i tried : I tried to get velocity in -(void)update:(CFTimeInterval)currentTime method & i provide it to my SKSpriteNode...

SpriteKit Animation isn't loading when it supposed to be. What am I doing wrong?

swift,animation,sprite-kit,skphysicsbody
I'm making a simple game. Hero should jump over the enemies and if they collide - enemy should eat the hero. I have 4 types of enemies randomly spawning in front of a hero. I use the first sprite of every enemy-array as a main, the rest are for animation....

Collision Bit Mask Hero passes right through EVERY thing, even the Ground

ios,swift,sprite-kit,skphysicsbody,skphysicscontact
I am trying to avoid collisions between Hero and Coins, but not between Hero and Grass Ground. As soon as I apply a Collision Bit Mask to Hero, my Hero passes right through EVERY thing. I want it to pass through the Coins but not through the Grass Ground. Here...

Locking Physics Body to X Axis Spritekit Swift

ios,swift,sprite-kit,skphysicsbody
I have a node that I would like to lock to the x axis. I have an impulse on the node (shown below). rectangle.physicsBody!.applyImpulse(CGVectorMake(-11,0)) The node starts off on the x axis, but as soon as it makes contact with another physics body, it flies off on the y axis...

Physics detecting collision multiple times

swift,sprite-kit,skphysicsbody
When the ball collides with the cup, the score should increase by 1. However it currently increases by 1, 3 or sometimes 4 which means the collision is being detected multiple times. I think I must be checking for collisions incorrectly: let ballCategory : UInt32 = 0x1 << 0 let...

SKPhysicsBody “Extra argument 'size' in call”

swift,size,skphysicsbody
What I'm trying to achieve is to make a shape of the object(physical body) exactly as a texture, so that only the texture (and not the transparent layer) would be clickable in the future. let fireLayer = SKSpriteNode(imageNamed: fireImage) fireLayer.anchorPoint = CGPointMake(1, 0) fireLayer.position = CGPointMake(size.width, 0) fireLayer.zPosition = Layer.Z4st...

SpriteKit collision angle of line and circle is odd

ios,sprite-kit,skphysicsbody
Suppose a circle is enclosed in a diamond, and the circle heads due right. I would expect a series of continual 90 degree angles as the circle draws a square within the diamond. What I'm seeing, however, is that the circle eventually gets pushed toward the center of the diamond...

Swift SpriteKit edgeLoopFromRect Issue

ios,swift,sprite-kit,cgrect,skphysicsbody
The following code recognizes the bottom and top edges of the scene and the ball bounces off as expected. However, the left and right edges of the scene are breached all the time. The ball goes off screen and then eventually returns back if enough force is applied. It is...

My character's SpriteNode is shaking when moving, why?

objective-c,sprite-kit,skspritenode,skphysicsbody,shake
My main character, which is a SKSpriteNode with physicsBody, is shaking when moving or jumping. I am moving my character by changing is character.physicsBody.velocity.dx's (in order to simulate acceleration or deceleration), and I use applyImpulse to make him jump. Code : To move : [self.physicsBody setVelocity:CGVectorMake(self.physicsBody.velocity.dx - 30, self.physicsBody.velocity.dy)]; To...

node.physicsBody.joints downcasting error

swift,sprite-kit,skphysicsbody,skphysicsjoint
The following code gives an error - it appears the physics joints array have the class PKPhysicsJoint. Anyone have any ideas how I can iterate through the joints in Swift? The documentation does say that physicsBody.joints should return an array of SKPhysicsJoint. import SpriteKit let scene = SKScene(size: CGSize(width: 200,...

How do I give the basket its exact shape, for a physics body, it is an image?

swift,sprite-kit,game-physics,skphysicsbody
basket.position = CGPointMake(self.size.width/2, self.size.height/8) basket.size.height = 50 basket.size.width = 75 basket.name = "basketNode" basket.physicsBody = SKPhysicsBody(rectangleOfSize: basket.size) basket.physicsBody?.usesPreciseCollisionDetection = true basket.physicsBody?.categoryBitMask = basketCategory basket.physicsBody?.dynamic = false self.addChild(basket) So currently, balls are falling from top of the screen, the players job is to catch them in the basket, for it to...

Properly setting up SKFieldNode.categoryBitMask & physicsBody.contactTestBitMask

objective-c,ios8,sprite-kit,skphysicsbody,skfieldnode
I'm trying to make an invisible boundary, with the SKFieldNode, that sends an NSLog(@"Contact registered between bird & field node."); through didBeginContact method when a sprite is inside the SKFieldNode radius. I think I set up the code correctly, but nothing's happening when a bird sprite's physicsBody is contacting the...