Detecting collisions - Intermediate Python Programming p.20
Key Takeaways
The video demonstrates operator overloading in Python using custom dunder methods like __add__, and collision detection between blobs by calculating the distance between their centers and comparing it to the sum of their radii, utilizing tools like numpy and the norm function. It also covers handling collisions between blobs by defining a function to handle collisions and updating the blob list accordingly.
Full Transcript
what's up everybody Welcome to part 20 of our intermediate python Series in this video we're going to be building on the last video which we talked about how we could do operator overloading create our own Dunder ad method which would allow us to let's just use these two um you know self is the object itself and then other blob would be the second parameter um it'll allow us to basically do this self plus other blob and it'll just work you don't there's no parentheses sees here there's no real typical um you know passing of parameters it just works it's amazing it's incredible and uh now that we have that we need to have some sort of handling to know when to use it so we need to know when two blobs are touching each other so to do that I mean that's not the easiest operation ever uh but that's why this has its own tutorial so from blob. py we know that every blob has its own X and Y which is the center of that blob and then we know how big the blob is with size and that size is actually the radius I'm pretty sure of that blob so what we could do is we can calculate the distance between two blobs what's that distance and then if that distance is less than the radiuses of those two blobs added together those two blobs must be touching so that's how we're going to know if they're touching so to do that we're going to use um we're going to calculate the norm the norm is a way of calculating distance between two points on a plane um also you might have heard of like ukian distance or something like that um and if you want to know more about this I do actually have a tutorial specifically on ukian distance and using Norm um it's for the machine learning series you don't actually have to follow along that series you can just straight into ukan distance slorm if you like um but it's not necessary we're going to import numpy as NP and then we're going to come on down and above draw environment we're going to add a new function we're going to say Define is touching and it's going to take two parameters B1 B2 for blob one and blob two and then we're just going to ask if np. linal for lin algebra. Norm for the norm if that of basically and we're going to say np. array of um b1x B1 y so these are the locations of that blob and again this this is this could be on an infinite dimensional plane so you have three dimensions 15 Dimensions or in our case just simple two Dimensions so it's ukian distance but you could go further if you wanted if you had 3D blobs for example this would still work um let's see before I get myself lost you want to take that it's the norm and when you calculate the norm it'll be that minus um basically the same thing only B2 okay if that is less than b1. size plus b2. size and we'll put spaces around our plus because we're good little pepers if that's the case uh you know we want to return true otherwise we would uh turn false but and actually what we can do is simplify this even further um and just do this right this will return true or false make sure you get rid of the colon at the end though um but this will just return true or false now most of the time I'm kind of against oneline functions but this function will allow us to just have this much text rather than this much text on every time we want to compare two blobs now in our case we're probably only going to call this once anyway um but it makes sense to me so that's how we know if they're touching now we still have to actually iterate through all of our blobs though and find out if they're touching right ask this question so now we got to do that so now we're going to have a new function and it's going to be uh Define handle collisions and it's going to take whatever the list of blobs is and this is this pains me but we're going to do it this way Reds or Blues Reds greens equals blob blist so we're going to unpack them this is very much a local function that you would not take to anyone else um because we're basically hard coding the order of this list but we're going to do that so we don't have to know or pass the values of what color never mind anyway we're just going to do it this way I'm trying to think if there there's probably a better way to do this I'm going to carry on here but um there's got to be a better way anyway um new blob dicks uh is going to be an empty list because blob lists is basically we're going to write a new blob list um now and in fact actually I think we'll get rid of this we're not going to use that now we're going to do is for blue ID I should have prepared much better for this I'm sorry blue blob in blues. copy. items so why might we be doing this as soon as you see this we know that um Blues For example blob list is a list of um these like this is the blob list Blues Reds and greens it's a list of dictionaries so we know Blues is a dictionary Reds is a dictionary greens is a dictionary so when you see this for something something in something copy right we know what Blues is it's a dictionary this is key value so as soon as you see someone say copy. item there's really only one reason you would ever copy the dictionary as you iterate through it or or make a copy before you iterate through it and that is you're going to modify it because you never want to modify the thing that you're iterating through especially because there are going to be times where you'll get away with it most of the time so it's dangerous um so in this case we're actually going to make a copy rather than going through the real one and then what we're going to say is for other blobs in blues Reds greens what are we going to do so what we're going to say is for the other blob ID other blob in other bobs.com copy do items what are we going to do well if blue blob equals other blob we're going to pass what's this basically we're asking is this blob touching itself right um settle down guys so basically we're just saying are we evaluating the exact blob that we're evaluating right because we don't CU this will ring true for every blob every blue blob as we iterate through a copy of the blue blobs we're going to have one blob that it it's going to it's going to check against itself and if you check against itself and you passed that it's that blue blob as blob number one and blob number two this would come back as a true so we just want to make sure that we don't do that so we're passing here because otherwise it would always come back true for that one blob else if is touching blue lava other blob if they're touching we're just going to say blue blob plus other other blob now we could be done and if we were done at this point we could say um n i don't I keep thinking of way better ways to do this but I'm going to keep going on um um if if we were done at this point we actually wouldn't have needed to copy both of these dictionaries but we what's going to happen is as was seen in the last video sometimes when these two blobs come together one blob gets to be worth zero or less in size and when that happens that Blob should be dead we need to just remove it from the scenario otherwise we're going to be iterating through it every time it might actually touch another blob somehow and all kinds of bugs could could arise so what we need to do is we're going to say if um other blob. size is less than or equal to zero at this point after we've done that operation we're going to D other blobs other blob ID and then we're going to say if blue blob. size is less than or equal to zero Dell uh Blues Blue ID perfect so now at the end of all of this we return Blues Reds greens beautiful now we'll save that and basically all we need to do is come down to draw environment we're going to still fill that um green but we're going to say now we're going to take this copy that blue Reds and greens paste that blue Reds and greens we're going to say that's equal now to handle Collision we pass that blob list that was passed into draw environment and now draw environment is going to have to Output this so we're just going to do actually we'll just copy this line um after the update return Blues Reds and greens so now you might must be guessing that we have one more thing we're we're going to have to do coming down here you've got this right blue blobs red blobs green blobs those are coming from up here these original ones so actually what we need to say now is just this list paste equals that I'm going to run it and then we'll run through one more time um what's happening I just want to make sure didn't have an error that was what the goal was less than or equal to zero let's try again handle collisions is not defined handle Collision collisions try again items really where did I where's the other copy there it is items that's a lot of Errors my my friends all right let's hopefully we can see one if we don't darn this random let's add oh there we go okay so that blue blob just consumed actually that's off the screen darn it okay that's okay it happened for me you guys will have to take my word for it actually not really let's just rerun this one more time let put these blobs up here let's see if we can spot I'm just looking right now to see if anybody's close nobody is close to doing doing anything maybe I'll just have to restart it oh there we go this blue blob just consumed that green one you can scroll back if you want to see it but anyways that happened um I'm hoping to see maybe these two combine or these two maybe can you see my yes you can there we go oh man that red one just ate that blue blob all right so um we think everything's working as intended at least visually it appears that things are working come on guys come into contact oh this one just ate something so anyway as you can kind of see things are are working it'll be interesting to see what would happen after you know a few hours or something how these blobs would be handling each other um probably you would just have a couple like really really big blobs after a while anyway that's all for now so um I did say I would run through one more time so let's go ahead and do that just in case something was a little confusing um so basically we start with the blobs that we have that were just basically just generated on random then we get into this wild true loop we're passing those blob dictionaries and they're just being passed to this draw environment so we pop on up to draw environment which takes blob list which it took here and we know it's going to return something back to these values and then this Loop just keeps going on so we're let's go see what what it returns so we pass that blob list great we take that blob list we do the same things that we've always been doing and then now we are newly returning the new values because we are going to be it's going to be a changing state from this point because before this never changed it was always these blobs but now they change so we take them and the first thing that we do is we fill white um that's bothering me a lot of times you're going to actually probably separate I mean this is not a g a p game tutorial I need to redo my pie game tutorial but a lot of times you're actually going to separate the drawing of the pame stuff from the processing stuff to make pame run as fast as possible so actually you would not fill the display with white and then run this operation that would be dumb so we'll pull that apart and eventually you would probably run this in a separate process or something but that's for another time so um anyway we pass this we handle collisions all that handle collisions does basically just runs through iterates through all the blobs are they touching if they are we use our lovely Dunder ad method to change what needs to be changed return the new dictionaries and life is good so that's what's happening here so that's all for now that's our operator over loading and actually applying that operator overloading to our blob world uh if you have questions comments concerns leave them below if you have a suggestion especially for handle collisions I have a couple ideas that I I almost applied live on video and and I I just thought better of it um but this is almost certain to be improvable so if someone comes up with a really great way to improve it awesome if you come up with a simpler way to calculate the is touching let me know too that'll be interesting so anyways leave that stuff below too otherwise I will see you in another tutorial
Original Description
Now that we've got handling for the "+" operator, we need to devise a way to actually detect when we need to use it. In this tutorial, we're going to write the functionality that will detect when two blobs are in contact with each other.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI