Build a swinging hammer with SKPhysicsJointLimit and SKPhysicsJointPin

Here comes another little tutorial how to make a swinging hammer with Sprite Kit for your iOS game. It’s very easy to build such little objects with the given physics engine in Sprite Kit. All you need is the idea how you can use physical bodies and physical joints to realize the object you want.

Here comes the class code and some notes for a swinging hammer as you can see in this video capture.

When I start to write a class I think about the physical elements first. As you can see in this image we need only 3 elements: an anchor, a rope and the hammer.

swinging-hammer-physics-debug_

The anchor is a physics body. The rope is a SKPhysicsJointLimit and the hammer is again a simple physics body with a huge mass value. So let’s see the code

The anchor:

The rope:

The Hammer:

I use some references and variables in this code snippets that are given from the method call of the complete class. So I can call this class in every level as often as I need it with a configurable position and rope length. Here is an example for the call of the hammer swing class:

For some realistic effects I do simply some collision detections in the Sprite Kit game loop to play a sound and shake the background if the hammer smashes something:

Additionally I add this typical swoosh sound in my game loop if the hammer is moving downwards to make it more realistic:

So here is the complete hammer swing class code in objective-c for Sprite Kit in Xcode to add for an 2D iOS game. At the end of this class all physical built objects are pinned together with joints.

 

 

 

 

6 thoughts on “Build a swinging hammer with SKPhysicsJointLimit and SKPhysicsJointPin

  1. Hi
    Thanks for all this short bits for achieve cool physics mechanics with Sprite-Kit.

    I have read your other tutorials and I have a question:

    Why don’t you just instance the SKnode as a node for the anchors instead a transparent pixel png file?

    • Hi Juan,
      there‘ s no specific reason for this. It’s kind of WIP… just to keep the possibility to replace this dummypixel also with some graphic/artwork.

  2. Hi
    Thanks for all this short bits for achieve cool physics mechanics with Sprite-Kit.

    I have a question:

    how you will call the ‚addHammerSwing‘ method in ‚initWithSize‘ method in your Scene?

    Please replay with code in example.

    Thanks in advance.

    • Hi Akshay,
      first remember to make the method publicly available in your „WWHammerSwing.h“ like this:
      +(void) addHammerSwing:(CGPoint)pos ropeLength:(int)ropeLength game:(SKScene *)game start:(int) start;

      In your class with the initWithSize method you also have to import this header file like this:
      #import "WWHammerSwing.h"

      Then you can call the hammer method in your initWithSize like this:
      -(id)initWithSize:(CGSize)size {
      if (self = [super initWithSize:size]) {
      [WWHammerSwing addHammerSwing:CGPointMake(500, 330) ropeLength:250 game:game start:1];
      }
      return self;
      }

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
*
Website