Skip to content

Conversation

Brackets-Coder
Copy link
Contributor

@Brackets-Coder Brackets-Coder commented Feb 25, 2025

Ammo Physics

A physics extension based on the ammo.js physics library, which is a direct port of the world-renowned C++ Bullet Physics SDK.

The goal is to provide feature-complete, advanced, and performant 3D physics in a simple-to-understand manner. This extension aims to be consistent with Box2D and Simple3D.

video.mp4

Task list:

  • Shapes
    • Boxes
    • Spheres
    • Cylinders
    • Cones
    • Capsules
    • Convex Hulls
    • Triangle Meshes
    • Compound bodies
    • There are some others (planes, heightfields), but I don't want to support these
  • Physical Materials (friction, restitution, etc.)
  • [ ] Constraints (All types)
  • Collision:
    • Collision Detection
    • Raycasting
    • Enable/disable collision response
  • Manual Impact forces (like "push with force" block in Box2D)
  • Bug fixing

Things I'm considering now or for a later update:

  • Vehicle support
  • Debug rendering support
  • Soft Bodies

Miscellaneous Notes Mainly for Moderators:

  • ChatGPT was used to assist and enhance the making of this extension, simply because I couldn't find good quality, coherent documentation of Ammo.js, and attempting to figure out how to use it via the developer inspector isn't fun. It did not write the code for me and I am still the main author of this extension. It just helped me learn Ammo.js. Hope this doesn't cause conflicts.
  • I'm not good at graphic design so if someone wants to make a banner/thumbnail that'd be great
  • Still have to incorporate Scratch.Cast where necessary, right now I'm just working on functionality though

Put an emoji (👍, 🚀) if you like this extension

@Brackets-Coder Brackets-Coder marked this pull request as draft February 25, 2025 22:31
@github-actions github-actions bot added the pr: new extension Pull requests that add a new extension label Feb 25, 2025
@WAYLIVES
Copy link

WAYLIVES commented Mar 4, 2025

Wow! It's very cool and useful for people like me)

@Brackets-Coder
Copy link
Contributor Author

Wow! It's very cool and useful for people like me)

Thank you! I've been making steady progress, but it probably won't be ready for a while (which is why this is a draft).

@Brackets-Coder Brackets-Coder changed the title Ammo Physics Ammo Physics (3D Physics Extension) Mar 19, 2025
@Brackets-Coder
Copy link
Contributor Author

Added partial support compound bodies - all child shape types.
Also working on convex and concave mesh support... it'll be a while.

@WAYLIVES
Copy link

wow

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Apr 2, 2025

Finally got convex hulls working 🥳 🎉

video.mp4

Still have to optimize and bug fix everything and add more features

@Brackets-Coder

This comment was marked as outdated.

@Xeltalliv
Copy link
Contributor

It's a bit complicated. Simple3D itself can have meshes represented in 3 ways: triangles, triangle strips and triangle fans. Positions of each of the vertices can either be listed directly, or be specified once per unique position and referenced multiple times from the list of indices, to share one vertex between multiple primitives. Triangles require 3 vertices per triangle. Triangle strips and fans require 3 vertices for the first triangle and only 1 vertex for each subsequent triangle, where 2 old vertices get reused. In case of triangle strip it reused 2 immediately previous vertices. In case of triangle fan it reuses 1 previous vertex and the 1st vertex. Index of 0 can be used as a separator to have multiple unconnected triangle strips/fans.
image

The built-in OBJ importer doesn't use any of those modes and always just outputs a simple list of triangles pre-processed using fan triangulation algorithm. It does not handle concave n-gons.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder

This comment was marked as outdated.

@Brackets-Coder
Copy link
Contributor Author

Also, Ammo.js seems to support wireframe debug rendering. I might want to add a layer to the canvas that can show contacts, convex hulls, velocities, etc.

@Xeltalliv
Copy link
Contributor

Xeltalliv commented Apr 11, 2025

This looks like a faulty collision detection. Changing framerate, sub-steps and solver iterations is mostly useful when you have a lot of bodies interacting in a chain reaction, which isn't the case here.

I could try using a more specialized collision algorithm but I don't want to sacrifice performance for simpler simulations.

Not sure exactly what you are referring to, because there isn't much documentation, but I asked ChatGPT how to correctly use btGImpactMeshShape and it on it's own mentioned that using btGImpactCollisionAlgorithm is absolutely mandatory for it to work correctly.

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Apr 11, 2025

This looks like a faulty collision detection. Changing framerate, sub-steps and solver iterations is mostly useful when you have a lot of bodies interacting in a chain reaction, which isn't the case here.

I could try using a more specialized collision algorithm but I don't want to sacrifice performance for simpler simulations.

Not sure exactly what you are referring to, because there isn't much documentation, but I asked ChatGPT how to correctly use btGImpactMeshShape and it on it's own mentioned that using btGImpactCollisionAlgorithm::registerAlgorithm is absolutely mandatory for it to work correctly.

I'm already registering the GImpactCollisionAlgorithm, but ChatGPT suggests using a more narrow phase parent collision algorithm... I don't really think that will affect anything. Looking at the official demos, btDbvtBroadphase is being used and that's what I'm using. I could try to see if I'm registering the impact collision algorithm correctly, but I think I am:

    let dispatcher = new Ammo.btCollisionDispatcher(collisionConfig);
    Ammo.btGImpactCollisionAlgorithm.prototype.registerAlgorithm(dispatcher);

@Brackets-Coder
Copy link
Contributor Author

it's because of the prettier update pull request you merged where now your local prettier doesn't match what's on your pull request branch

that's why the pull request wasn't merged for a long time

rebase, reinstall, reformat, itll go away

Oops sorry just trying to help

@Daniel-Dami
Copy link

this extension is super cool!!

@Brackets-Coder
Copy link
Contributor Author

this extension is super cool!!

Thanks! Apparently though, it's too large, so I'll have to find some way to compress the library further

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Sep 22, 2025

Previous commit reduces the file size ≈0.2 MB
The extension is now only 1MB (1020 KB, around half the original size); I don't think I can do much better than that without breaking functionality.

@Daniel-Dami
Copy link

that's really great!

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Sep 29, 2025

Previous commit reduces the file size ≈0.2 MB
The extension is now only 1MB (1020 KB, around half the original size); I don't think I can do much better than that without breaking functionality.

that's really great!

@CubesterYT "The weight loss update" lol

@unknown07724
Copy link

I feel like merging this with the simple3D extension might work, not sure

@Brackets-Coder
Copy link
Contributor Author

I feel like merging this with the simple3D extension might work, not sure

Personally, it probably wouldn't work considering all of the custom transformations that Simple3D relies on. Additionally, it's a different category of extension, so instead of bloating it just keeping it independent will make it more accessible for integration into other 3D renderers.

I highly considered accessing Simple3D's i_will_not_ask_for_help_when_this_breaks API to manually update transformations upon simulation step, but it's integration requirements with the vast possibilities of Simple3D doesn't make it feasible or logistical. How do I know what objects are what? I can't just assume meshes because those could be instanced, and a project could have any number of ways of implementing that, etc...

So I find it best that users integrate both extensions manually for a custom-tailored experience that they're familiar with their own code.

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Oct 1, 2025

Honestly I think I should just crank out getting constraints figured out and finishing documentation

If all goes well this can be merged after that but there are several concerns that need to be addressed

@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Oct 8, 2025

@CubesterYT @Xeltalliv
How crucial is it that I have constraints on the initial release? I was considering polishing a few things, adding a small block here and there, and just opening for review without them, then adding them later.

How many users would require constraints for their projects? How many users could I expect to use this at all? Would it be safe/acceptable to put off advanced features (constraints, vehicles, kinematics, etc.) for later?

@Brackets-Coder Brackets-Coder marked this pull request as ready for review October 8, 2025 17:06
@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Oct 8, 2025

Alright, I think this is ready for review now
I'm really busy with things outside of Turbowarp right now so I'm in a rush and I might have missed something but I guess that's what reviews are for

I don't have a sample project yet because they would all use localhost instead of extensions.turbowarp.org

The gallery banner is mostly a placeholder but I found it "good enough," let me know if it needs more polish.

@CubesterYT and @GarboMuffin Due to the size and complexity of this extension, I'd like the most in-depth reviews possible to guarantee stability and polish. Although faster is nicer, I'm not on a time crunch so take whatever time you need.

@Xeltalliv due to your experience with this kind of thing I'd be nice if you could look at a few things as well like memory management or OBJ parsing

Let me know if there's anything I can do to help.

@Brackets-Coder Brackets-Coder marked this pull request as draft October 9, 2025 00:46
@Brackets-Coder

This comment was marked as resolved.

@Brackets-Coder Brackets-Coder marked this pull request as ready for review October 9, 2025 02:46
@Brackets-Coder
Copy link
Contributor Author

Brackets-Coder commented Oct 9, 2025

Added two blocks that detect if a body or any body is active (a.k.a. "fallen asleep")

Previous commit makes this ready, no known issues that I'm aware of
I've done extensive testing so if there's still something I missed please let me know

I'll be working on a slightly more professional banner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: new extension Pull requests that add a new extension
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants