Programming Balls #1 Circle Vs Circle Collisions C++

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024

Комментарии • 306

  • @jewe37
    @jewe37 6 лет назад +153

    10:40: ehhh, that fabs is useless, the sum of two squares is always gonna be positive anyways in the reals.

    • @javidx9
      @javidx9  6 лет назад +87

      lol, oh yeah! Hey I'm just back off my break, give me a week to get back up to speed! :D

    • @antonmorozov3008
      @antonmorozov3008 4 года назад +18

      @@javidx9 I've wasted about an hour trying to find some "secret knowledge" that you've put into that fabs!
      I read about x87 instructions and other horrific low-level stuff, tried to perform fabs on assembler level using __asm__ statement.
      And you were just back off your break... XD

    • @peterneiss7284
      @peterneiss7284 3 года назад +9

      @@antonmorozov3008 set b=p, then fabs(balls), => giggle

    • @neillunavat
      @neillunavat 3 года назад +5

      ​@@javidx9 hey, im havin a glitch where sometimes the... balls get stuck together... any idea on how to resolve that?
      Also, when the balls are stuck together, if I add velocity to 1 ball, the other one also moves with it maintaining that position relative to the ball... its a really annoying bug :(
      Nevermind, I solved it by moving the dynamic collision code just after the static collision thingy... wow.... I wonder why now.
      (btw, you are bob ross of programming... seriously)

    • @loStraniero17
      @loStraniero17 2 года назад

      Eja Eja vai

  • @oblivionronin
    @oblivionronin 4 года назад +43

    7:36, nope biggers balls, always funny even at 30 years of age. ill giggle on friend. Nice Work (y)

  • @jonnylaw4569
    @jonnylaw4569 5 лет назад +53

    7:35 i wasn't until you said to stop lol

  • @zadockmaloba5938
    @zadockmaloba5938 4 года назад +35

    I'm still wondering how the whole thing runs on only 2mb of RAM

    • @Name_cannot_be_blank
      @Name_cannot_be_blank 2 года назад +15

      Have you ever play nintendo ds game? All in 4mb ram
      I wonder how this take up 2mb, actually pretty big for what it is

  • @edwinmartens7459
    @edwinmartens7459 5 лет назад +6

    Great explanation !
    However you are doing a lot of colisions double,
    Since collision (b1, b2) is the same as collision (b2,b1)
    So the loop should be somerhing like
    for (int i=0; i

  • @andrew.r.lukasik
    @andrew.r.lukasik 6 лет назад +41

    I find this to be an awesome resource. You made it into simple, approachable subject. Thanks!

    • @javidx9
      @javidx9  6 лет назад +5

      Hey thanks Andrew, I'm pleased you found it useful!

  • @MrNickolay1986
    @MrNickolay1986 3 года назад +1

    25:12 I don't get it, shouldn't the angle of incidence be equal to the angle of reflection in dynamic resolution? How is it that ball should move along tangental line?

  • @autofool2981
    @autofool2981 3 года назад +8

    Great stuff! I'm new to C and following your tuts in C#. You talked about optimizing code. I wonder if you could make dedicated playlist for explaining how, why and when optimizations are done using these codes. As I understand collision detection is costly I'd like your simple wording and explanation any day. Please consider it. Thank you for sharing your amazing knowledge!

  • @annonymoritz
    @annonymoritz 2 года назад +4

    Hi, first of all nice video^^
    I would still like to ask something.
    At 26:34, is it not enough to take radius1 + radius2 as distance?
    It happens immediately after the static resolution, so the balls should be touching each other.

  • @ndemonicangel
    @ndemonicangel 4 года назад +5

    "Programming Balls" is really funny title! Expect to get a lot of fun from this video!

  • @thefoolishgmodcube2644
    @thefoolishgmodcube2644 6 лет назад +31

    Programming balls? Circle vs circle? Collisions? I see what you did here.

  • @mIsPtr
    @mIsPtr 3 года назад +3

    hi, i want to convert this to olcPGE but there's no DrawWireFrameModel function in olcPGE
    edit*
    nvm, i copied the function from CGE to PGE

  • @mrcheese7393
    @mrcheese7393 2 года назад +1

    girls: why do boys always laugh when someone says 'balls'. they're so immature
    boys: IT'S THE LAW

  • @aryesegal1988
    @aryesegal1988 6 лет назад +6

    Javid's back! :D
    Seriously, I was about to PM you, asking if everything's ok. Did not see you active in a month or so.
    Great video and topic in general. Looking forward for more! :)
    Thanks for taking the time to share with us :)

    • @javidx9
      @javidx9  6 лет назад +4

      Hi Ayre, Thanks!, I think it will be a useful topic to expand on. I took a little break over the holiday season. Finally got to do some non-video code!

  • @Kentauros1991
    @Kentauros1991 5 лет назад +1

    Ehh, the "tangential response" explanation is... not exactly wrong, but misleading? It's not "response", it's just that during the elastic collision of two smooth balls their tangential velocities (what you called "tangential response, simply described as the dot product of ball's velocity vector and the tangential vector" - it's tangential speed) do not change - otherwise, the kinetic energy won't be conserved. Only the normal velocities change (according to the formula you've coded - which is the case of central collision), and the resulting velocity after the collision is sum of the new normal velocity and the unchanged tangential velocity (in other words, old_tangential_speed * tangent_vector + new_normal_speed * normal_vector, just as coded).

    • @jaroslawmichalski9021
      @jaroslawmichalski9021 4 года назад

      Thank you @Kentauros1991. Your comment really helped me to progress with this stuff. The video was a bit confusing (at least for me), but once I realised what's wrong then I could successfully (and quickly) finish my ball collision simulation. Thanks to both of you @Kentauros1991 for your explanation in your comment and @javidx9 for another great video!

  • @rugioe8434
    @rugioe8434 4 года назад +4

    I was searching so long for a video that explains that properly! Thanks! :)

  • @johnjackson9767
    @johnjackson9767 6 лет назад +18

    Hehe...collisions.
    Great video like usual!

    • @javidx9
      @javidx9  6 лет назад +1

      Cheers John!

  • @akarshrastogi3682
    @akarshrastogi3682 6 лет назад +6

    Hi, I came here for finding more optimized ways for detecting collision between multiple particles on the screen (QuadTree implementations or Hashing as in Box2D etc).
    Any recommended video for the same?

    • @artemisDev
      @artemisDev 3 года назад

      well i'm 2 years late but for people of future ruclips.net/video/OJxEcs0w_kE/видео.html series is pretty good

    • @TWFDeadzoneII
      @TWFDeadzoneII 2 года назад

      @@artemisDev Hello, I am a person from the future. Thanks for the link.

  • @AdvancedSoul
    @AdvancedSoul 6 лет назад +6

    This video inspired me to do a series of small programs in go, using the wireframe logic. Thanks!

  • @47Mortuus
    @47Mortuus 3 года назад +2

    Square roots are not costly to compute. It's

  • @creaperzombie2579
    @creaperzombie2579 5 лет назад +6

    im so glad i found this channel, humor and learning :) keep up the good work!

    • @javidx9
      @javidx9  5 лет назад +1

      Thanks Creaper!

    • @johnnyserup5500
      @johnnyserup5500 5 лет назад +1

      CreaperZombie25 yes humor is good, notice the way he can easily just increase his balls using a simple float - hehe.

  • @NeZversSounds
    @NeZversSounds 6 лет назад +19

    HA you can't say R2 without D2 :D

  • @neillunavat
    @neillunavat 3 года назад +2

    hey, im havin a glitch where sometimes the... balls get stuck together... any idea on how to resolve that?
    Also, when the balls are stuck together, if I add velocity to 1 ball, the other one also moves with it maintaining that position relative to the ball... its a really annoying bug :(
    Nevermind, I solved it by moving the dynamic collision code just after the static collision thingy... wow.... I wonder why now.
    (btw, you are bob ross of programming... seriously)

    • @billowen3285
      @billowen3285 3 года назад

      What do you mean just after? I've got the same problem lol

    • @neillunavat
      @neillunavat 3 года назад

      @@billowen3285 lol. What are u using to do that?

    • @billowen3285
      @billowen3285 3 года назад

      @@neillunavat the PixelGameEngine C version if that makes a difference

  • @syllogism2034
    @syllogism2034 6 лет назад +5

    I just wanted to say thank you so much for making the videos that you do. You have given me motivation to keep practicing. You're an inspiration.

    • @javidx9
      @javidx9  6 лет назад +1

      Hey thanks Lloyd! I really appreciate that, comments like that make it all worth while. Cheers!

  • @r1pfake521
    @r1pfake521 5 лет назад +2

    Awesome video. I have a question about the static push back. What happens if many balls are very close and you do the push back math and then one of the ball ends up inside an other ball after it's pushed back, so it would be possible that two balls still overlapp at the end of the loop if they are not checked again (for example if the two balls are at the start of the vector) or am I missing something?

    • @javidx9
      @javidx9  5 лет назад +1

      These are good observations, and i attempt to address them in part #2.

  • @haywardhaunter2620
    @haywardhaunter2620 2 года назад +1

    It seems the static resolution of an overlap could result in a new overlap. For example, in resolving overlap between balls A and B, B might be moved so that it overlaps C. If B-C has already been tested, then this new overlap will remain until the next frame. Since your O(n^2) loop essentially gives you a second chance, this probably won't happen for a secondary overlap. But if you had a row of balls close together (as in a Newton's cradle), tertiary overlaps won't get static resolution until the next frame. Right? I guess that's not a big problem, but I'm wondering if I'm missing something and alleviates this concern.

  • @jcstatt8698
    @jcstatt8698 4 года назад +3

    Thank you so much for this video. It's helped me tackle the hardest part of my project so far

  • @zohichnazirro8640
    @zohichnazirro8640 4 года назад +2

    28:35 wow, thanks for challenge!
    If you didt say that, i would just copy the formula. But now im happy i did it too

  • @Cheems_on_Toast
    @Cheems_on_Toast 4 года назад +3

    You sir are a lifesaver. Brilliant video, reduced the daunting equations down to easy code. Thank you very much

    • @javidx9
      @javidx9  4 года назад +2

      No problem James!

  • @NeilRoy
    @NeilRoy 6 лет назад +3

    Laughed out loud right off the bat. XD Seriously though, circular collision is my favorite to use as it is so simple to implement. I use it in my Deluxe Pacman 2 game. I'm currently working on some 3D, and this sort of thing, only with three dimensions, could make for some nice asteroid collisions (3D space game).

    • @javidx9
      @javidx9  6 лет назад +2

      Hi Neil, extending this approach to 3D should be trivial. However, this approach does have some draw backs which will be demonstrated in a later video. 3D asteroids sounds fun, but I imagine a very difficult game to play.

  • @marcoottaviano527
    @marcoottaviano527 4 года назад +1

    Hi there, first of all great videos! I have a question:
    Suppose you have 3 balls lined up: ball 3 striking and colliding with ball 2, and ball 2 almost touching ball 1.
    In your algorithm, ball 2 will be displaced backward and penetrate ball 1. But this collision is not detected nor resolved, since the pairs 1-2 and 2-1 have already been analysed in the "for" loop, before ball 2 had been displaced.
    So there's a momentary penetration between ball 1 and ball 2, and it's only resolved in the next update cycle.
    Is it correct?

  • @marvinCEF
    @marvinCEF 6 лет назад +7

    Amazing work! I followed your tutorial but instead of C++, I used Python3 and Pygame:
    github.com/marvin939/pygame-circle-collision
    As always, your tutorials are quick to the point and easy to understand. Thank you!

    • @javidx9
      @javidx9  6 лет назад +5

      Hi Marvin, Cheers! That's great! Could I show it in a future community showcase video?

  • @NightpireVideos
    @NightpireVideos 6 лет назад +6

    Great, I'm gonna try this

  • @Duxuuuu
    @Duxuuuu 2 дня назад

    Best programming channel EVER dude

  • @woodfragment1384
    @woodfragment1384 3 года назад +1

    hi I need to know if the "olcPixelGameEngine.h" is the same as the "olcConsoleGameEngine.h", as I can't find "olcConsoleGameEngine.h" on your github. please help me...
    and also, how do I execute/compile your .cpp-Files such as the "olcExampleProgram.cpp"? when I open it in Visual Studio, i can't click anything like "debug", because this button now says something like "append" / "attach" / "enclose" in my language ("Anfügen" in German, I don't know how to change languages there)

    • @javidx9
      @javidx9  3 года назад

      They are very very similar ruclips.net/video/eTGSTTxR-Ss/видео.html

    • @woodfragment1384
      @woodfragment1384 3 года назад

      @@javidx9 thank you!

  • @JaySiggz
    @JaySiggz 5 лет назад +1

    Please please please please please please show us how to implement rotation aswell. I don't know why I have such hard time doing it but yeah... Please.

  • @saishykitty
    @saishykitty 4 месяца назад

    One thing I didn't understand is how are u preventing every collision from affecting each ball twice?
    On your overlap detection you iterate the balls for each ball, so BallA will eventually check against BallC, but when it reaches BallC, it will also check against BallA, and you are applying the displacement against both balls on each check, so wouldn't they move twice?
    When I tried writing in another language they are being displaced twice.

  • @maximumiq158
    @maximumiq158 4 года назад +1

    How many times did you say ball in this video. Instead of target ball, you should use left and right ball 😅

  • @andrewscott5960
    @andrewscott5960 4 года назад +1

    I'm following along with you trying to figure out each section before watching you and most of the time I can't, but at 29:11 I was like "oh i know how to calculate relative mass; it's just radius^2 for discs and radius^3 for spheres" and then you made it linear but at least you admitted it was rubbish :) . All of your videos are awesome by the way.

  • @zxuiji
    @zxuiji 5 месяцев назад

    Knowing what I do now I would not have started with a ball object, instead I would've started with an edge object and given it a start, middle and end point along with and how sharp the curve is (0 being flat). Then I would give the ball object 2 of these objects, a triangle 3, a square 4, etc.
    If the xy distances added together are less than either objects inner radius then a collision is definite, if it less than the outer radius then loop through 1 objects edge objects looking for the closest middle point using the same distance adding noting down which is closer as you go through them.
    Then you likewise do for the other object, checking which is closer to the edge middle point you just noted. Then do √0.5 * (xdist + ydist). If the result is behind either middle point then a collision has occured. Throughout this not a single division is done besides the initial storage of √0.5 making it the fastest form of pin point collision.

  • @FrostGamingHype
    @FrostGamingHype 3 года назад

    Gamers:Circle Fights Circle Collision
    Me:Circle Vs Circle Is An Example Of The Collision
    Gamers:(⓿_⓿)🤨
    Me:? You Are A PG Right
    Gamers:Now Whats PG
    Me:Programmer
    Gamers:Pro Gamramer So You Are A Pro At Grammer
    Me:Forget It

  • @jackpret4547
    @jackpret4547 6 лет назад +3

    Aaargh! Great!
    How about balls that interact with lines like the edges of a pinball game or a pool table!!

    • @javidx9
      @javidx9  6 лет назад +1

      Thanks Jack, See Part 2 :D

    • @jackpret4547
      @jackpret4547 6 лет назад +2

      Aaah! !! Great Tutorials! Thanks!
      Will check them all out!!

  • @lexmarch6309
    @lexmarch6309 6 лет назад +2

    Looks really good. Great idea, as usual. Hello from Ukraine :)

    • @javidx9
      @javidx9  6 лет назад +1

      Hi Lex! Thank you! Hello from United Kingdom!

  • @UFO26
    @UFO26 3 года назад

    Hi, javidx9. I copied the contents of your collision handling function. I do not observe the law of conservation of momentum. In the Wikipedia version, too, everything is illegal. Have you checked the sum of the pulses before and after the impact? Do you comply with this law?
    p,s, translated and voiced by Google Translate Studio )))

  • @riibbert
    @riibbert 6 лет назад +3

    This is exactly what I needed for my Monogame Billiards game! Cool tutorial, I love how you go straight to the point while keeping it all explained and clean.

    • @javidx9
      @javidx9  6 лет назад +2

      Hey thanks riibert, if you make something cool, I'd love to feature it in a community showcase!

  • @burntt999
    @burntt999 4 месяца назад

    12:54 its funny how many times ive had to hit the left arrow to back up 10 secs because as soon as you get the the end of the line you fly back to the front of the code...
    and only NOW is when i remember i can slow down the video :)

  • @javierdieguez1801
    @javierdieguez1801 6 лет назад +6

    Hey man , thanks! I've been struggling with my 2d physics engine for days (I was trying to apply snell's refraction law to dynamic spheres making some modifications and it just wasn't right), but your formulae and the clarity of the video really help out. Once this is handed in ill get on to space subdivision schemes and hopefully try to understand the Kinetic Energy implementation :P.

  • @abuk95
    @abuk95 3 года назад

    Isn't fOverlap always 0 when you basically subtract radius of both balls from distance between them?

  • @nickscurvy8635
    @nickscurvy8635 2 года назад

    It's not considered unfashionable to use pointers. It's considered unfashionable to use raw pointers to dynamically allocated memory. You are urged to use smart pointers which automate the acquisition and release of the resource.
    Imagine how absurd it would be to be coding in c++ without pointers lmao. If you don't like pointers use Java.

  • @esnapeye7098
    @esnapeye7098 3 года назад

    You computed tangential and normal vectors without calculating/using angles they make with the x-axis 😱 and here I am calculating the angles and resolving everything to both co ordinate axes then finding the final magnitude and direction of balls after collision and making the code slower, gosh I still don't understand the tricks you did with tangential and normal vectors :(

  • @cursedfox4942
    @cursedfox4942 11 месяцев назад

    interesting I wonder if you were using vertices if a for through multiple shapes like squares would be ok for multiple collisions here you slide resolve I guess a for to go through shapes is fine then

  • @あれくす
    @あれくす Год назад

    Do you have a video where you start a project from scratch without the dependance on the one lone coder engine? If not, do you have a series that walks us through building it? Im trying to learn more advanced concepts but every vide you have is dependent upon the engine or this engine that you made some time ago and none of that helps me because im totally lost unless i can see how it all comes together and not to mention its better learning that way for me. Thanks for any reply, i think you do great stuff, but for a beginner i'd love to start AT the beginning with you.

  • @aabs7978
    @aabs7978 6 лет назад +1

    Thank you very much for translating the Wikipedia to C++, that is something i can not do myself.
    btw : i noticed this algorithm adds velocity if you dont use the acceleration integrator.
    Now i have a nice asteroid field, thanks again!

    • @aabs7978
      @aabs7978 6 лет назад +1

      Oh it adds velocity becus the big ball is hitting the smaller ones, how smart of me.

  • @iqbalmaulana4927
    @iqbalmaulana4927 4 года назад

    Excusme sir.how to boost logic in brain for make game alghoritmh for beginners
    #please answer
    #thanks

  • @renevank
    @renevank 6 лет назад +2

    i have been working for 4 hours trying to make a similar program in java in processing, only to find out its wayyy harder than I thought... I GIVE UP

    • @javidx9
      @javidx9  6 лет назад

      Dont give up renevank! conversion to java should be almost 1:1!

    • @renevank
      @renevank 6 лет назад

      @@javidx9 I am going to give it another shot, I was frustrated in the heat of the moment haha 😅 thanks for making such an informative video :)

    • @philippg6023
      @philippg6023 5 лет назад

      @@renevank good luck... processing is awesome

  • @77Zamien
    @77Zamien 6 лет назад +2

    I really liked this video and have implemented it into a MonoGame application! One thing I would really like would be if you could discuss the "Bullet through paper" problem when velocities gets high? Typically "thin" contacts are easily totally missed or the static collission is resolved in the incorrect direction.

    • @javidx9
      @javidx9  6 лет назад

      Thanks Samuel! I will be addressing some of the pitfalls of this method in the next video. You are right thin, objects can get missed out and cause problems. It would be great to see your implementation. Drop a link?

    • @77Zamien
      @77Zamien 6 лет назад

      Great news! Looking forward to that video! Here is my implementation: github.com/ZamTheman/PlayingWithPhysics

  • @jean-naymar602
    @jean-naymar602 6 лет назад +2

    I think I'm definitely in love with you.
    EVERYTIME I want to learn how to do something, you post a video on that topic a few days later.
    It's really fascinating (but also quiet frustrating because I loose the option to tackle the problem on my own, but that is my fault because I can't help watching)

    • @javidx9
      @javidx9  6 лет назад +1

      Hi Jean-Nay, Sorry! lol, as one of my earliest supporters, you have a special place in my heart too! Perhaps I should put warnings on the videos "CAUTION! May Ruin Intellectual Exercises"

    • @jean-naymar602
      @jean-naymar602 6 лет назад +1

      lol, it would be a significant advancement

  • @linus7529
    @linus7529 6 лет назад +2

    How do you like your vertical mouse? I've been thinking of picking one up as well.

    • @javidx9
      @javidx9  6 лет назад +2

      Hi Tiddly, I really like it. I have one at home and at work, and within 24 hours all my wrist pain has gone and not returned. The only slight complaint I have is precision clicking is frustrating at first, because when you press the button, you are no longer pressing against a solid surface, so the mouse moves ever so slightly, but I got used to it

    • @dagonmeister
      @dagonmeister 3 года назад

      What’s the model? I use a vertical mouse too, want to try a different model, mine is not good enough

  • @jamesw242
    @jamesw242 4 года назад +1

    I have a bit of an internal conflict, javid. Because i spend so much time online and mix with a lot of yanks i'm not sure to use the american or british spelling of words. Sometimes i use both and confuse myself with colour/color. defense / defence etc

    • @javidx9
      @javidx9  4 года назад +1

      lol James, well I use Colour and I'm sticking by it XD

  • @jephthahjoseph8539
    @jephthahjoseph8539 Год назад

    i dont understand the formula behind displacing the current ball

  • @harryfox4389
    @harryfox4389 5 лет назад +2

    Congrats on 20k. Also this video is exactly what I need thanks

    • @javidx9
      @javidx9  5 лет назад

      Hey thanks Prof Fox!

  • @xuhan1981v
    @xuhan1981v 6 лет назад +6

    I suggest you to use vector class instead of .x, .y. I mean, if you use a vector class and express all the position, velocity, force in vectors, then it will make your coding much easier. And also, instead of using collision formula which is derived from momentum and energy conservation, you may also directly use impulse-momentum law (F=dp/dt, F ~ k*dl, dl = r1+r2-distance between two balls, p, dl, F are all vectors), it will again make the code much simpler and easier to understand. Moreover, it is quite easy to introduce a non zero damping constant C (F= k*dl - C*momentum) to mimic a non-elastic collision .

    • @javidx9
      @javidx9  6 лет назад +5

      Thanks Han, I'm quite aware that a vector class would make the code clearer for people who are already familiar with coding, but then I would need to explain operator overloading which would mean a separate video.

  • @jackmead7292
    @jackmead7292 5 лет назад +2

    I've been looking for a while for an intuitive algorithm for elastic collisions. I'll make making sure to bookmark this video... well done. Thank you very much.

    • @javidx9
      @javidx9  5 лет назад

      Hey thanks Jack, its pleasing to hear its appreciated!

    • @jackmead7292
      @jackmead7292 5 лет назад

      @@javidx9If you're interested, go to www.speblog.org/datavis and click on "2D gravity". Implemented your demonstrated algorithm in a D3.js gravity simulation.

  • @harryfox4389
    @harryfox4389 5 лет назад +2

    I was following along until you vomited maths all over the program at the end.

    • @javidx9
      @javidx9  5 лет назад +2

      lol, I will confess this made me giggle a little bit XD

    • @Kitulous
      @Kitulous 5 лет назад

      @@javidx9 stop giggling!

  • @doctortrouserpants1387
    @doctortrouserpants1387 3 года назад +1

    Thanks, I've been scouring the web for hours, and your explanation is the clearest.. subscribed :)

    • @javidx9
      @javidx9  3 года назад

      Awesome, thank you!

  • @dehrk9024
    @dehrk9024 2 года назад

    *Me laughting my ass off*
    Javidx9: 0:05 :(

  • @helgesverre
    @helgesverre 6 лет назад +1

    I see you use hungarian notation, is this old habit or does it have any other benefits to you, seeing as any ide can correct you when using variables in an invalid context etc?

    • @javidx9
      @javidx9  6 лет назад +1

      Hi Helge, It's an old habit - but I still find it useful, firstly I don't like the IDE coding for me, but also in my head identifying the types quickly just makes things simpler, particularly because I mess around with casts and pointers and other bad things frowned upon today :-D, but which are in fact incredibly useful and powerful. Also I'm very much an embedded systems programmer, in that world clever IDEs are few and far between.

  • @likeyou3317
    @likeyou3317 5 лет назад

    Oh this is great, but there is some problem. I did this with same masses and without decreasing the speed whatsoever, and with every collision it seems that the particles get slightly faster... and faster and eventually u bearly see them. Did it with wikipedia method and same results. What's wrong here?
    edit... AHH Found it, I wanted to be smart and calculated the distance between circles just once, at the begining and forgot that I had to update it after repositioning colided circles...
    Well anyway thanks for explaining this, was looking it up on stack or wiki but couldn't really understand it.

  • @steffen5121
    @steffen5121 2 года назад

    My appreciation for computer games only grew...

  • @onecoding
    @onecoding Год назад

    Balls. Thank you for this very useful video.

  • @PauloConstantino167
    @PauloConstantino167 6 лет назад +1

    I had a quick look at the source code and even though I didn't see the header itself, I saw that you updated the vectors based on mechanics equations using the current elapsed time. I think that is not the best way to do this because you will always depend on the total elapsed time, and of course that variable has a physical size limit. The best way to do it is to consider time increments and steps, and use differentials instead.

    • @javidx9
      @javidx9  6 лет назад +1

      Hi Paulo, thanks for your observation however elapsed time in this case is fundamentally differential. You are correct that accumulating time is problematic, not just from a precision perspective, but it makes no sense to do it for the physics either as it implies that all past states of the ball are somewhat relevant, and they are not as soon as its properties are changed in a non continuous domain. What is more important regarding timing precision in this video is the elapsed time step is discrete, meaning time is abandoned after a collision; a topic discussed and implemented in video 2, where a time step is partitioned into a "chain" of sub-steps between non-linear events, encouraging convergence to a stable state.

    • @PauloConstantino167
      @PauloConstantino167 6 лет назад

      Ah so you reset it when colliding, which makes sense. But what happens if there are no collisions for a long time ? Anyhow, I love your work Javid. I wish you all the best and hope you always make new videos for us ! :)

  • @jiweonpark9958
    @jiweonpark9958 2 года назад +1

    ウマイ!

  • @rogershaffer8909
    @rogershaffer8909 4 года назад +1

    Hi again! In the near future my son and I are going to start learning programming. He is 17 and this would be his first programming language. I have studied Computer Science 25 years ago but haven't done much with it. I do have a summary understanding of C++ and think he could learn it with a group effort amongst us. What are your thoughts of Dev C++ as a starter IDE and compiler? Might you have a book on C++ or website that you recommend for newbies?

    • @javidx9
      @javidx9  4 года назад +1

      Hi Roger, personally i think C++ is a great language for beginners (though many do not). Ive not used dev-c++ so have no opinion about it, but if you're on windows Visual Studio is free, and fully equipped. Regarding books, im quite out of date, but there is a great stack overflow post "the definitive C++ book list" which is maintained with the best quality material as judged by the community.

    • @rogershaffer8909
      @rogershaffer8909 4 года назад

      @@javidx9 you sir are a real gent! Thank you for all that you are doing. What you are doing is something I was passionate about 25 years ago and just didnt have the intellectual capacity to do it. Not much has changed for me there but I got to try even in my old age. What better way to spent winter days with my son

  • @gael8
    @gael8 6 лет назад +1

    I don't understand when you have to use a class or when you have to use a struct... (I'm new in object oriented language) Could you explain me please?

    • @javidx9
      @javidx9  6 лет назад +4

      Hi Gaël, In principle they are the same thing. By default, all properties and methods of a struct are public, and in a class they are private. Struct comes from a time before classes, where variables could be grouped into a structure for convenience, it has stuck around to remain compatible with older code. I like to use structs to represent small objects, and classes to represent larger objects that have methods.

    • @gael8
      @gael8 6 лет назад

      Ok, I understand now. Thank you !

  • @vodkawhisperer3923
    @vodkawhisperer3923 Год назад

    thanks mate the collision algorithm works great

  • @janusmcgee8909
    @janusmcgee8909 6 лет назад +1

    I'm struggling to implement this in OpenGL, instead of x and y i'm sticking with x and z (i dont want the ball to go up in the air), however my issue resides in that i dont have a velocity, only speed, meaning i only have the position x and z values, not individual velocity.x or .z values (i have a heading that decides what position my object is facing). My main struggle is deciding where the ball should bounce when hitting another, my collision detector detects when a collision happens perfectly, however i cannot for some reason implement the correct angle bounce for both balls, using the formulae from the video seem to make it trade sides for me (i've gone over and checked they are identical to yours, the tan, normal, dot product etc). When i say trade sides, let's say a ball is at -80 on z, and a ball from 22 on z is coming directly at it, instead of bouncing the still ball away to lets say -90, the still ball will go to -60, is this a consequence of pairing these formulae with position coordinates rather than velocity values? Also, should i be using a different formula for the strict position values?

    • @javidx9
      @javidx9  6 лет назад

      If you have a heading and a speed then you have a velocity vector vx = cos(heading) * speed; vy = sin(heading) * speed; Also its very important to address all static collisions (overlaps) before trying to solve collision responses, you cannot do them per ball one at a time, the system needs to be stable first. In these systems, position is almost irrelevant, so not having a velocity vector is not going to help you much.

    • @janusmcgee8909
      @janusmcgee8909 6 лет назад

      Thanks for the response, especially that velocity x and y being cosine and sine of heading multiplied by speed respectively. Yeah, i really stabbed myself in the foot by going this route, i am only a begginer and i tried doing something different, which backfired completely. I was able to get collisions with multiple balls working somehow, i just need to calculate the angle of bounce to be a bit more accurate. Hopefully your velocity and heading information can make it possible, thanks for the help.

  • @emmettdja
    @emmettdja 3 года назад +1

    9:41 :D

  • @OLIV3R_YT
    @OLIV3R_YT 7 месяцев назад

    Thanks, this is really well presented!

  • @jason_vegas5304
    @jason_vegas5304 2 года назад

    Love you man... I was scratching my head for a week to get these results.. and now i think I have some idea on how I should go about rewriting my code to have tangential and normal responses.. btw I'm a beginner and working with python.. anyway logic is where I was stumbling.. thnks again..

  • @Larsykfz303
    @Larsykfz303 5 лет назад +2

    HAAHHAH BALLS (12yo btw)

    • @javidx9
      @javidx9  5 лет назад +2

      Its ok, I still giggle too XD

  • @poem-saxophoneTV
    @poem-saxophoneTV Год назад

    相信耶稣基督的话,你和你的家人会被救的。

  • @BanMashad
    @BanMashad 6 лет назад +1

    Hello, great video(not only this one). I have a question. When you do "DrawString" is possible to draw somehow bigger text then text fitting in one square? I mean something like "Game Over" in the center of screen... Thank you,

    • @javidx9
      @javidx9  6 лет назад

      Hi Ban, Firstly thanks!, Secondly - its not easy to have larger text. The console assumes each cell is one character. However, others have created sprite sheets, or used algorithms to "enlarge" text for display on high resolution consoles, so instead of displaying the text, they draw it to the screen pixel by pixel in a larger fashion.

    • @BanMashad
      @BanMashad 6 лет назад

      Hi, thanks for the reply. I was thinking about it. There is one solution. Maybe better. Using your "Fill" and create algorithm for region(4x8 - 8x16) then define "Fill" similarly like synth beat ".XX.X..XX..XXXXXX..XX..XX..XX..X" will be "A" for example in 4x8... if there will be library for each character you can launch something like FillText(startx, starty, "LUCKY", region). Well any better idea?

    • @javidx9
      @javidx9  6 лет назад

      Hi Ban, that's a fine idea - ultimately you need to store the pixel data somewhere. On the worms video source I create large numbers using some quite obscure code if you fancy working it out. It could be extended to your all characters with some effort :D

  • @nicholasgalliano8541
    @nicholasgalliano8541 Год назад

    You used a nested for next loop for your collision detection. Then it's not as bad as I thought - it could be the best and only solution. I've read that nested for next loops should be avoided when possible. Am I right in saying this? Is there another way to do this in order to avoid the nested loops? I guess that, if it works.. it works, although perhaps with more objects, the nested loops iterations might slow things down?

    • @javidx9
      @javidx9  Год назад

      There's nothing wrong with nested for loops.

  • @dagonmeister
    @dagonmeister 3 года назад

    Awesome tutorial, thanks javidx9. People complaining it was too much maths in the last mins of the video, one can also show how simple reflections can be implemented (no physics equations involved) though the results are not quite realistic, they almost do the job. Later to conclude the only realistic and proper way to solve it, is to add the momentum equation and electric collisions. Perhaps it needed two videos 30mis each!

  • @UFO26
    @UFO26 3 года назад

    14:00 There is an inaccuracy in this calculation. Sometimes the balls do not move completely and still the ball remains inside the target. Is this the error of the float calculations? Or does the algorithm give an error for other reasons?
    Translated with Google translator and God's help ))
    I am not a native English speaker ((

    • @UFO26
      @UFO26 3 года назад

      I'm sorry, the code works. I just didn't write down the formula correctly.
      And yes! Still, there is an error due to the float.
      Example:
      sum of radii = 30
      overlap of balls = 5 px
      after executing the function, the distance should be equal to the sum of radii = 30, but sometimes it is equal to 29.999999999999996.
      so for the fOverlap variable, I replaced the coefficient "0.5f" with "0.5000000000001"

  • @magnusm4
    @magnusm4 3 года назад

    Another way to reach zero without hardcoding it is to just give it friction. Simply drag but with a constant value instead of the ball's.

    • @billowen3285
      @billowen3285 3 года назад

      I think floating points could take a long time to reach zero

  • @loukitmyname
    @loukitmyname 4 года назад

    Probably gonna get ignored, but I am getting error "Screen Height/ Font Height Too big Could not Construct Console". Not sure what to do.

    • @javidx9
      @javidx9  4 года назад +1

      Sounds like your console is too big for your display.Try halving the fontw and fonth values in construct console function.

  • @Ireag
    @Ireag Год назад

    Thank you for this great video! I have one thing on my mind: Would the balls miss a collision if they were too fast? I mean if a ball would be before the target ball on one step and behind it on the next? If my thought is right, would it be easy to resolve that?

    • @Ireag
      @Ireag Год назад

      I just figured out, that your second video just starts with exactly that :D

  • @billowen3285
    @billowen3285 3 года назад

    As soon as I try to add the right click functions it does some strange behaviour, but my code is the same at 22:00... did something change in the PixelGameEngine?

    • @billowen3285
      @billowen3285 3 года назад

      Nevermind, I created a workaround by making another pointer

  • @MrWinterfressh
    @MrWinterfressh 4 года назад

    @javidx9 I ran into the issue that the although collision works in the correct direction, the overall speed of both entities will be changed.
    Am I doing something wrong or should change their velocity on both x and y axis, but still keep the same overall speed (i do sqrt(vx^2 + vy^2)) to check

  • @vancemccarthy2554
    @vancemccarthy2554 4 года назад

    I've tried this in Basic, but only got as far as having one ball colliding off multiple stationary balls.

  • @SaifUlIslam-db1nu
    @SaifUlIslam-db1nu 5 лет назад +1

    Can anybody help me with this? I have watched uptil 7:22, and when I try to select debug or select run without debug, I am faced with a blank console screen that only says "Press any key to continue". Let me know how you guys solved the problem. Thanks!

    • @SaifUlIslam-db1nu
      @SaifUlIslam-db1nu 5 лет назад +1

      Added a std::cin.get() to see if some error occurred in main(). Now, before "Press any key to continue", I see "Could Not Construct Console" which is the error message in main() for CirclePhysics game.

    • @SaifUlIslam-db1nu
      @SaifUlIslam-db1nu 5 лет назад +1

      If this helps anybody, here is the way to fix this. Change from "Debug" to "Release", and when you write game.CreateConsole(160, 120, 8, 8);, make fontw, ( 2nd last arg), and fonth ( last arg ) lower than 8. For some reason, it cannot construct that for consoles with fontw and fonth bigger than 7. My application is working with both Release x86, and Release x64.

    • @javidx9
      @javidx9  5 лет назад +1

      Thanks buddy, the reason it fails is you are trying to create a console that is too big for your display. But its really cool you figured this out!

    • @SaifUlIslam-db1nu
      @SaifUlIslam-db1nu 5 лет назад

      Actually, the debug mode is working as well, but with the release mode set, I got the warning that my chosen screen width/ screen height was too big, which I didn't get in Debug. Just make sure the console size fits your screen, as @javidx9 has put it.

  • @core36
    @core36 2 года назад

    the displacement is neat and dandy, as long they are not too fast. if a ball is fast enough so that form one frame to the next it appears on the other side of another ball, the displacement will go in the wrong way (or not at all if the ball is even faster). how would one deal with this unwanted "tunneling"?

    • @javidx9
      @javidx9  2 года назад +1

      Cast a ray along the velocity vector, test other objects against ray. Contact point is within radius and positive, then you have a collision.

  • @BOT-gx6qq
    @BOT-gx6qq 4 года назад

    Tehehe
    You said balls
    And collision

  • @ahmedbelloufa2503
    @ahmedbelloufa2503 3 года назад

    How to let the ball collides with the wall (top, bottom, left and right) and takes a new orientation?

  • @meeharbin4205
    @meeharbin4205 6 лет назад

    there is an easier way to get the equation for velocity after a collision. Instead of denoting the masses as m1 and m2. You can call them m and k*m, where k is a known constant.

  • @helicon2ify
    @helicon2ify 6 лет назад +2

    Great as always, thank you!

    • @javidx9
      @javidx9  6 лет назад

      Thanks lukas, you're welcome.

  • @vireyehome4060
    @vireyehome4060 4 года назад

    This trick of removing sqrt just blowed my mind. 15 years I was using this and now ... Like. And subscription.

  • @ValeriCFBG
    @ValeriCFBG 3 года назад

    Just a small optimisation: We computed the distance between the centers in DoCirclesOverlap, but we did not sqrt-ed it. We can pull this calculation to line 74, before the DoCirclesOverlap if and use it afterwards for the check and for the real distance calculation inside the if. Now we calculate it twice in the straight case and once if the balls do not overlap. After this change we would calculate it once in every occasion.

  • @berkcan3475
    @berkcan3475 4 года назад

    can i use this on server side only providing vel. and acc. of circles when a move started and affected other by colliding