1

After trying for ages, i was not able to integrate neither the c++ version or some swift port for box2d i found on github.

I tried adding an objective-c file then auto-generated a bridge header file not knowing how and where to add the Box2D/Box2D.h file.

Simply adding the entire Box2D folder into my project and trying

let world = b2World(...)

does not work, but why it does not find the Box2D files?

How do i integrate Box2D into a new blank Swift SpriteKit project in XCode?

NovumCoder
  • 4,349
  • 9
  • 43
  • 58

1 Answers1

1

As @KinghtOfDragon already said in a comment, Box2d has already been integrated into SpriteKit. And you don't need to use C++, everything is perfectly available to Swift (or Objective-C).

The subject is described into the SpriteKit Programming Guide. You will also find plenty of Q/A here on StackOverflow searching for these tags

A couple of examples

Creating a Physics Body for a Sprite

This snipped created a physics body matching the texture of a sprite

let sprite = SKSpriteNode(imageNamed: "enemy")
sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.texture!.size())

The Physics World

Inside your GameScene class you'll find the physicsWorld property. It allows you to change the parameter of the physics world of your game. E.g. you can remove gravity simply writing

physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
Community
  • 1
  • 1
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • Strange, because i use that already but have a big issue with collision detection. some people here at stackoverflow recommended to switch to box2d instead. check my question about this issue: http://stackoverflow.com/questions/38815820/swift-physics-wrong-angle-calculation-on-collision – NovumCoder Aug 08 '16 at 23:26
  • looks like this is not true. other tell me spritekit has another engine? Where you guys have this info? Is this documented officially anywhere on Apple sites? – NovumCoder Aug 14 '16 at 22:54
  • @NovumCoder I suspect it's BulletPhysics with an axis turned off, and then the API made to look similar to Box2D. A few things made me suspect this. The units, the lack of a spring similar to Box2D (and its favoured mouse/touch drag facility) and the fields also being more like what Bullet does. It's also apparent Apple has contributed hardware optimisations to Bullet for ARM Neon and SIMD on Intel. Plus they were using (what looks like) Bullet before UIKitDynamics and SpriteKit were released, in SceneKit. – Confused Oct 01 '16 at 22:49