Bouncing Balls, Round Two: Logarithms?!

  Рет қаралды 5,705

The Art of Code

The Art of Code

Күн бұрын

Пікірлер: 47
@djmips
@djmips 2 жыл бұрын
I think this is probably how they approximated bouncing in analog computer games from the ancient times like Tennis for Two.
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
If you can keep a state (store values between frames) then you can do bounces with simple multiplies and adds, so I would expect that almost all bounces are implemented like that.
@Shamysoza92
@Shamysoza92 2 жыл бұрын
Man! Using desmos for explaining math concepts totally blew it out of the water. These are the sort of things that are not seen everywhere. Totally love your content.
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Yeah Desmos is a great tool for that. :)
@realcygnus
@realcygnus 2 жыл бұрын
ALL such things are absolute staples imo & you're quite good at them & just as good at explaining them clearly. Priceless content, as always. 👍 Its mostly math that separates the script kiddies from the Sr. software engineers. I'm really digging this "series".
@GrinyaPLAY
@GrinyaPLAY 2 жыл бұрын
Thank you! Waiting for splines!
@kappasphere
@kappasphere 2 жыл бұрын
I can't really test right now because I'm on mobile, but iirc Desmos does subscript using underscore. (So just what you would type in latex) As an example, log base 2 of x would be "log_2(x)"
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Works! Thank you :)
@blaxxun75
@blaxxun75 2 жыл бұрын
I love the videos. The name of the channel nails it. I would be very interested in 2 things since i would love to create VR environments and apply the knowledge to something practical. How to get a dynamic shader onto a skybox without distortions in Unity (For example the starfield shader)? How to render raymarched "geometry" for VR in Unity? I know you showed something similar on a cube with a raymarched torus but not in a whole VR scene. That would be awesome to gather some insights and starting points on this 2 topics
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Noted!
@blaxxun75
@blaxxun75 2 жыл бұрын
@@TheArtofCodeIsCool Hi Martijn, thats great! Thank you! Cant wait!
@marcelliino
@marcelliino 2 жыл бұрын
Great video as always! Detailed explanation and straight forward.
@metalmastery5460
@metalmastery5460 2 жыл бұрын
Cool video, thanks! The bonus time somehow reminded me of an attempt to implement accelerated turning of a spaceship. PID controller oversteered badly and was wiggling the nose of the ship like that all the way to the target point. Ideas and topics: celtic patterns (personal kink, yeah), reaction diffusion, cellular automata and practical application beyond the game of life, raymarching in 2D (generation of distance field for non-sdf objects is a mess), height field in 2D and generated normals (or even shadows?), parallax mapping, particles with attractors, celtic patterns again
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Keltic Patterns eh? I could get into that ;)
@GrinyaPLAY
@GrinyaPLAY 2 жыл бұрын
Thank you! Can you explain more useful math optimisations for coders as you presented in this video for reducing loops with plain math function!
@CompositeNation
@CompositeNation 2 жыл бұрын
Thank you very much! I love to understand how I could use other functions!
@SellusionStar
@SellusionStar 2 жыл бұрын
would be interesting to do this with trigonometric functions. something like abs(cos(x)) * 0.8^(floor((x-π/2)/π)+1) where 0.8 is an arbitrary damping factor.
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
That would look a look like a bounce but since its not based on a parabola, the ball will not accelerate all the way to the ground and will therefore look 'off'. I suggest you try it.
@SellusionStar
@SellusionStar 2 жыл бұрын
@@TheArtofCodeIsCool Good point... btw I love your videos! all of them :)
@tidatida4145
@tidatida4145 2 жыл бұрын
The abs(cos(x) has it's mayor weekness when it comes to resolution and calculation. The calculation of the inflection point is not precise enough, that it results to exactly "0". Hence you will always receive an unpredictable drifting at the turning point. This method ist very fast and robust when it comes to this issue...
@66nos
@66nos 2 жыл бұрын
That's a great video. I love to see how we can use the math that we learnt in school, even tho I've probably forgot most of it. I didn't try it myself yet, but I think the two functions did the opposite of what were supposed to do because the variable i (the integer part of the number) had negative values, so it could have worked as intended if we did i = abs(i).
@djmips
@djmips 2 жыл бұрын
Not this method is only needed if you want to do a pure function / analytical bounce as a function of time. If you don't have that requirement, ie not a shader or can at least keep state around from a previous frame, you can use some simple code that needs nothing more than adds / subtracts and a compare. It will give you a nice looking bounce.
@djmips
@djmips 2 жыл бұрын
My code looks something like this (400 x 400 screen) . The gravity constant is dn which I initialized to 0.16. xs is initialized to some constant horizontal speed and the rest can be initialized to some other reasonable position on the screen and initial state. bx = bx + xs; if (bx < 90){ bx = bx - xs; xs = -xs; } if (bx > 310){ bx =bx - xs; xs = -xs; } by = by + n; n = n + dn; if (by > 310 ){ n = n - dn; n = - n; }
@djmips
@djmips 2 жыл бұрын
To make it bounce with a decay do this. if (n >= 0 && y > 310 ){ n = n - dn; n = - n*0.95; }
@GuilhermeOliveira-kr8vw
@GuilhermeOliveira-kr8vw 2 жыл бұрын
Nice video. I have a question related to performance. Does OpenGL (or the GPU really) have hardware implementations for log, pow, exp and so on? From my experience with C/C++, these functions are implemented by using for-loops with methods that accelerate convergence of their series expansions. In order words, you might have potentially exchanged a single small for-loop by several bigger for-loops.
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Its a great question. My understanding is that on modern hardware this functions are all built in and very fast, on the order of a couple of clock cycles. The slowest operations are the inverse trig functions and texture fetches.
@LeutnantJoker
@LeutnantJoker 2 жыл бұрын
Undoing the distortion was interesting. That's the difference between school math and applied math. You need to play around with the function to really understand their use.
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Yes, playing is crucial!
@themrpancake
@themrpancake 2 жыл бұрын
Really useful. Thanks again sir!
@lukasgehtdichgarnichtsan7906
@lukasgehtdichgarnichtsan7906 2 жыл бұрын
Awesome video!
@andrey730
@andrey730 2 жыл бұрын
Very interesting - I haven't developed intuition for logarithm function even though I can describe it and draw it But wasn't it possible to make the easing with exponential function like fract(x^n) instead of logarithm? It has the same property of getting it's period less and less with x growing. Why use logarithms then? I'm curious if there's logarithm unique uses that can't be done with expontial functions?
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
The exponential function and log function are two sides to the same coin, so I wouldn't be surprised if this could be implemented with that. I chose the log because it has an asymptote at 0, which I can use to fade the bounce to zero when t=1. I'd be interested to see other solutions though!
@NikolaNevenov86
@NikolaNevenov86 2 жыл бұрын
just awesome
@yankeshi5983
@yankeshi5983 2 жыл бұрын
nice tutorial, and why x -= (1.-pow(base,.5))/(1. -1./base) let the function offset to the middle? I found when r to the large number, it will not to the middle.and I use x -= log(1.-(1.-base)/2.)/log(base).
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
When I suspected the same solution as the one for undoing the distortion and plugged it in, it look right so I didn't think about it further. Good catch!
@yankeshi5983
@yankeshi5983 2 жыл бұрын
​@@TheArtofCodeIsCool love your tutorial let me have a deeper graphical understanding of functions by using desmos.
@gower1973
@gower1973 2 жыл бұрын
Why does the path traced out by the ball look sharp where it meets the x axis but blurs out as it reaches the apex and the effect seems to get worse as you go to the right, is it something to do with the aspect ratio or something else?
@KaedennYT
@KaedennYT 2 жыл бұрын
Press underscore (shift-hyphen) to subscript. This works exactly like caret (shift-6 on my keyboard).
@markuss.6094
@markuss.6094 2 жыл бұрын
26:00 this could be used for e.g. a rope that is being stiffened by the character pulling it!
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Yes!
@w.mcnamara
@w.mcnamara 2 жыл бұрын
Incredible as always! Could you consider posting your videos to Odysee? I use that platform a lot and it would be great to watch your vids on there!
@TheArtofCodeIsCool
@TheArtofCodeIsCool 2 жыл бұрын
Voila! odysee.com/@TheArtOfCode:b
@w.mcnamara
@w.mcnamara 2 жыл бұрын
@@TheArtofCodeIsCool incredible!! Thank you so much!
Live Coding: Making the American flag with math!
53:46
The Art of Code
Рет қаралды 10 М.
How to Create Cool Bounce Effects Using a Loop
26:32
The Art of Code
Рет қаралды 7 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Coding the Game of Life
30:01
The Art of Code
Рет қаралды 21 М.
Live Coding:Bending Light
24:29
The Art of Code
Рет қаралды 22 М.
Coding a crazy weave pattern
43:46
The Art of Code
Рет қаралды 12 М.
Useful functions for Game Designers: Easing Functions
24:51
The Art of Code
Рет қаралды 17 М.
Coding a Bezier curve from scratch!
27:04
The Art of Code
Рет қаралды 22 М.
Useful functions for game designers - Lagrange Interpolation
18:01
The Art of Code
Рет қаралды 18 М.
Live Coding an Alien Orb - Glitters & Blinding Light
27:48
The Art of Code
Рет қаралды 6 М.