How to use bullet physics as a System Module

Hello!

First of all I would like to apologise if this is a very basic question, or if this is not the correct place to ask.
I've been working on a rendering project lately to learn about graphics programming with Metal. I was now looking for a physics library to use, and bullet seems like a fairly well documented one so I decided to give that a shot. However I have not been succesful in including it in my project.

My project is set up to use different local Swift Packages that get imported as needed. So in this case my issue lies with my "IAEPhysics" package.

it has the following structure:
IAEPhysics/
β”œβ”€β”€ README
β”œβ”€β”€ Package.swift
└── Sources/
. β”œβ”€β”€ IAEPhysics/
. . . β”œβ”€β”€ shim.h
. . . └── module.modulemap
. └── IAEBullet/
. . . └── PhysicsController.swift // here I import IAEBullet

So the idea is here that my physics controller takes care of physics related things while making use of bullet, thus bullet does not necessarily need to be exposed outside of the package.

First I tried to make a separate package out of bullet similarly as described here:

But that seems to have been outdated and this should have been the current way to do it:

I also came across another post here: How do I fix linkages for a system-module package following a brew update?
And I found a relevant repository on OP's github: GitHub - spencerkohan/Swift-Cbullet: A swift system module for the Bullet Physics

However I've yet to find the correct way to put this information together to get it to work. Hopefully someone here can point me in the right direction.

Package.swift

import PackageDescription

let package = Package(
    name: "IAEPhysics",
    platforms: [.macOS(.v11)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "IAEPhysics",
            targets: ["IAEPhysics"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(path: "../IAEFoundation"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "IAEPhysics",
            dependencies: ["IAEFoundation", "IAEBullet"]),
        .systemLibrary(name: "IAEBullet", pkgConfig: "bullet", providers: [.brew(["bullet"])]),
        .testTarget(
            name: "IAEPhysicsTests",
            dependencies: ["IAEPhysics"]),
    ]
)

module.modulemap

module IAEBullet [system] {
//  header "btBulletCollisionCommon.h"
  umbrella header "shim.h"
  link "bullet"
  link framework "BulletDynamics"
  link framework "BulletCollision"
  export *
}

shim.h

#include <BulletCollision/btBulletCollisionCommon.h>
#include <BulletDynamics/btBulletDynamicsCommon.h>

The errors I'm seeing now are:

  • Could not build Objective-C module 'IAEBullet'
  • A number of errors similar to: Unknown type name 'class'; did you mean 'Class'? (also for 'false' , 'btScalar', 'bool', ...)

What is the correct way to do this? Is it even possible to do this? In case not, what would be a good alternative way, or an alternative framework that I could use?

Thanks a lot in advance and have a nice day!

I don’t exactly know what’s causing your errors, but you might be able to figure it out if you take a look at these swift bindings for bullet physics. They include bullet physics as a system library like you are trying to do. Hopefully that helps :+1:

1 Like

Hello!

Thanks for the reply I am going to try it out, I hadn't come across that one. I am curious to see what I did wrong. First thing that comes to mind might be those compiler flags. I will report back what the solution turned out to be!

So this was indeed the solution, thanks a bunch! I've been trying to import it for so long..

So the main issue with what I was trying to do above was the shim.h importing:

#include <BulletCollision/btBulletCollisionCommon.h>
#include <BulletDynamics/btBulletDynamicsCommon.h>

That gives those compilation errors. The imports from the linked repo works, now I just need to figure out how to use and implement bullet. But at least I am no longer stuck.

Thanks again! @stackotter

1 Like