Writing a soft body cube in C/C++ using XPBD | Devlog Episode 1

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 มิ.ย. 2024
  • I wrote a simple soft body cube in C/C++ using the extended Position-Based Dynamics method for my first devlog episode.
    The mentioned papers (and a lot more)
    matthias-research.github.io/p...
    SDL Template
    github.com/blackedout01/sdl-g...
    0:00 Introduction
    0:41 PBD overview
    1:11 Game
    1:25 Tools
    2:25 SDL template
    4:23 Writing the scene (time lapse)
    8:55 Physics theory
    14:55 Physics results
    15:27 Writing the physics (time lapse)
    16:06 The end

ความคิดเห็น • 93

  • @vladk9152
    @vladk9152 3 วันที่ผ่านมา +1

    this is fantastic. I wanted to write my own game from scratch for a while but never knew where to start. Thank you

  • @SilverstringsMusings
    @SilverstringsMusings ปีที่แล้ว +18

    I enjoy your editing style and appreciate that you have decent audio balancing.

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +3

      I'm happy you like it. Would you believe me if I told you that I recorded this with a built-in microphone?

  • @oleksandr.kryvonos
    @oleksandr.kryvonos ปีที่แล้ว +3

    nice explanation! Thank you, I am looking forward to your next video.

  • @gameofpj3286
    @gameofpj3286 ปีที่แล้ว +1

    Really cool :D Always wanted to see some more implementations of this!

  • @arunu2002
    @arunu2002 ปีที่แล้ว +4

    Very high quality content. Thanks for sharing. Thanks

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +1

      Happy to hear that, thank you

  • @dameck9570
    @dameck9570 ปีที่แล้ว +1

    Really nice video and a great addition to Matthias Müllers Videos about this topic

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +1

      Thanks! Glad you think so

  • @karsten4849
    @karsten4849 11 หลายเดือนก่อน +4

    This is amazing! I love the editing and commentary style. This should have way more views.

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน +1

      Woah thanks! I am happy you think that

  • @gr4dience
    @gr4dience หลายเดือนก่อน

    Phenomenal video! The commentary, editing, and visuals are on point!

    • @blackedoutk
      @blackedoutk  หลายเดือนก่อน

      Great to hear thanks! Now I am curious though what you think of my newer episodes if you have watched any

  • @stefano9135
    @stefano9135 ปีที่แล้ว +6

    Oh, wow
    This is exactly what I needed, in the exact coding language I needed it.
    It just popped in my recommended, magic!
    Thank you so much for the video :)

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +2

      Haha cool! Thank you for your nice comment

  • @kae2018
    @kae2018 11 หลายเดือนก่อน

    Hey that maths explanation was actually quite nice and made it really easy to understand :) Good video!

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Thank you :)

  • @metalstarver642
    @metalstarver642 ปีที่แล้ว +5

    Great, exactly what I needed! One note: maybe don't include timelapse? It is useless because you can't really read any code that fast and it flickers too much. Better would be just to show important code fragments or paper fragments or something.

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +6

      I thought about that too, will keep that in mind for the next video. Thanks for your feedback

  • @awesomedreamerstudio
    @awesomedreamerstudio 3 วันที่ผ่านมา

    "masterpiece"

  • @Doppio_00
    @Doppio_00 11 หลายเดือนก่อน

    physics from scratch? wow amazing!! Irresistible Recommendations

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Haha great to hear

  • @soyouz3
    @soyouz3 3 หลายเดือนก่อน

    Hey! Great video really inspiring!
    I'm actually trying to develop my own jiggle solver inside SideFX Houdini as a challenge, helps a lot your explanation on the papers love it!

    • @blackedoutk
      @blackedoutk  3 หลายเดือนก่อน

      Thank you :) it's nice to hear that

  • @hayaop4942
    @hayaop4942 11 หลายเดือนก่อน

    This is really helpful! Thx!!!

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Happy to hear that

  • @deffuls
    @deffuls 6 หลายเดือนก่อน

    Very cool, continue !

    • @blackedoutk
      @blackedoutk  6 หลายเดือนก่อน +1

      Thank you

  • @nikevoflo1448
    @nikevoflo1448 ปีที่แล้ว

    Great video, thanks a lot! Grüzi :)

  • @__hannibaal__
    @__hannibaal__ 11 หลายเดือนก่อน +3

    You should know basic of tensor calculation, Tensor Calculus, Continuum Medium Mechanic, Analysis Finit deformation , elasticity-plasticity, C+++, Geometry Differential, …, SFML for me and GLSL , easy to make complex soft body (generalized boundary) . But starting is so hard. I worked on in 2001 with OpenGL at that time.

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน +1

      Yeah it‘s so much stuff. Luckily this is just for a game, so visual plausibility and computational cost have a higher priority than physical accuracy in terms of choosing the right simulation model. And so far the mass spring model seems to work

  • @olivierbossel
    @olivierbossel 11 หลายเดือนก่อน

    Very cool dude! 😊

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Thank you :))

  • @purpleAiPEy
    @purpleAiPEy ปีที่แล้ว

    thanks for the video

  • @VladimirDjokic
    @VladimirDjokic 11 หลายเดือนก่อน +1

    Cool :)

  • @yangchen9700
    @yangchen9700 8 หลายเดือนก่อน

    Great stuff. I am trying to do some physics as well. But lost quickly in those formulas

    • @blackedoutk
      @blackedoutk  8 หลายเดือนก่อน

      Thanks :) Yeah it‘s quite a bit of formulas

  • @garydeschaines
    @garydeschaines ปีที่แล้ว +1

    Physics and 3D rendering; "Welcome back my friends to the show that never ends...", ELP.

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว

      Haha, that seems to fit quite well

    • @garydeschaines
      @garydeschaines ปีที่แล้ว

      @@blackedoutk Started my journey over 40 years ago, and still trudging along. Good work, hope to see more. Cheers from GED.

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว

      @@garydeschaines Thank you! 40 years is impressive. It’s such an exciting field. Cheers

  • @SphealIcecream
    @SphealIcecream หลายเดือนก่อน

    Hi again! I'm trying to implement this from scratch just like you, but I am curious why you have a CosAngle member variable for SoftBodyEdge (15:34 on the right). Is it for future videos to add different types of constraints?
    Also... what is LambdaLength and LambdaAngle?

    • @blackedoutk
      @blackedoutk  หลายเดือนก่อน

      Hi, all of the angle variables were meant for future use (bending constraints). When I planned this I thought I could store the cosine of the angle instead of the angle itself to save some operations, but as it turns out this doesn't work. So this was a planning mistake which I corrected later.
      The lambda variables are the ones of their respective constraint, so LambdaLength for the distance constraint and LambdaAngle for the bending constraint (again the last one future use). The reason I defined these in the struct instead of in the simulation algorithm is because I like to minimize the amount of temporary memory allocations to improve efficiency (not having to alloc and free all the time) and to reduce the number of failure points while running the game.

  • @Hammish
    @Hammish 10 หลายเดือนก่อน

    I refuse to believe this guy only has 1k subs

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน

      I was actually quite happy when I saw 1k this morning haha. But even happier you think I should have more

  • @shengzhehou2209
    @shengzhehou2209 ปีที่แล้ว

    Very cool! 😃 And, will this project be open source?

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว

      Thank you 😄 Maybe at some point, I'm not sure yet

  • @u9vata
    @u9vata ปีที่แล้ว +1

    Is the physics-added code also available?
    I am also wondering how this method is related or not to that old C++ library no game used as far as I know except demoscene where cows were falling from above and there were spikes that either deform or even cut them! It was an awsome lib and I remember my friend at uni tried to replicate it in 2D with similar contraints + mass technique!!! I mean we knew the 3D one used that and it sounded really similar to thiis. That source code surely predates 2006 though with some years so someone else might have already found this out earlier....

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +2

      Lmao that sounds crazy. Do you have a link to a video or the library or something?
      I haven't open sourced the physics added code, no. Maybe I will once the game is finished, not sure. But if you are interested in the code, I would recommend the Ten Minute Physics channel, where Matthias Müller himself goes over small demos that he open sourced. Be prepared though, the code is in javascript

    • @u9vata
      @u9vata ปีที่แล้ว +1

      @@blackedoutk Found it! By that I mean I thought about it hard enough to remember its name... "Realmatter tech by Alec Rivers". There is video about that in youtube, but the stuff is earlier than the upload of that video to YT! But when my friend at university (the one I used to go to acm icpc competitions with and now he works at google actually so is and always was really smart guy) just by seeing the vids wrote it from scratch in 2D. You could break textured boxes into pieces with his demo that I have no idea if he still has. Basically he made the same dots + springs approach, just because we were mor about the "make it possible to destruct and deform it and stay it such" that is what was his focus too - just like Alec's code.
      There were some tech docs from this demo and how to do it randomly around the net at the time...
      Also look up "realmatter zombies" for an other YT vid of the same.

    • @blackedoutk
      @blackedoutk  ปีที่แล้ว +2

      @@u9vata Cool! But also pretty brutal. And the Thomas the Tank Engine one was funny lol. They even published a paper about their method "FastLSM: Fast Lattice Shape Matching for Robust Real-Time Deformation". I haven't read all of it but it seems they use the shape matching method described in the paper "Meshless Deformations Based on Shape Matching" from 2005 and improved its efficiency by reusing intermediate results.
      Contrary to the mass spring implementation I used, their shape matching method defines small regions of points that are then matched (accelerated towards) the same region in the original model (with adjusted rotation and translation). Figure 3 from the 2005 paper I mentioned depicts this pretty well. Because this matching is quite limited in terms of deformation capabilities, they use multiple overlapping regions of a grid generated from the original surface mesh.
      Definitely interesting. Glad you remembered it

  • @BrezhnevStan
    @BrezhnevStan 9 หลายเดือนก่อน

    Hey amazing video, it inspired me to try make something similar for a university project, but after looking at soft body examples in HTML/js from ten minute physics I'm lost on how to implement the constraints in C++/openGL. Could you please share some of the basic code for setting up the scene itself with the cube? It would be very helpful

    • @blackedoutk
      @blackedoutk  9 หลายเดือนก่อน

      Thank you, that's great to hear. I'm building a basic 3d renderer like in this video in my livestreams currently and the code is on my GitHub as well. You may want to look at that, the only problem is that it's not done yet 😅 But for the missing parts and constraints you can join my discord server, where we can discuss this. The link is in the comments of my second (currently latest) livestream recording

  • @darknetworld
    @darknetworld 11 หลายเดือนก่อน

    Learn a bit but great to see how it work. It a lot math XD. I wonder how fracture works later on destruction.

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Yeah, implementing physics is quite math heavy. But I think math can be fun too if someone explains it well and doesn‘t just write down a bunch of formulas.
      I would really like to implement fracture eventually but currently I don‘t know how it‘s done. Have to do some more reading

  • @Z_Z.t
    @Z_Z.t 10 หลายเดือนก่อน

    there is a channel "Ten minute physics", the man is physics researcher from Nvidia does tutorials and deep explanations of computer physics. He is also the man behind behind paper with deformation simulations in realtime, and that paper also got implemented in PhysX and UE 5.1

    • @Z_Z.t
      @Z_Z.t 10 หลายเดือนก่อน

      But its still great to see you implementing physics and facing practical problems with practical solutions, because he was more into theory behind implementations

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน

      Yeah, he does some pretty awesome things. I even used some of the techniques he presents in his tutorials for my game. Glad you like my videos as well

  • @user-np1sh5pz7j
    @user-np1sh5pz7j 10 หลายเดือนก่อน

    Hi! Great video : ) What version of MacOS are you using / are you on an M series Mac? I've had issues with the opengl drivers in the past where I can't use any textures because of a driver related shader compilation error for sampler2Ds. I was on an M1 mac air using opengl 4.1. Thanks!
    Have you seen this shader compilation error:
    UNSUPPORTED (log once): POSSIBLE ISSUE: unit 0 GLD_TEXTURE_INDEX_2D is unloadable and bound to sampler type (Float) - using zero texture because texture unloadable

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน

      Hi, thanks :) I haven't seen this error yet, my machine is an M2 MacBook Air currently on 13.5. How are you binding your textures? It seems to be an issue with that.

    • @benceweisz1063
      @benceweisz1063 10 หลายเดือนก่อน

      @@blackedoutk Hi, its me again. So the funny thing is that it must have been either 1) Fixed in a recent MacOS update or 2) It was a problem with using GLFW as a windowing library. In any case, I've implemented it as a fork to your cg-papers repo and it works : ) Thanks!

    • @blackedoutk
      @blackedoutk  9 หลายเดือนก่อน

      @@benceweisz1063 Oh, that's strange. But great that it works now

  • @masonhawver3577
    @masonhawver3577 10 หลายเดือนก่อน

    why not use glm for the matrix math?

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน

      It would definitely be less error prone, but sometimes I like implementing things myself to also get a better understanding of the math.
      The only downsides of using glm I can think of right now are that huge template libaries are often hard to read and add a significant amount of compile time. Also, I would have to use their code style preferences, like camel case.

  • @SphealIcecream
    @SphealIcecream 2 หลายเดือนก่อน

    Hi! Can I ask you why the equation at 14:04 looks different from the equation on the Ten Miute Physics video about XPBD? th-cam.com/video/jrociOAYqxA/w-d-xo.html

    • @blackedoutk
      @blackedoutk  2 หลายเดือนก่อน +1

      Hi, there are a few reasons why they look different. First, the algorithms in the Ten Minute Physics video only does a single solver iteration. This means that any λ in the Δλj equation here can be ignored, since before the solver runs, each λ is initialized to zero. As to why, this is explained in section "Iterations vs. Sub-steps" and specifically at 7:05 he mentions that λ is not needed anymore. The consequence is that the part "- alpha j tilde λij" of equation 18 can be ignored.
      Secondly, because we are only dealing with point masses, the inverse mass matrix is still a (block) diagonal matrix. So the term "∇Cj M^-1 ∇Cj^T" is really just the dot product between ∇Cj with itself, where each summand is weighted by the corresponding mass. The absolute value lines | | are used because each of his ∇Ci are still vectors.
      Also, note that the index j in my video does not correspond to the indices of C in the denominator of his video, because he lists these equations for a single constraint, whereas here the equations take the whole system of constaints into account for which ∇C is actually a matrix (the jacobian matrix). So the λ in his video equals the Δλj here and ∇C1 and ∇C2 in his video are entries in the vector ∇Cj, meaning ∇Cj1 and ∇Cj2.
      Next, the alpha tilde j is not present in his denominator because he set it to zero, meaning the springs are infinitely stiff.
      Equation 18 is a really general way to describe the algorithm. You wouldn't implement it this way if your constraint was really simple, like a linear spring. So if you simplify equation 18 with all the knowledge of what you have at about linear spring constaints, set alpha tilde to zero and ignore λ (not Δλj), you end up with the definition of λ in the Ten Minute Physics video. For linear springs the gradient vectors of the constraints of the relevant point masses are of length one, so their dot product or squared length is 1.
      Lastly for Δx it's very much the same reasons. In constrast to Δλj however, Δx is a vector that contains the delta positions of all point masses. So even more general that the Δλj equation. If you were to multiply everything out and only look at a single constraint Cj and a single point mass x1, you would end up with the equations in his video.
      I hope this helps, it's a bit difficult conveying this through text. If you didn't understand some part of it don't hesitate to ask

    • @SphealIcecream
      @SphealIcecream 2 หลายเดือนก่อน

      @@blackedoutk Was not expecting this detailed explanation! Thank you so much :)

  • @edwardtran137
    @edwardtran137 11 หลายเดือนก่อน

    I want to do this myself too. Can you drop a full tutorial without time lapse?

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      There are some tutorials on the Ten Minute Physics TH-cam channel that I would recommend. For now I want to focus on my devlog series. But if you have specific questions you are welcome to ask me.

  • @Bloodlinedev
    @Bloodlinedev 11 หลายเดือนก่อน

    Ah, C++ build system... I can recommend premake5

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Don't scare me 😭

  • @JerroudiYassine
    @JerroudiYassine ปีที่แล้ว

    OMG a 1 Giga Bytes hello triangle file hahahaha

  • @nangld
    @nangld 11 หลายเดือนก่อน

    Now do that for c64

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      😵‍💫

  • @bartoszstyperek6306
    @bartoszstyperek6306 3 หลายเดือนก่อน

    This video is great. Any chances for video explaining this part - th-cam.com/video/MgmXJnR62uA/w-d-xo.html . Looks like firs equation is: ma = grad( Potential Energy) ? Then discreet version, then something about this potential energy 'wrapped' in constraint C matrix (equation?). Not sure what is - alpha. Will try what chat gpt will say... but still, video woud be great.

    • @blackedoutk
      @blackedoutk  2 หลายเดือนก่อน +1

      Thanks. I wanted to make a video where I explain the derivation of these equations, but that would still be far in the future, I've been quite busy lately unfortunately. You're right, the first two equations are mass times acceleration being equal to the gradient of the potential energy and then discretized.
      For the definition of the potential energy I would encourage you to write down the potential energy of a Hookean spring and then for multiple ones. The potential energy of multiple springs will just be the sum of the potential energies of the individual potential energies, but if you choose fitting vectors and matrices, you can rewrite the sum as matrix multiplications. Which is what equation 5 is. The inverse of alpha for example contains the spring constants of all the springs as a diagonal matrix.
      Again, writing this down for small examples and seeing what is what really helps here, I am sure you can figure this out. You might also wanna look at the paper where I got this from "XPBD: Position-Based Simulation of Compliant Constrained Dynamics", which contains some further although admittedly very brief explanations. The only thing I am still not exactly certain about is why they introduce the lambda formulation. I looked at the referenced paper by Servin et al but it's still not really clear to me.
      Also because you mentioned chat gpt, I would be careful with it for maths, sometimes it creates resonably looking proofs for example but upon further inspection there are small errors which make the proof invalid. At least that's my experience, so I would be careful with that thing

  • @homeopathicfossil-fuels4789
    @homeopathicfossil-fuels4789 11 หลายเดือนก่อน

    First triangle in Vulkan... 1.3 GIGABYTES? I wrote my first software renderer in less than 1k lines of C a few years ago, what even happened?

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน +1

      That's kind of what it felt like for me following a tutorial a few years ago. In reality it was much less of course but still 1k lines or something. Enormous amounts of setup. 1k lines for a software renderer is more understandable I would say

    • @homeopathicfossil-fuels4789
      @homeopathicfossil-fuels4789 11 หลายเดือนก่อน

      @@blackedoutk Damn, and I thought OpenGL was tough. Something something vulkan has lower level access, right? That would explain the experience :p

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      @@homeopathicfossil-fuels4789 Yeah, Vulkan and other apis offer much more control over the gpu and its interaction with the cpu. Synchronization for example is one thing that scared me away. I would say they are even tougher than OpenGL. But part of why the first triangle in Vulkan is so long is also because you have to fill in setup structs like do you want to draw filled or wireframe and so on, whereas in OpenGL you just use the preset values.

    • @homeopathicfossil-fuels4789
      @homeopathicfossil-fuels4789 11 หลายเดือนก่อน

      @@blackedoutk That makes my mouth water! Been looking for a new low level mindscrew to fiddle with for a while. I normally work in pure C (on PC) or assembly (on experimental homebrew systems or their emulations) and sometimes when I am feeling "wasteful" or "lazy" I take it up a notch of abstraction to C++. Sweet! Thanks for your information!

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      @@homeopathicfossil-fuels4789 Haha cool, yeah that might suit your needs

  • @desine_
    @desine_ 10 หลายเดือนก่อน

    what background knowledge do you have?
    it looks very not understandable from my perspective :/

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน +2

      Oh :/ I study computer science, so I attended a few math and simulation lectures. What about you?
      I try to explain these concept using mostly high school knowledge, but maybe I don't always succeed in that. Is there something specific I could explain further or link resources to that might help you?

    • @desine_
      @desine_ 10 หลายเดือนก่อน

      @@blackedoutk Well, every school is different and I'm from russia actually, I suppose the teaching programs are very much different.
      I am currently in college, I love programming and staff. aaand I'm bad at physics.
      It would help to know more about physics and how it's done via code, do you have any cool videos about that?

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน

      @@desine_ Yeah, probably. Is college like university in some sense?
      For physics and code I would recommend the TenMinutePhysics TH-cam channel. He creates tutorials and also uploads the code to his GitHub, though it might be a bit technical.
      For physics in general there are probably a lot of resources but I remember watching some Khan Academy videos and recently I watched the friction video by Professor Dave Explains. He has a playlist about physics as well:
      th-cam.com/play/PLybg94GvOJ9HjfcQeJcNzLUFxa4m3i7FW.html
      I haven't watched all of it but they look like good introductory videos. If you watch them, let me know if they were helpful.

    • @desine_
      @desine_ 10 หลายเดือนก่อน

      ​@@blackedoutk Thank you, I will start with the playlist.
      "Is college like university in some sense?" - College prepares you to a job, at least it should, but does not go deep into math or physics. I think university is more focused on "how and why", college on the other hand only introduces you to some topics.

    • @blackedoutk
      @blackedoutk  10 หลายเดือนก่อน +1

      @@desine_ Ah, okay. University is a lot of theory indeed

  • @atomictraveller
    @atomictraveller 11 หลายเดือนก่อน

    hi, i'm going to ask you once. when you do something, will you please do it, and not do it from scratch. scratch ain't doing you no favors holmes.

    • @blackedoutk
      @blackedoutk  11 หลายเดือนก่อน

      Is that a quote or something?