5 ms·
I have huge admiration for anyone who manages to implement a physics engine. I've tried to do this twice now (to integrate with my 2D canvas library) and both t
by rikroots 6y ago
I have huge admiration for anyone who manages to implement a physics engine. I've tried to do this twice now (to integrate with my 2D canvas library) and both times I've not been able to solve the collision response part of the system.
Given that I failed my 'A' level maths in spectacular fashion (I honestly don't get integration - it just doesn't compute for me) I'm obviously attempting the impossible here. But I live in hope that one day I will somehow stumble on a solution that looks like its working on screen ... which will be enough for me.
I've tried combining my library with third party physics engines - but that, too, is hard work. The best I've ever managed is with the Matter.js[1] engine but the amount, and complexity, of code needed to create a scene is too much for my tastes[2].
Maybe I'll go back for a third attempt if/when the second Covid wave hits.
[1] Matter.js home page - https://brm.io/matter-js/ https://brm.io/matter-js/
[2] Codepen demo - https://codepen.io/kaliedarik/pen/zYvbwBy https://codepen.io/kaliedarik/pen/zYvbwBy
- sillysaurusx 6y agoThe solution is to borrow ideas from those who have done it before. One idea comes from quake: for collision response, call a function recursively until there's no more collision. Basically, it pushes you away from a given plane. So the function looks like pushAway(plane, you). If that pushes you into something else, you get pushed away again, recursively. If you imagine a V shape, the result is that you end up "standing" on the V, because you're getting pushed away from both sides of the V equally. I'm not sure my explanation is very good, so to simplify: just add recursion, lol. (You'll need to limit the max recursion depth, or else you'll get a frozen game.) I used to try to solve things from first principles – and it's more fun to do that – but there's no shame in copying the answer. Carmack's solutions tended to be copied from the medical rendering field anyway. :)
- rikroots 6y ago> The solution is to borrow ideas from those who have done it before. I've tried that. Open various libraries, go through the code, work out how/where they perform the collision response calculations, trace back the vars passed into those calculations to see where their values get calculated, etc, etc ... and clearly I keep missing some key step or other along the way. I'm either misunderstanding the collision normal, or misinterpreting the collision depth, or my understanding of torque is deeply flawed (very possible, that one) ... frustration builds. Then I blame the engine coders for being too clever with their code and try a different library. I'll get there one day. Failure is not a disaster!
- pasabagi 6y agoAs somebody who also struggles with math, I feel that the problem is it's extremely unforgiving. For instance, when I was trying to write some simple 3d rotation matrices in lua, I kept on getting crazy results just because I'd invariably mix up one cosine with a sine, or a plus with a minus. I don't really have any thoughts about how to get around this though.
- bigdict 6y agoFailure is wonderful. Keep going.