-1

I have a game made of little SKNodes moving around. They can not overlap, so I use physics bodies, so that they move around each other. However, in the case that one sknode is animated to follow another, it pushes the sknode ahead. Setting the collision bitmask to 0 makes them overlap, so that is not an option. But otherwise, they push each other way beyond my desired speed. I need a way to get rid of the 'pushing' without overlapping using skphysics bodies. Is there a property I can set to fix this?

Notes: I use skanimations to move my nodes around. If you need any pertinent code, tell me... I don't know where to start. I am using Swift 3.0, but I will accept answers with 2.2/2.3 syntax.

EDIT: The real solution was to change the node's velocity instead of animating movement with SK Actions.

Michael
  • 1,115
  • 9
  • 24
  • Not nearly enough info. 4 posted answers and nothing accepted? – Daniel K Jun 21 '16 at 17:43
  • I'm chatting with people on here that need more info. I haven't had time to perfect the suggested methods, but I'm still trying. When I get to my computer I can add some sample code. If there is certain info you find applicable, please ask @DanielKanaan – Michael Jun 21 '16 at 17:48

4 Answers4

0

Have you tried setting the dynamic property to false on the body being pushed?

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • I have tried that, and it gives me the desired speed. Except, when it isn't dynamic, it freely floats through the walls. – Michael Jun 21 '16 at 14:18
0

After you have set contactTestBitMask you could set this:

myHero.physicsBody!.collisionBitMask = 0

to prevent collisions but permit contacts.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • Is there a way to do this and still prevent overlapping? – Michael Jun 21 '16 at 14:41
  • @Michael Austin with this way you receive always contacts in func didBeginContact(contact: SKPhysicsContact) {} and you decide here if you want to outdistance or superimpose them. – Alessandro Ornano Jun 21 '16 at 14:45
0

Updating position of dynamic physics body with SKAction doesn't work well. It's just like saying - Hey, no matter what physics world rules say just move my node like I want.

First solution - to make one node follow another with some particular distance use SKConstraint.

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let nodeA = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width: 50, height: 50))
        nodeA.name = "nodeA"
        addChild(nodeA)

        let nodeB = SKSpriteNode(color: SKColor.redColor(), size: nodeA.size)
        nodeB.name = "nodeB"
        addChild(nodeB)

        let followConstraint = SKConstraint.distance(SKRange.init(lowerLimit: nodeB.size.width * 1.5, upperLimit: nodeB.size.width * 1.5), toNode: nodeA)
        nodeB.constraints = [followConstraint]
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }


        childNodeWithName("nodeA")!.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 2))
    }
}

If you still want physics bodies and SKActions...

  1. Set "dynamic" property of every physics body to false.
  2. Check in didBeginContact (SKPhysicsContactDelegate protocol) if they are overlapping. If it's true than remove all the actions that are changing their positions. Tip - make the physics body little bigger than it's node.
mikem
  • 112
  • 1
  • 8
  • Thanks, but after setting the constraint, node b never updates the constraint to move. It just stops, while node a continues on. – Michael Jun 21 '16 at 14:39
  • Constraint can't be updated. You can enable/disable particular constraint. You can also remove it and add new one. – mikem Jun 21 '16 at 15:17
  • okay, but I followed your example, and node B no longer follows node A, instead, node B doesn't move after spawn. – Michael Jun 21 '16 at 15:22
  • How do you "spawn" nodeB? It's little hard to help you without sample code:) – mikem Jun 21 '16 at 16:58
  • Maybe "added to the scene" is the correct terminology. The node is a complex AI system. "node B" in this case is offspring of "node A"... I don't decide to "add" node B. The mother, when meeting specific requirements, adds node B. I am not sure what sample code you want. I am simply doing something along the lines of self.addChild... – Michael Jun 21 '16 at 17:03
0

Change the restitution on your physics body of the moving object to 0 when a contact happens, and set the weight of the object you do not want to move really high

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • This is almost what I need, but the following node looks really glitchy, snapping positions – Michael Jun 21 '16 at 15:03
  • I'd like to post a video.. The following node is closely attached to the leader node, but when the leader node slows down, the follower starts rapidly snapping in and out of the leader's physics body, almost like seeing double, until the leader speeds up again. – Michael Jun 21 '16 at 15:08
  • that is what youtube is for, you can use quicktime to screen capture your ios simulator – Knight0fDragon Jun 21 '16 at 15:09
  • are the bubbles suppose to be attached? – Knight0fDragon Jun 21 '16 at 15:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115218/discussion-between-knight0fdragon-and-michael-austin). – Knight0fDragon Jun 21 '16 at 15:24
  • watch the few at the bottom of the screen though, they get "glitchy" – Michael Jun 21 '16 at 15:24