Linear Collision Resolution in 2D Game Physics

Поділитися
Вставка
  • Опубліковано 15 чер 2024
  • In this video, you will learn a game physics technique to resolve collisions between rigid bodies.
    We'll learn the linear impulse method, used to resolve collisions by applying linear impulses to the bodies that are colliding.
    For a full course on 2D Game Physics, visit:
    courses.pikuma.com/courses/ga...
    Chapters:
    00:00 Introduction
    02:09 The Impulse Method
    03:19 Collision Relative Velocity
    05:05 Relative Velocity Along the Collision Normal
    06:00 Coefficient of Elasticity
    09:35 Difference of Momentum & Impulse
    14:44 Derivation of the Impulse Magnitude Formula
    22:19 Proposed Exercise
    25:49 Linear Impulse Method Function (Source Code)
    32:44 Conclusion & Next Steps
    For comprehensive courses on computer science, programming, and mathematics, visit:
    www.pikuma.com
    Don't forget to subscribe to receive updates and news about new courses and tutorials:
    / @pikuma
    Enjoy!
  • Наука та технологія

КОМЕНТАРІ • 57

  • @Bingo901
    @Bingo901 Рік тому +12

    I’m taking your course on 2d physics, it is just fascinating, makes me love coding and math more and more. Would love to see more of this content.

    • @pikuma
      @pikuma  Рік тому +1

      Thank you for the kind words. The only way I can continue to make more videos is if more students find out about pikuma.
      Every help in spreading the word helps. 🙂👍

  • @unknown-bx8my
    @unknown-bx8my Рік тому +1

    this is the best game physiscs video i have ever seen.

  • @danielzachtec
    @danielzachtec Рік тому +1

    Absolutely great video, great explanations!

  • @Mong3
    @Mong3 Рік тому +2

    Akita brought me here! So nice to discover your content.

    • @pikuma
      @pikuma  Рік тому +1

      Grande Akita. Ajudando os canais menores. 😄 Abração e obrigado pelo follow.

  • @macaco_agiota
    @macaco_agiota Рік тому +1

    Really great content! Thank you!

  • @fabianrr
    @fabianrr 2 роки тому +3

    Muy bueno, me compre el de Raycasting Programming with C 👌

  • @joseloeza371
    @joseloeza371 2 роки тому +1

    Excelent Gustavo...

    • @pikuma
      @pikuma  2 роки тому

      Gracias Jose! All the best. 🙂

  • @vincenzopanella2705
    @vincenzopanella2705 Рік тому +1

    I bought his course on 2d physics and it is the best online course i have ever taken, he goes deep into advanced physics, all the other stuff i have read online is incorrect and incomplete, buy his course and you won't regret it.

    • @pikuma
      @pikuma  Рік тому +1

      Thank you for the kibd words, Vicenzo! It also helps that the topic is extremely fun. 🙂

    • @vincenzopanella2705
      @vincenzopanella2705 Рік тому

      Hello Gustavo!, are you italian too ??? cannot wait to see your next 3d physics engien programming

    • @pikuma
      @pikuma  Рік тому +1

      I'm from Brazil. 🙂

  • @SkSafowan
    @SkSafowan 2 роки тому +3

    Great content really, you should use some audio software to clear out the walkie sound

    • @pikuma
      @pikuma  2 роки тому +1

      Thank you. I'll eventually learn how to do this video thing. 🙂
      I'll look into it. Let me know if you have any suggestions of software that does that.

  • @Arkensor
    @Arkensor 9 місяців тому +2

    Hello, thank you for the good explaination. I have one question and that is that the final resolve does not seem to take into account HOW MUCH overlap there was between the objects. The calculation result seems to be the same no matter if they barely touch or one object penetrates deep into the other. Can this be ignored and if so why? I would normally imagine that you would want to make sure that at the end of a discrete time simulation step the two objects are not colliding anymore and have their final velocity for the next simulation tick to then move away from each other.

  • @StarContract
    @StarContract 2 місяці тому +1

    Very nice,
    I wonder if you also made a video about collision resolution that involves angular momentum

    • @pikuma
      @pikuma  2 місяці тому

      Absolutely! 🙂 It's all included in the lectures.
      🔗pikuma.com/courses/game-physics-engine-programming

    • @StarContract
      @StarContract 2 місяці тому

      @@pikuma nice, I just finished watching your verlet integration video.
      I have a question -
      I am implementing a 2d physics engine and I want to support multiple constraints like springs, to enable exotic features like soft bodies. Does your course go over topics such as a system of constraints, verlet integration etc?
      If it does, I'll buy it right now

  • @jiriblahos4051
    @jiriblahos4051 2 роки тому +4

    Great stuff! I would have one question though. Isn't it possible that you resolve a collision, then move your objects in one time step, and they are still in contact after that? Since the game runs in discrete time steps and you need to get an overlap to detect the collision. That could cause you to resolve the same collision twice which would be nonsense I suppose.

    • @pikuma
      @pikuma  2 роки тому +4

      That is a great question! You see, you are absolutely correct. In this video I focused on the "detection" part of the collision, and not on the "resolution." To resolve the collision we have many different techniques; some engines perform a position correction to first offset the bodies away from each other so they don't collide anymore, and we also have engines that apply 'impulses' to both bodies to simulate a proper collision resolution.
      These techniques have pros and cons. Just changing the position of two colliding bodies might be okay when we only have two bodies, but what happens when we have multiple bodies touching each other (fixing one collision and offsetting the bodies might cause other collisions with other neighboring bodies). You see, it's a tricky game.
      What most engines do is to work with "constraints", and solve both position and velocity constraints of the system. The engines either solve a huge system of equations to account for all the bodies, or solve one collision at a time but iterate to get closer to the best solution each pass of the iteration.
      I'm sorry, your question might look simple but it's the core of super complex discussions in game engines. There are many methods that engines use to solve that.
      As always, if you're interested to really dig deeper, check out my 2D physics course at pikuma.com. :)

    • @jiriblahos4051
      @jiriblahos4051 2 роки тому

      @@pikuma Thanks for a great answer! I realise that it is a complex topic for sure. One idea that I had was that you could add condition to collision detection that the bodies need to be moving towards each other along the normal vector. So after you resolve the collision the first time, the bodies start moving away from each other so the next step you wouldn't detect any collision even if the overlap still exists. But I haven't thought it through entirely, it could have some issues.

  • @halleffect5439
    @halleffect5439 2 роки тому

    So the parallel component of the relative velocity is not affected, if i am right?

  • @Test-iv4pm
    @Test-iv4pm Рік тому +1

    Do you have a lesson like this for polygons / with rotations?

    • @pikuma
      @pikuma  Рік тому

      Sure. I go over 2D polygon collision detection and resolution in the course (2D convex polygos).

  • @va9if
    @va9if 2 роки тому +1

    You went so far back to the relative velocity explanation. It took a bit long cuz if this but still is nice

    • @pikuma
      @pikuma  2 роки тому

      I know. Just need to make sure we don't leave anyone behind. 🙂

  • @krumss
    @krumss 2 роки тому

    any new video? I love your video

  • @davidemarchese7569
    @davidemarchese7569 20 днів тому +1

    if the objects in my scene are subject to acceleration will the method still work?

    • @pikuma
      @pikuma  20 днів тому +1

      Absolutely. All the forces of your system will result in an acceleration, which will change their velocities, which will then change their position. Collision check happens after the pisition was updated and the resolution proceeds from there.

  • @unknown-bx8my
    @unknown-bx8my Рік тому

    does apply impulse change acceleration too?

    • @pikuma
      @pikuma  Рік тому +1

      No. Acceleration happens when there are forces being applied to an object. Applying an impulse, in our simplified version of things, is basically just an instantaneous change in velocity.

  • @user-ji1gf6vv8u
    @user-ji1gf6vv8u 4 місяці тому

    for some reason in my implementation of this code causes the objects to get energy out of nowhere over time, do you have a solution to this

    • @pikuma
      @pikuma  4 місяці тому

      This is probably connected to the type of integration you're using for the simulation of movement. Can you kindly check my video on Verlet integration please? There, I mention some different integrations that instead of gaining energy end up losing energy over time (but a lot slower).

    • @user-ji1gf6vv8u
      @user-ji1gf6vv8u 4 місяці тому

      ​@@pikumaok I have shifted to verlet integration but the problem very much remains, when I put project the bodies directly at one another along the X or Y axis or 45 degrees with respect to the one another the program runs as expected but when subjected to more unusual angles extra momentum is seen among the bodies

  • @Test-iv4pm
    @Test-iv4pm Рік тому

    How do you know which object (A or B) gets the +J and which one gets the -J?

    • @Test-iv4pm
      @Test-iv4pm Рік тому +3

      Actually I am thinking now that it doesn't matter, because if A and B labels are swapped, the sign of the relative velocity also swaps

    • @pikuma
      @pikuma  Рік тому

      @@Test-iv4pm Correct. The magbitude of the impulse should be the same, just flipped.

  • @gcpezzi
    @gcpezzi 2 роки тому +1

    If anyone wants the snippet of code that I used, feel free to ask here in the comments. :)

    • @prometheusli101
      @prometheusli101 2 роки тому +1

      i dont use C++,just learn concept in this channel

    • @pikuma
      @pikuma  2 роки тому

      @@prometheusli101 That's a good approach. :) This should be valuable regardless of the language you use. What's your language of choice?

    • @prometheusli101
      @prometheusli101 2 роки тому +1

      @@pikuma dart, seems nobody teach this by using dart,so i can only choose this approach

    • @pikuma
      @pikuma  2 роки тому

      @@prometheusli101 True! Dart is a great language, I believe Bob Nystrom works in the Dart team. Meanwhile, I'll try to keep things as language-agnostic as possible. All the best.

    • @prometheusli101
      @prometheusli101 2 роки тому +1

      @@pikuma you needn't change any thing,good enough already,thanks for your work

  • @GoatsOnAParachute
    @GoatsOnAParachute 28 днів тому

    This video is missing a huge component: how to calculate the collision normal n. For spheres, I think this is the difference vector between the two centers of the spheres at time of collision.

    • @pikuma
      @pikuma  28 днів тому

      This is part of collision detection and collision information, which is shape-dependent.

    • @GoatsOnAParachute
      @GoatsOnAParachute 28 днів тому +1

      @pikuma But for the spheres example in this video, it's what I said above correct?

    • @pikuma
      @pikuma  28 днів тому +1

      @@GoatsOnAParachute Yes. Spheres are easy to detect and find the collision info. 🙂👍

  • @Midredel
    @Midredel 6 місяців тому

    При столкновении объектов не происходит тангенциального вращения т.к. это нарушает закон сохранения энергии. Механика, стр. 162, Сивухин Д.В., 2005
    When objects collide, tangential rotation does not occur because this violates the law of conservation of energy. Mechanics, p. 162, Sivukhin D.V., 2005

    • @user-bc9tu6om8b
      @user-bc9tu6om8b Місяць тому

      Столкновение между абсолютно упругими там расписывается, а не как в физ движке

  • @thomasamathew4058
    @thomasamathew4058 Рік тому +1

    Did you stop YT?

    • @pikuma
      @pikuma  Рік тому +1

      No, I'll still post videos here every once in a while. But since UA-cam is not a great platform for structured learning, I'm mosrly focused on other content.

    • @thomasamathew4058
      @thomasamathew4058 Рік тому

      @@pikuma other content ? Your website ?

    • @pikuma
      @pikuma  Рік тому +2

      @@thomasamathew4058 Well, yes. But my lectures for the university as well. 😬

    • @thomasamathew4058
      @thomasamathew4058 Рік тому

      @@pikuma oh okay. I checked out your course and it is everything I wanted..I'll buy it soon.

  • @Felis0000
    @Felis0000 Рік тому

    I'm unable to contact you on the messenger ;(
    I promise I won't inconvenience you, again.
    I may be tiresome to you at this point, I apologize for this state. I wish to learn C++ game engine and physics engine development more than anything else right now, and I think my request justifies by the unfair conversion rate considering our economical value, trust me most of the people are impoverished here due to the bajillion population count and other platforms offering things at reduced cost after conversion. Sir, please let me know what you consider.