The O.G. Virtual learning! Buy one, get 2 more Lifetime Memberships! - Click Here!

How do you create multiple node characters?

Home 4 Forums Swift and Sprite Kit How do you create multiple node characters?

This topic contains 3 replies, has 2 voices, and was last updated by  Justin 5 years, 2 months ago.

  • July 18, 2016 at 6:08 pm #153116

    I am creating a game and for my character his legs and torso have separate animations so they are separate nodes with separate physics bodies. I am having a lot of trouble linking the torso and the legs together however, linking them is not the problem, keeping them linked is. While moving around sometimes the hero’s torso slides off of the legs. Kind of funny but not practical lol. Here is my physics coding

    `enum BodyType:UInt32 {

    case bullet1 = 2

    case enemy1 = 4
    case enemy2 = 16
    case enemy3 = 32

    case desertForegroundCase = 64

    case tank11 = 128
    case tank12 = 256
    case tank13 = 512

    case tank21 = 1024
    case tank22 = 2048
    case tank23 = 4056

    case tank31 = 8112
    case tank32 = 16224
    case tank33 = 32448

    case cliff1 = 64856
    case cliff2 = 129212

    case soldierT = 258424
    case soldierL = 516848

    static let ground: UInt32 = 1
    func CreateScene (){

    desertBackground.position = CGPoint(x: frame.size.width / 2, y:
    frame.size.height / 2)
    desertBackground.size = CGSize (width: 1136, height: 640)
    desertBackground.zPosition = -5
    desertForegroundImage.position = CGPointMake(568, 100)
    desertForegroundImage.zPosition = -1
    let desertForeground = SKSpriteNode(texture:
    desertForegroundTexture, size:CGSize(width: 1136, height: 200))
    desertForeground.position = CGPointMake(568, 100)
    let desertForegroundBody = SKPhysicsBody(texture:
    desertForegroundTexture, size: CGSize(width: 1136, height: 200))
    desertForegroundBody.dynamic = false
    desertForegroundBody.affectedByGravity = false
    desertForegroundBody.allowsRotation = false
    desertForegroundBody.categoryBitMask = BodyType.ground
    desertForegroundBody.contactTestBitMask = BodyType.enemy1.rawValue
    | BodyType.enemy2.rawValue | BodyType.enemy3.rawValue |
    BodyType.soldierL.rawValue | BodyType.soldierT.rawValue
    desertForeground.physicsBody = desertForegroundBody
    desertForeground.zPosition = -1

    self.addChild(desert gully)

    func CreateHero (){

    soldierLegs.position = CGPoint(x: 405 , y: 139)
    soldierLegs.zPosition = 1
    soldierLegs.anchorPoint.x = 0.6
    soldierLegs.anchorPoint.y = 0.7

    let soldierLegsBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize:
    soldierLegsBody.dynamic = true
    soldierLegsBody.affectedByGravity = true
    soldierLegsBody.allowsRotation = false
    //body.restitution = 0.4
    soldierLegsBody.categoryBitMask = BodyType.soldierL.rawValue
    soldierLegsBody.contactTestBitMask = BodyType.enemy1.rawValue |
    BodyType.enemy2.rawValue | BodyType.enemy3.rawValue |
    soldierLegs.physicsBody = soldierLegsBody

    soldierTorso.position = soldierLegs.position
    soldierTorso.zPosition = 2
    soldierTorso.anchorPoint.x = 0.25
    soldierTorso.anchorPoint.y = 0.1

    let soldierTorsoBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize:
    soldierTorsoBody.dynamic = true
    soldierTorsoBody.affectedByGravity = true
    soldierTorsoBody.allowsRotation = false
    soldierTorsoBody.categoryBitMask = BodyType.soldierT.rawValue
    soldierTorsoBody.contactTestBitMask = BodyType.enemy1.rawValue |
    BodyType.enemy2.rawValue | BodyType.enemy3.rawValue |
    soldierTorso.physicsBody = soldierTorsoBody
    let joint =
    SKPhysicsJointFixed.jointWithBodyA(soldierLegs.physicsBody!, bodyB:
    soldierTorso.physicsBody!, anchor: soldierLegs.position)

    soldierLegsBody.collisionBitMask = BodyType.ground
    soldierTorsoBody.collisionBitMask = BodyType.ground


    with this, coding the hero’s torso keeps sliding off of the legs when moving around. I have tried setting the dynamic value of the torso to false however, when i do that the torso ignores gravity so every time the character jumps the torso does not come back down. Is there a way to just code one physics body with 2 separate nodes? or am i just missing a little code? Any help is help, thank you.

  • July 18, 2016 at 7:11 pm #153118

    Yeah I’ve seen some crazy stuff happen before with physics and parented bodies. I think you need to have a rock-solid reason to setup your character that way if you’re going to try it. I don’t think the parenting in Sprite Kit is really meant for character style rigs.

    If the only reason you’re separating them is for animations, I think you’re better off making more animation sequences and just controlling when those run. Even if you have to export out like 16 separate sequences, thats gonna be easier than wrangling the physics bodies.

    • July 19, 2016 at 2:16 pm #153147

      yes i do have a rock solid reason lol, it is a shooting game with a joystick so his torso needs to be able to move around and aim and his legs, need to be a able to walk forwards, backwards and jumping. while aiming. The torso rotates 360 degrees. I would have to combine 30 aiming frames, with 50 walking frames with 12 different guns and 6 different characters. So i would have to match up and create 30 x 50 x 12 x 6 frames.

  • July 19, 2016 at 2:22 pm #153148

    Just making sure. Some people come from a 2D or 3D animation background, and think in terms of rigging up inverse kinematics.

    Obviously, You still need to rethink the best approach though if the current one isn’t working. Just because the main parent body is affected by physics, doesn’t mean the children need to. You could still rotate the children with or without them having physics bodies. That could be as simple as

    torso.zRotation = torso.zRotation + 1

You must be logged in to reply to this topic.

Know what an affiliate program is? You make money just by sharing links to our site! Win. Win.

Earn when you refer any buyer here! 30 day tracking. Commissions are 33%-50% and recur on subscription products!

Uh oh, missing your affiliate ID. Create one real quick