Static bodies still moving - sprite-kit

this may be a duplicate of
Static Physics body still moving with contact
but I have not the reputation to add a comment.
My problem is with collisions and static bodies. I'm working on a little game and I have a player, so configured (from sks file):
player = worldNode.childNodeWithName("player") as! SKSpriteNode
let playerBodyTexture = SKTexture(imageNamed: "player_physicBody_mask")
player.physicsBody = SKPhysicsBody(texture: playerBodyTexture, size: CGSizeMake(player.size.width, player.size.height))
player.physicsBody!.usesPreciseCollisionDetection = true
player.physicsBody!.categoryBitMask = PhysicsCategory.player
player.physicsBody!.collisionBitMask = PhysicsCategory.ground | PhysicsCategory.enemies
player.physicsBody!.contactTestBitMask = PhysicsCategory.enemies
player.physicsBody!.restitution = 0
player.physicsBody!.friction = 0
player.physicsBody!.allowsRotation = false}
an enemy:
worldNode.enumerateChildNodesWithName("enemy") { node, _ in
let enemy = node as! SKSpriteNode
enemy.name = "enemy"
enemy.physicsBody = SKPhysicsBody(circleOfRadius: 17)
enemy.physicsBody!.dynamic = true
enemy.physicsBody!.mass = 0.4
enemy.physicsBody!.restitution = 0
enemy.physicsBody!.categoryBitMask = PhysicsCategory.enemies
enemy.physicsBody!.collisionBitMask = PhysicsCategory.ground | PhysicsCategory.player
enemy.physicsBody!.contactTestBitMask = PhysicsCategory.player}
and I have the platfrom brick, so configured:
worldNode.enumerateChildNodesWithName("fixedBlock") { node, _ in
let fixedBlock = node as! SKSpriteNode
fixedBlock.name = "fixedBlock"
let fixedBlockBodyTexture = SKTexture(imageNamed: "tripleBlocks_physicBody_mask")
fixedBlock.physicsBody = SKPhysicsBody(texture: fixedBlockBodyTexture, size: CGSizeMake(fixedBlock.size.width, fixedBlock.size.height))
fixedBlock.physicsBody!.categoryBitMask = PhysicsCategory.ground
fixedBlock.physicsBody!.collisionBitMask = PhysicsCategory.player | PhysicsCategory.enemies
fixedBlock.physicsBody!.contactTestBitMask = 0
fixedBlock.physicsBody!.dynamic = false
fixedBlock.physicsBody!.affectedByGravity = false
fixedBlock.physicsBody!.allowsRotation = false
fixedBlock.physicsBody!.pinned = false
fixedBlock.physicsBody!.friction = 0.3
fixedBlock.physicsBody!.restitution = 0}
When the player jumps on the enemies, he has an impulse (like Super Mario :-)), so I have this:
func didBeginContact(contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == PhysicsCategory.player | PhysicsCategory.enemies {
self.player.physicsBody!.applyImpulse(CGVectorMake(0, 500))}}
Ok... sometimes, often, when the player jumps on enemy, the impulse appens correctly, but the ground tile moves down, also if its dynamic propriety is set on false. I tried to apply a JointFixed on the ground tile but when it is pushed down, its physic body "glitch"; I tried to print the physicsBody velocity propierty of the ground tile and when the problem occours, the velocity's dx vector is >100, but should static bodies not be affected by forces?
Any idea?
Many thanks

Related

getting the sprite attached to a SKPhysicsBody

I'm animating a sprite, and when it touches another one, a physics contact function is called. Than, in this function, I'm trying to get the sprite with touched the other. Its body is bodyA but it's a SKPhysicsBody, and it can not be converted as a SKSpriteNode. Do you have any ideas?
The function is correctly called when contact, I just try to get the sprite which body makes the contact. The final idea is getting the actions attached to the sprite with I'm looking for but I suppose when you have the sprite it's easy.
let shootCategory: UInt32 = 0x1 << 0
let enemyCategory: UInt32 = 0x1 << 1
// Declaration of the SKPhysicsBody of the sprite wich will touch the other one
sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20))
sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.categoryBitMask = shootCategory
sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory
sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory
sprite.physicsBody?.affectedByGravity = false
// Declaration of the SKPhysicsBody of the sprite wich will be touched
sprite.run(SKAction.group([moveAction, fadeInAction]))
sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20))
sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.categoryBitMask = enemyCategory
sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory
sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory
sprite.physicsBody?.affectedByGravity = false
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){
// I tried this to get the sprite wich the bodyB is attached to but it doesn't even build
let sprite: SKSpriteNode = sprite.contact.bodyB
contact.bodyA.removeAction(forKey: "moveToAction")
}
}
I finally found the solution with is really simple:
let Node = contact.bodyB.node as! SKSpriteNode

Make a node that doesn't move after collision iOS 8

I have nodes (sand) falling downwards.
I have other nodes (walls) that are static.
How do I make the walls not move when the sand collides with it?
let wall = SKSpriteNode(texture: chosen)
wall.position = location
wall.physicsBody = SKPhysicsBody(circleOfRadius: sprite.frame.width)
wall.physicsBody?.affectedByGravity = false
wall.physicsBody!.categoryBitMask = BLOCK
self.addChild(wall)
//////////
let sand = SKSpriteNode (imageNamed: img)
var randLoc = arc4random_uniform(26)
sand.position = CGPointMake(location.x - CGFloat(10) + CGFloat(randLoc), location.y)
self.addChild(sand)
//gravity
sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.frame.width)
sand.physicsBody?.affectedByGravity = true
//contact
sand.physicsBody!.categoryBitMask = self.PARTICLE
sand.physicsBody?.collisionBitMask = self.BLOCK | self.PARTICLE
if you have wall.physicsBody?.dynamic = false & sand.physicsBody?.dynamic = true then you should be able to detect the contact without the walls being pushed/moved by the sand. - Daniel Mihaila
https://stackoverflow.com/users/4963031/daniel-mihaila

How to change physics body and sprite image of existing sprite after contact

I have created two sprites with physics bodies and handled their contact. How can I change the physics body and sprite image for one of sprites when the contact occurs? Please see relevant code below:
// circle Sprite
circleSprite.name = circleSpriteCategoryName
circleSprite.position = CGPointMake(2*self.frame.size.width/3, 2*self.frame.size.height/3)
circleSprite.zPosition = 10
self.addChild(circleSprite)
circleSprite.physicsBody = SKPhysicsBody(circleOfRadius: circleSprite.frame.size.width/2)
circleSprite.physicsBody?.friction = 0
circleSprite.physicsBody?.restitution = 1
circleSprite.physicsBody?.linearDamping = 0
circleSprite.physicsBody?.angularDamping = 0
circleSprite.physicsBody?.allowsRotation = false
circleSprite.physicsBody?.applyImpulse(CGVectorMake(4, -4))
// square sprite
let squareSprite = SKSpriteNode(imageNamed: "square.png")
squareSprite.position = CGPointMake(CGRectGetWidth(frame)*0.4, CGRectGetHeight(frame)*0.8)
squareSprite.physicsBody = SKPhysicsBody(rectangleOfSize: squareSprite.frame.size)
squareSprite.physicsBody?.friction = 0.0
squareSprite.name = squareSpriteCategoryName
squareSprite.physicsBody?.categoryBitMask = squareSpriteCategory
squareSprite.zPosition = 10
addChild(squareSprite)
// contact in didBeginContact
if firstBody.categoryBitMask == circleSpriteCategory && secondBody.categoryBitMask == squareSpriteCategory {
println("contact works")
}
EDIT 1
I would like to change update the physics body and image of circleSprite to have the following properties:
circleSprite = SKSpriteNode(imageNamed: "recto.png")
circleSprite.physicsBody = SKPhysicsBody(rectangleOfSize: circleSprite.frame.size)
circleSprite.physicsBody?.friction = 0.0
circleSprite.name = newSpriteCategoryName
circleSprite.physicsBody?.categoryBitMask = newSpriteCategory
EDIT 2
I managed to get change the sprite image thanks to help from Max_Power89. But the image has been reduced to the dimensions of the physics body. Please see the relevant code below:
if firstBody.categoryBitMask == circleSpriteCategory && secondBody.categoryBitMask == squareSpriteCategory {
if let newSprite = firstBody.node {
let newImage = SKTexture(imageNamed: "newSprite.png")
let action = SKAction.setTexture(newImage)
newSprite.runAction(action)
}
}
You can implement SKPhysicsContactDelegate
then in your didBeginContact delegate you can do something like this
func didBeginContact(contact: SKPhysicsContact) {
// Return whatever it's the other body
let other = contact.bodyA.categoryBitMask == PhisicsCategory.Player ? contact.bodyB : contact.bodyA
if other.categoryBitMask == PhisicsCategory.Playe1 {
//Change sprite
let deadTexture = SKTexture(imageNamed: "Your image name")
let action = SKAction.setTexture(deadTexture)
player1.runAction(action)
}
}

SKSpriteNode Collision Removal In Removing Wrong One With Xcode Swift SpriteKit

I am making a game where when the character lands on the cloud, the cloud fades out. I put in code to determine when it is hit, there are multiple clouds registered under the same SKSpriteNode, but when it lands on the cloud, the wrong cloud is fading away, it is the most recently added SKSpriteNode that is being removed, not the one it is colliding with.
Is there any way to do it so it only removes the one that the character has collided with, not the earliest one that spawns? Here is the code:
func didBeginContact(contact: SKPhysicsContact)
{
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch(contactMask)
{
case BodyType.PersonCategory.rawValue | BodyType.CloudCategory.rawValue:
let CheckDelay = delay(0.02)
{
self.Cloud.runAction(self.FadeAway)
}
default:
return
Person.physicsBody?.usesPreciseCollisionDetection = true
Person.size = CGSizeMake(self.frame.size.width / 25, self.frame.size.height / 16.25)
Person.physicsBody = SKPhysicsBody(rectangleOfSize: Person.size)
Person.physicsBody?.restitution = 0
Person.physicsBody?.friction = 0
Person.physicsBody?.allowsRotation = false
Person.physicsBody?.affectedByGravity = true
Person.physicsBody?.dynamic = true
Person.physicsBody?.linearDamping = 0
Person.zPosition = 5
Person.physicsBody?.categoryBitMask = BodyType.PersonCategory.rawValue
Person.physicsBody?.contactTestBitMask = BodyType.CloudCategory.rawValue
Person.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) * 1.7)
self.addChild(Person)
Cloud = SKSpriteNode(texture: NormalCloudTexture)
Cloud.zPosition = 7
Cloud.physicsBody?.usesPreciseCollisionDetection = true
Cloud.physicsBody?.affectedByGravity = false
Cloud.physicsBody?.allowsRotation = false
Cloud.size = CGSizeMake(self.frame.size.width / 8.05, self.frame.size.height / 40)
Cloud.physicsBody = SKPhysicsBody(rectangleOfSize: Cloud.size)
Cloud.physicsBody?.friction = 0
Cloud.physicsBody?.restitution = 0
Cloud.physicsBody?.dynamic = false
Cloud.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) / 7.60)
addChild(Cloud)
You need to remove the cloud represented by the physicsBody actually making the contact. so bodyA is either the player, or the cloud, and then bodyB is either the player or the cloud. Then get the actual SKSpriteNode from the physics body. It can be referenced via the node property on the SKPhysics body. So either bodyA.node, or bodyB.node.

How to detect collisions between two fast moving objects?

Hey so I have a problem detecting collisions. I have this project I am making based on Ray Wenderlich's tutorial on How to Make a game like Mega Jump. So everything is works great at the beginning, where platforms and stars are static in one place. But as you go further in the game, stars and platforms start moving (I am using SKActions for this), tried to do this to make it a little harder. But when I get there, collision is NOT being detected at all, player just passes by through the objects like they weren't there. I've been reading everywhere online, I am using precise collision detection but still I see no difference. Any ideas on what can be wrong or what else can I do? Here is a bit of code on how I am doing this:
player = SKSpriteNode(texture: firstFrame)
player.position = (CGPoint(x: self.size.width / 2, y: 50.0))
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.restitution = 1.0
player.physicsBody?.friction = 0.0
player.physicsBody?.angularDamping = 0.0
player.physicsBody?.linearDamping = 0.0
player.physicsBody?.usesPreciseCollisionDetection = true
player.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Player
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.contactTestBitMask = CollisionCategoryBitmask.Star | CollisionCategoryBitmask.Platform | CollisionCategoryBitmask.Monster
foregroundNode.addChild(player)
func createPlatformAtPosition(position: CGPoint, ofType type: PlatformType) -> PlatformNode {
let node = PlatformNode()
let thePosition = CGPoint(x: position.x * scaleFactor, y: position.y)
node.position = thePosition
node.name = "NODE_PLATFORM"
node.platformType = type
var sprite: SKSpriteNode
var spriteFrames : [SKTexture]!
if type == .Break {
let spriteAnimatedAtlas = SKTextureAtlas(named: "CloudBreak")
var cloudFrames = [SKTexture]()
spriteFrames = cloudFrames
let firstFrame = SKTexture(imageNamed: "Cloud02")
sprite = SKSpriteNode(texture: firstFrame)
let move = SKAction.moveToX(self.position.x - 160.0, duration:2.0)
let back = SKAction.moveToX(self.position.x, duration:2.0)
let sequence = SKAction.sequence([move, back, move, back])
sprite.runAction(SKAction.repeatActionForever(sequence))
} else {
let spriteAnimatedAtlas = SKTextureAtlas(named: "Cloud")
var cloudFrames = [SKTexture]()
spriteFrames = cloudFrames
let firstFrame = SKTexture(imageNamed: "Cloud")
sprite = SKSpriteNode(texture: firstFrame)
}
node.addChild(sprite)
node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.physicsBody?.dynamic = false
node.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Platform
node.physicsBody?.contactTestBitMask = CollisionCategoryBitmask.Player
node.physicsBody?.collisionBitMask = 0
return node
}
func didBeginContact(contact: SKPhysicsContact) {
var updateHUD = false
let whichNode = (contact.bodyA.node != player) ? contact.bodyA.node : contact.bodyB.node
let other = whichNode as GameObjectNode
updateHUD = other.collisionWithPlayer(player)
}
struct CollisionCategoryBitmask {
static let Player: UInt32 = 0x00
static let Star: UInt32 = 0x01
static let Platform: UInt32 = 0x02
static let Monster: UInt32 = 0x03
}
Slow the objects down and see if they collide. I had an issue where I was moving my nodes too fast and they were past the collision points when the new frame was rendered. If they move so fast they pass each other's physics body in the frame cycle they will not register a hit.
To detect a collision in that case, you can compare where they are in each frame, and if they have passed each other or their x/y plane values overlap, you can execute your collision code.
The collision is not detected because you didn't set the collisionBitMask.
player.physicsBody?.collisionBitMask = CollisionCategoryBitmask.Star | CollisionCategoryBitmask.Platform | CollisionCategoryBitmask.Monster
node.physicsBody?.collisionBitMask = CollisionCategoryBitmask.Player
Contact and Collision aren't the same thing. You can find more information online.
If it still doesn't work, please show us your CollisionCategoryBitmask to make sure you created it properly.
Edit :
struct CollisionCategoryBitmask {
static let Player: UInt32 = 0
static let Star: UInt32 = 0b1
static let Platform: UInt32 = 0b10
static let Monster: UInt32 = 0b100
}
Here is a table I've made for another question, that might help you too :
http://goo.gl/7D8EGY
Check out my collision physics engine, it's very simple but has support for continuous/predicting/bullet collisions. You might learn something from the code, and it's written in javascript so it should be easy to read.https://github.com/Murplyx/AAE---Axis-Aligned-Engine