Detecting collisions Susan Ibach | Technical Evangelist Sage Franch | Microsoft Student Partner
At the moment nothing happens when our hero hits an obstacle
How do we detect collisions between objects? Pseudo code: (the robot runs into an obstacle) Remove a life from the game. Sprite objects have a built-in collision detector! Why not “if robot->overlaps with(obstacle)”? -> scope of robot. Don’t do anything. (Just keep playing!) Needs to be checked “on every frame”.
But there’s a catch! Remember…sprite objects are rectangular! If the RECTANGLES overlap, then the sprites overlap. Overlap? Overlap? No! Yes!
Using “overlaps with”
Why am I losing multiple lives when I hit an obstacle? Remember we are checking to see if we overlap on EVERY frame Frame 1 Obstacle and hero overlap Remove 1 life Frame 2 Obstacle and hero overlap Remove 1 life
Think like a coder…
Your challenge Add code to remove a life if the hero hits an obstacle
Congratulations! You’re getting close!
Vocabulary and concepts Pseudo code: Literally, “fake” code, used to sketch out ideas in code without paying attention to the syntax. Sprite: a 2-D image or animation that is integrated into a larger scene. Collision detection: In TouchDevelop, a sprite is always a rectangle. Often, much of the rectangle is transparent. Two sprites collide when their rectangles overlap. “overlaps with”: A function on a sprite used to detect a collision with another sprite. E.g. obstacle->overlaps with(hero). This function should be checked on every frame.