03 - Writing a billiard simulation in 10 minutes

  Рет қаралды 11,886

Ten Minute Physics

Ten Minute Physics

Күн бұрын

For the source html code and all other tutorials see matthias-research.github.io/p...
Here I show you how to handle collisions of balls in 2d. For this we create a billiard simulator in a stand alone html document that runs in any browser. I recommend to first watch tutorial 01.

Пікірлер: 15
@ttrkaya
@ttrkaya 2 жыл бұрын
Great content, much appreciated sensei! I'd suggest colliding the balls and applying impulses only when they are moving against each other, i.e; when the dot product of their velocities is negative. The reason is: due to numerical precision, friction or multiple collisions happening at the same time, the colliding balls can end up still overlapping after the collision. In the next frame, even though they were moving away from each other, we shouldn't apply the same collision logic to this overlapping pair, because it will just turn them against each other. Hence they will flip at each frame. I've experienced this happening on many projects. It is a headache to debug, but the solution is very simple.
@TenMinutePhysics
@TenMinutePhysics 2 жыл бұрын
Very good observation. You are absolutely right. That check is missing in the code!
@alexisdupuis5616
@alexisdupuis5616 Жыл бұрын
It's an interesting observation but I don't think your solution is correct. What if 2 balls are moving in the same direction but the one behind is faster. Eventually it will catch up to the slower ball in front, and the collision will be dismissed by your test. Imo the best way to handle this is to apply a slight overshooting when separating the balls by updating their positions. Multiplying the correction by a small amount like 1.01 is enough to prevent issues related to numerical precision. I'm not sure if double collisions are at fault at all here, since if a second collision causes the ball to immediately re-enter a first collided object, this second collision will also flip the velocity, so we're back to normal when re-handling the first collision a second time.
@ghostbusterz
@ghostbusterz 9 ай бұрын
I think the inverse would be a good solution - dismissing any collisions when the velocities are moving the objects *away* from each other. I believe that would be a positive dot product.
@ttrkaya
@ttrkaya 9 ай бұрын
​@@alexisdupuis5616 Very well spotted sir! You are right. My actual solution that I use is actually not the dot product of the velocities, but instead: *the dot product between the difference in position and the difference in velocites* Perhaps a pseudocode will be easier: ``` Vec2 deltaVel = objA.vel - objB.vel; Vec2 deltaPos = objA.pos - objB.pos; bool ignoreCollision = dot(deltaVel, deltaPos) > 0; ```
@guilhermejofili
@guilhermejofili Жыл бұрын
Once again you provided lots of fun! Thanks professor!
@ntrgc89
@ntrgc89 Жыл бұрын
This is fantastic, thank you for putting this together!
@dr_ned_flanders
@dr_ned_flanders Жыл бұрын
These tutorials are wonderful. Thank you so much.
@mariovelez578
@mariovelez578 Жыл бұрын
If only this video existed when I was trying to figure this out when I was in high school
@hoytvolker3
@hoytvolker3 2 жыл бұрын
I like the simplicity to the point :)
@ThatNiceDutchGuy
@ThatNiceDutchGuy Жыл бұрын
You, removed so much fluff. Awesome serie, and thank you!
@alexisdupuis5616
@alexisdupuis5616 Жыл бұрын
I think there's a small mistake in the way collisions are handled that can cause infinite increase of the Velocity on some basic cases. When applying the correction to the positions to fix the overlaping objects, we are basically doing the same thing we are doing to solve constraints in PBD. But this time we don't update the velocities to reflect this positional correction. It's easy to see the issue by letting a ball bounce on a ground collider. At each frame, the ball has fallen slightly more than physically possible due to the collision overlap. When reflecting the velocity, we are thus letting the velocity grow a bit more than physically possible at each new bounce. This leads to a very noticeable drift/increase in energy. I corrected this by substracting the velocity gained during the deltaTime between the actual collision and the moment it is detected and handled in handleBallsCollisions. I had to store the acceleration of the particle though (which I do in the updateVelocity method). Then assuming a constant acceleration during this deltaTime, we find that: correctedVelocity² = velocity² - 2 * acceleration * correctionDistance
@alexisdupuis5616
@alexisdupuis5616 Жыл бұрын
Btw the reason why this is usually not observable in your videos is because either you handle ground collisions by simply preventing objects y coordinate to fall under 0, or in the case of video 11 you use floating balls, so the gravitational acceleration is not causing any issues.
@MsJeffreyF
@MsJeffreyF Жыл бұрын
I think there are situations where if there's 3 collisions in the same frame, depending on the order of the balls, 1 of those collisions might not get corrected if the ball is earlier in the array because of the way the for (j = i+1...) loop checks only the latter part of the array. I'm not sure what's the appropriate fix for this Btw these tutorials are amazing. I've been fairly scared of web programming, but doing these so far is giving me more confidence. Thanks a ton!
@sinam1951
@sinam1951 Жыл бұрын
Amazing ❤
04 - How to write a pinball simulation
16:37
Ten Minute Physics
Рет қаралды 9 М.
05  - The simplest possible physics simulation method
11:05
Ten Minute Physics
Рет қаралды 13 М.
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 9 МЛН
Lehanga 🤣 #comedy #funny
00:31
Micky Makeover
Рет қаралды 24 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
02 - Writing a VR physics simulation in 10 minutes
8:46
Ten Minute Physics
Рет қаралды 7 М.
01 - Writing a physics simulation in 10 minutes
12:45
Ten Minute Physics
Рет қаралды 26 М.
The Clever Way to Count Tanks - Numberphile
16:45
Numberphile
Рет қаралды 820 М.
06 - The simplest way to simulate a triple pendulum
9:54
Ten Minute Physics
Рет қаралды 14 М.
Mastering Pool using Physics
9:46
yazan sakkab
Рет қаралды 68 М.
Nature's Incredible ROTATING MOTOR (It’s Electric!) - Smarter Every Day 300
29:37
Building a Physics Engine with C++ and Simulating Machines
11:23
AngeTheGreat
Рет қаралды 689 М.
12 - 100x speedup for soft body simulations
8:11
Ten Minute Physics
Рет қаралды 8 М.