r/GraphicsProgramming • • Aug 25 '23

Question Bullet Or PhysX integration into Metal API?

Hey all! Back here with a graphics question but this time, physics! So I was browsing some demos that apple created within metal and none of them had any opengl demos(apple deprecated it I already know), but wondered how to implement physics in the api. I probably asked question before on here, but to ANYONE who uses metal api but with objective-c or c++, did you ever use on of the physics sdks like bullet or physx or any other engine for physics? And if so, how did you do it? Any comment is welcome here!

EDIT 08/25/2023: OK, clearly I confused everyone here who posted their answers on my question. Bullet and PhysX are examples on which physics engine I'm currently using at the moment(like, I'm using bullet, for example, or I'm using physx, for example). I am trying to ask how to implement one of these engines in the rendering engine powered by the metal api by apple. For opengl, it's no problem. But for metal, that's where I'm asking how to implement the physics engine in the engine that uses metal. Then, when the physics engine is implemented in the rendering engine, I want to create shapes like a cube or a capsule in blender and import those in the rendering engine. Then, once the shapes are implemented, that's when I try to figure out how to implement physics in the shapes that I literally created in blender. Hopefully this all makes sense

1 Upvotes

21 comments sorted by

8

u/memorystomp Aug 25 '23

Jolt Physics, the physics engine from Horizon Forbidden West, is another option. It's new and doesn't have as much documentation but the API is nice and it compiles on Mac fairly easily.

2

u/Kingto400 Aug 25 '23

Won't we get in trouble if we use that for any project?

6

u/lithium Aug 25 '23

mit licensed, in other words, you're free to do whatever you like with it.

7

u/waramped Aug 25 '23

Physics engines and rendering APIs are two totally separate things. Or are you asking about simulating physics on a GPU?

0

u/Kingto400 Aug 25 '23

I should've been more specific. In the rendering engine, I gonna create a simple cube and then a capsule, like in unity(if you used unity before) in blender. So, in blender, I have two shapes that I want to test out for the physics engine. I'm not sure if you can even simulate physics on a gpu

2

u/waramped Aug 25 '23

You could easily write a gpu based physics sim, but that's besides the point :)

Ok, so it sounds like you already know what you need to do then. You are going to simulate some shapes using a 3rd party physics engine, and then render the results in Metal? I think I'm still not clear on your question, do you want recommendations on which engine to use? I would just pick one and run with it.

1

u/Kingto400 Aug 25 '23

I want to use physx since it performs better than bullet(for what I saw on youtube). When I said bullet or physx, I said those just an example if I was using ant of these. Sorry for the whole confusion.

3

u/the_Demongod Aug 25 '23

I don't think you would want to anyways, imagine the latency. You sort of can (PhysX is apparently able to do it) but I wouldn't generally say it's worth it, for typical game applications.

3

u/arycama Aug 25 '23

Generally you use GPU physics for stuff that is visual only and doesn't effect gameplay, eg debris, particles, smoke, etc. There's no latency because it's simulated on the GPU and then rendered on the GPU using indirect draw calls, no CPU involved. (Can also be used for boids and other visual elements that need basic collision logic)

NVIDIA waveworks also supports GPU water physics which can then be read back async to the CPU for gameplay. Latency can be as low as 2-3 frames which may not be noticeable depending on the frame rate/gameplay type.

0

u/[deleted] Aug 25 '23

[deleted]

1

u/the_Demongod Aug 26 '23

What does that even mean

2

u/Rhed0x Aug 25 '23

So you run the simulation using your physics engine (on the CPU), then retrieve the resulting transform (position+rotation) and feed that to the GPU to render your cube.

2

u/arycama Aug 25 '23

Your question isn't confusing but I don't think you understand enough about a rendering API vs a physics API. You don't implement a physics API inside your rendering API.

Your rendering API should interact with some sort of scene/object system that stores transforms, meshes, textures, and any interactive code such as input, cameras, movement etc if needed.

The position/rotation of these objects can then be controlled by a physics system. The physics system should know nothing about rendering and ideally could function completely independently of any rendering API, as it should generally just be reading/writing object transform and collider shape/size data.

Your rendering code should then read this data from your objects. The renderer and the physics should know nothing about eachother. Therefore it doesn't really matter which physics engine or rendering API you use as they should not need to know anything about eachother, as they do completely different things and function independently of eachother.

1

u/Kingto400 Aug 25 '23

Correct me if I'm wrong, just import a modeI made in blender and then code in the physics?

19

u/lithium Aug 25 '23

Any physics engine worth its salt should be completely renderer agnostic, there's not really any overlap in concerns whatsoever. As long as your renderer is capable of drawing physics primitives like boxes, capsules etc, there's no reason why it wouldn't just plug right in.

2

u/jmacey Aug 25 '23

I use Bullet, basically the sim runs for each body I get the transform as a matrix and apply it to the objects I'm rendering. This version is OpenGL not Metal but may help to get the idea.

In this demo I use the built in primitive types and a few meshes (meshes are slower but I use a lower res proxy geo for the sim). Basically the rendering and sim are separate with the sim driving the render.

https://github.com/NCCA/BulletNGL/tree/main

This does run on a mac but needs my NGL library too.

It may be easier to use this simple demo of a single body falling https://github.com/NCCA/NCCABoilerplate/tree/main/Bullet as a start point. It prints out the position which you can use to generate your transform.

1

u/Proud_Instruction789 Aug 27 '23

Sorry for not replying, so this is opengl of course, but is it possible that I can port this to the metal api?

1

u/jmacey Aug 28 '23

Totally, you just use the same transform data and pass it to the metal shader. Main issue is going to be if you use C++ or Swift when using metal.

The physics has nothing at all to do with the graphics.

1

u/Proud_Instruction789 Aug 28 '23

How's C++? I'm no fan of swift

1

u/jmacey Aug 29 '23

You will need to use the metal c++ api. https://developer.apple.com/metal/cpp/ You still need to create the metal context which is easy with the native Swift API's but harder with others (I've tried using GLFW and SDL but it's quite complex last time I tried).

2

u/saddung Aug 25 '23

Your question doesn't make much sense, graphics & physics are two separate things, and running physics on the GPU is mostly just stupid.