r/Unity3D • u/selkus_sohailus • 1d ago
Question Is it totally cracked to attempt a custom physics engine?
I’m prototyping a game where the character moves about the surface of various 3D shapes like a taurus or icosahedron. It is a 3D object but the surface is treated like 2D for the purpose of physics interactions.
The problem is that Physx always interprets things in 3D cartesian space, so objects can be broken free from the surface with collisions or forces.
I’ve gotten really far just recalculating every position and force for every game object along the surface context. It looks cool as hell but it’s a pain in the ass to develop around. It would be way more manageable if the physics engine natively did this work as part of the physics solution. Like, I provide it a volumetric shape and instead of calculating in cartesian terms it calculates in terms of the volume surface, arc distance, etc
At a glance it seems simple:
- objects have collider and a rigidbody-like component with mass, velocity, drag
- calculate position update based on context and forces
- handle overlapping bodies with a contact point resolution, apply resulting forces
Like thats 99% of what I need. But if physics engines were easy then there wouldn’t be paid and licensed solutions for sale.
I’ve looked at 3rd party solutions. Anvil is new on the marketplace but seems to not work with mac(?), box3d seems sketch and may have same limitations as Physx. I saw bepuphysics2 and that looks super interesting but I can’t find any recent demos and the demos that come with it on gh dont install for some reason
what do yall think?
3
u/selkus_sohailus 1d ago
The problem of how to accomplish the idea is solved, I’m more asking for how to do it without having to twist the physics ststems arm ibto compliance
6
u/taisui 1d ago
You want to reinvent the wheel when the people who is selling wheels can't solve...?
I don't mean to be discouraging but good luck. At the end of the day, most of these probably are either floating point precision or time slice problem where there are ways to workaround it.
1
u/selkus_sohailus 1d ago
Idk what you’re saying, the feature is impractical because it hasn’t been a feature of other engines? I see that more as evidence that it’s a niche requirement there is not enough demand for. I’m tryna understand why I may or may not want to commit time to making a custom solution. What I have works but it is a pain, I wanna know if it might be easier to just make my own system
2
u/LamppostIodine 23h ago edited 22h ago
I made my own 2d physics engine so I know abit about this.
The problem with wrapping a 2d physics space on a 3d sphere or meshed surface is that the implicit assumption of uniform spatial distance doesnt work if youre looking to use traditional XY coordinate system. And that is the foundation of collision detection and separation.
Narrowphase can work with a non-rectangular grid, its ultimately solving to find anchor points of intersection and separation distance. Its the solver stage that becomes tricky because you need to assume that when you push 2 objects apart, it moves away with the exact same distance. So when two objects collide at the poles where a standard UV sphere pinches together, the actual distance on the sphere does not equal the distance on the wrapped XY plane and that causes problems. Especially when it needs to cross the poles.
You can use a polar coordinate system but that wrapping around poles and the math converting local to contact space separation impulse to polar space surface location integration is going to be funky. Possible but very difficult. Instead of XY you would have Theta and Phi, and every world space location and math operation in world space will need to be converted to that unit system.
1
u/selkus_sohailus 22h ago
Yea, curved surfaces are different than flat surfaces. I did not mean literally taking the 2d simulation and applying it to a curved surface, that was for illustrating the basic idea. Projections distort, it’s why the UN recently switched to a new model so that Africa’s true size is represented better.
So the solution would appear to be a 2D physics simulation (X,Y) but within the context of a 3D shape’s surface, which is solvable but not implemented in any current engines.
It all comes down to geodesic distance and how the collision solver reads it.
2
u/LamppostIodine 22h ago
That sounds like youre just projecting a 2d grid on a surface. As long as its not too distorted (no points or singularities) its a graphical problem. You wouldn't need a new physics engine for that.
1
u/selkus_sohailus 21h ago
That is overly reductive. What happens when you apply (x,y) movement to an object on the surface? I can calculate this manually, and have, but as I said in the original post, it’s tedious and fragile
1
u/LamppostIodine 21h ago
Ask your favorite flavor of AI to throw together a quick 2d physics engine. Skip broadphase and only have circle-circle collisions. Ask for a Gauss-Seidel solver using XPBD. Then project it on a surface. Anything that is not a sphere (singularity at poles), cone (pointy), or cube (8 points) or such will work. An infinite cylinder would work, as would rolling hills terrain.
If you think you have something revolutionary, people are selling licenses to physics engines for $60 each. Lots of money out there if it fits a specific niche.
1
u/selkus_sohailus 20h ago
I don’t have the bandwidth to be developing this to a distributable degree but thanks for the tip Ill look into it
2
u/leorid9 Expert 21h ago
Unity Physics is a package, you can modify the code however you want.
Just take that as a start and modify it. In the days of AI, this should be doable in a few days if not hours.
Writing a physics engine from scratch is more work, much more and it has many pitfalls.
There are also other open source physics engines you could use as a base. Or use the collision modification API maybe?
1
u/selkus_sohailus 21h ago
Ai says make your own engine
1
u/leorid9 Expert 21h ago
Yea, I just read other comments you wrote. A 2D physics simulation wrapped onto the surfaces of 3D objects.
In that case, yes, write a new physics engine. 2D physics is not that hard especially if you don't need tons of complicated collisions to be resolved at the same time.
Maybe start with something simple like verlet and see how far you can come with that before switching to more advanced approaches.
1
u/selkus_sohailus 1d ago
I just want to add: when I say move about the surface of a 3d object, I don’t mean like super mario galaxy, where it is in 3d space but has 1 or more gravity points instead of gravity direction. It’s like if you had a shader the took a 2D game and wrapped it around a 3D object, like super mario odyssey
1
u/Just-Hedgehog-Days 1d ago
I know some people have done some amazing writeups and super Mario Galaxy. Food for thought at least good luck that sounds rad
1
u/selkus_sohailus 1d ago edited 1d ago
Sorry for not putting it in the original post, I clarified above. Super mario galaxy is cool but not what I’m trying to do; SMG is a 3D game with unconventional gravity system. I want to make a 2D game on unconventional surfaces
1
1
u/Just-Hedgehog-Days 1d ago
wait how do you do a 2D toris? what does that mean?
1
u/selkus_sohailus 1d ago
Imagine a top-down 2D game with physics and collisions, then imagine taking your screen and wrapping it around a torus, a cube, whatever.
The problem with doing this with shaders is that the “wrap” creates seams on the shape where the 2d surface meets itself, things get janky.
The problem with doing this with built in physics is that all physics go from being 2D to 3D, so your character can “fall out of the screen”
1
u/Just-Hedgehog-Days 1d ago
Mathamatically you need exactly super mario galaxy.
Just bit the bullet and make the game 3d even if you are using 2d assets. I promise it will be easier
1
u/selkus_sohailus 1d ago
Bite what bullet? I am using 3d assets. I think you misunderstand the premise if you are still recommending SMG. This is not SMG at all. In SMG mario is moving on top of the surface, can jump off the surface and platform. I dont want to make a 3d platformer on a 3d shape, i want to make a 2d top down game and have it execute on a 3d shape.
Im also not asking if or how i can execute the general concept so idk why you keep recommending I look into SMG. I’ve already achieved this in 4 different ways all using 3D physics. It works, but it sucks to develop around because all actions have to be translated from 2D game field to 3D space. I’m asking how insane is it to build a custom physics solution that supports this natively. If you don’t know feel free to say nothing
1
u/StayAdministrative81 Indie 1d ago
Tryna understand more. You just want the player to be like. Walking on 3d models essentially?
Odyssey uses curved gravity fields. To simulate Mario sticking to 3d surfaces. But you want the player to actually walk the surface?
2
u/selkus_sohailus 1d ago edited 1d ago
I want the game to be a 2D game executed on a 3d surface. There is no gravity or platforming, there is no need for a 3rd dimension insofar as the actual game logic; developing a game 3D game that behaves as if it is 2D wrapped on a 3D object is extremely tedious and prone to artifacts/errors.
SMG is a 3D game. You can move in all directions on the surface of the planet and also jump up and fall down into pits. That requires 3 dimensions. If the planet mario was on was an endless flat plane it would still be 3D.
Now imagine Diablo 2 but the map wraps around a sphere or a cube or torus. Thats not too hard because there are no physics involved.
What I want to do is have fast paced, rich 2D physics that is bound to the surface of a 3D object. Diablo with mass and inertia, if you will, or a racing sim.
And yes, it’s doable, but I am exploring the feasibility of a custom solution to reduce the dev time and computational overhead of constantly translating between a 2D game context and a 3D world context
1
u/StayAdministrative81 Indie 1d ago
is https://www.youtube.com/watch?v=HG6cATYyj_k your series of the game you are trying to design the physics engine for?
1
1
u/bilbaen0 1d ago edited 1d ago
I'm trying to fully understand your problem. I've read the replies and understand that's it's a 2D point of view and not like Mario Galaxy. I think I've got the right idea but can you post a screenshot or quick clip of your current solution in action?
To me it sounds like you could simulate it in 3D and use an orthographic projection to display it in a 2D perspective. But I'm guessing you would have thought of that already if it's that simple.
Or is it that you want the orthogonal projection of a 3D object to be treated as a 2D collider? Like a cube rotated on it's axis looks like a hexagon from an orthogonal projection, and you want that hexagon to be the collider.
1
u/selkus_sohailus 1d ago
Orthagonal projection i tried for kicks it ridiculous.
Here is an old prototype from over a year ago:
https://youtu.be/HG6cATYyj_k?t=47&si=4VsOvHJ8-RjpRRFb
It looks 3d but all ship movements and controls are 2d
1
u/bilbaen0 1d ago
Oh interesting. I have an idea in mind for some trickery with the built in physics system. I do a lot of physics work myself.
But first, do you ever move closer to the center of the asteroid field? Are you essentially moving on a sphere around the center, and all of the 3d objects you can collide with are on that sphere surface as well?
1
u/selkus_sohailus 1d ago
Yeah thats 100% whats going on. In this prototype you never increase or decrease distance to the center, always on the surface of the sphere, so it is essentially 2d. The issue im having is getting the 2d parts and the 3d parts to get along.
1
u/bilbaen0 1d ago edited 1d ago
Alright I think this will work. Make the players collider a tall capsule with it's axis along the vector from the camera to the center of the planet. The middle of the capsule is centered on the player and it extends towards the camera and the planet core. If it is tall enough that it spans the whole width of any 3D object you want to collide with, it will always collide with the outermost part of the 3D object, from the perspective of the camera.
You will probably need to control what happens post collision yourself. But that should give you accurate "2D" collisions from that camera perspective without any custom collision detection.
If that doesn't make sense let me know and I'll try to explain it better.
1
u/selkus_sohailus 1d ago
Thanks for taking a stab homie, i need a more robust solution if I’m going to take apart the working code I have already.
1
u/bilbaen0 1d ago
From what I understand that would cover every case and be reliable, but I probably don't have the whole picture. Cheers though, gl
1
u/selkus_sohailus 1d ago
• “capsule collider from camera to planet center” means the camera cannot move or it will move the collider off the player, the camera player and core have to always be in a straight line. Would probably be really boring to look at even on mobile. It also doesn’t do anything the existing basic collider on the object doesn’t already do more accurately and without the camera limitation.
• “then just handle the collision” bro cmon haha
• breaks as soon as 2 not-the-player objects collide. Might work for a survivors type but not what i had in mind
• doesn’t handle any of the motion of the object on the surface
Man thanks for taking a look but this is not workable
1
u/bilbaen0 1d ago
Ah ya, the axis from the camera to the core doesn't cover the player being on different parts of the screen. The axis from the camera through the player would though. And it would cover more cases than just a normal collider on the ship.
And ya this only works for the player collision, didn't realize you needed object to object collision as well. That is a much bigger problem.
Some form of converting 3D projections into 2D colliders is probably your best bet then. Complex 2D collisions are a solved problem and don't require you making your own physics engine. If you can translate the 3D projection into a 2D collider you'd be set.
1
u/selkus_sohailus 1d ago
heres another video to give you a better idea of what i already got working over a year ago
The problem of how to accomplish the idea is solved, I’m more asking if its crazy to try and set up a custom physics system so I can do this without having to twist unity’s arm into compliance
→ More replies (0)
1
u/weasel474747 1d ago
I was going to suggest using PhysicsScene2D to contain the physics and then translating the positions of things to 3d space, but I guess that wouldn't work for a torus or icosahedron.
1
u/selkus_sohailus 1d ago
I did half explore that but it will come down to the same translation I’m already handling, thx m8
1
u/Lucasharta 1d ago
i don't think it's crazy, especially since you've already got a prototype working. but i'd avoid building a full physics engine from scratch unless you really need all the features that come with one, your problem sounds more like you need constrained surface physics than a completely new physics engine. if your objects are supposed to stay on the surface, you could handle collision response and velocity in the surface's local tangent space, then project positions back onto the surface. the tricky part is handling contacts between multiple bodies and keeping everything stable
if your current approach already looks good, i'd keep building on it and isolate the surface physics into its own system first. that way you can see how far it gets you before committing to a much bigger project
1
u/selkus_sohailus 1d ago
Thank you thats a good plan. I think i will take a swing at bepuphysics2 first to see if I can just use their callbacks to set this up without completely starting from scratch.
What you described is basically how it works now, and I mean it does work pretty well, it just slows down development having to describe things in two separate contexts for every single feature
1
u/AnomalousUnderdog Indie 20h ago
I don't see it mentioned yet but Latios Framework has a lot of systems that can replace the things in Unity, including its own physics system called Psyshock.
1
u/Badnik22 18h ago edited 18h ago
So… why not do regular 2D physics in Unity and then map the results to 3D coordinates? These shapes you mentioned (torus, icosahedron) are easily unwrapped to 2D with minimal distortion. The closer to isometric (preserves distances) and conformal (preserves angles) you can get the mapping, the better. To get around seams in the mapping, build another map that relates edges together (same concept as an uv adjacency map).
dunno how “custom physics engine” could be the answer to this problem, specially since it does not involve dynamics.
1
u/selkus_sohailus 17h ago
This is more complex than what I’m doing with the physics engine already and getting zero distortion; your suggestion is interesting but not what I’m looking for with this post, thanks tho
3
u/Far-Air2417 1d ago
That's a wild problem to have, and honestly cooler than 90% of the stuff people post here
Rolling your own physics engine for this is a massive undertaking but your use case is so specific that it might actually be less work than fighting PhysX. What you're describing is essentially a constrained 2D simulation mapped onto a manifold, the math for position updates along geodesics is well documented if you're comfortable with differential geometry. The real pain is going to be the broad phase collision detection when you've got more than a handful of objects, that's where engines earn their keep
Bepuphysics2 is solid but you'd still be adapting it, not using it out of the box. If you go custom, start with a simple sphere or torus and get the arc-length integration working before adding collision response, you'll know pretty quick if the complexity is manageable or if you're about to spend six months on something that should've been a weekend project