Get Kids into Coding with Scratch

Implementing Collision Detection

Steve Kinney

Steve Kinney

Temporal
Get Kids into Coding with Scratch

Check out a free preview of the full Get Kids into Coding with Scratch course

The "Implementing Collision Detection" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:

Steve implements a code block to detect if the hero sprite is touching a platform. The code block keeps track of the hero sprite's position and moves the sprite back to its last known position when the sprite is touching a platform.

Preview
Close

Transcript from the "Implementing Collision Detection" Lesson

[00:00:00]
>> All right, we're not gonna really use this one, we're gonna use it for parts in a little bit. We're gonna create a new one to help us out here, how we haven't used in this one we had like move 10 steps, we're gonna make our own special version, okay?

[00:00:19]
Now we'll make a block. And you could give it the same name. We're given an input, Let's feel wrong move in 10 years. Small steps. [LAUGH] Right. Move in 10 steps and check to see if I crash into a wall, right, whatever you want to name it, it doesn't matter, like, it's all the same.

[00:00:54]
So we've got this new function called move small steps, right. Which point, like I said before, the, just to go back, the algorithm here is, if we wanted to move 10 steps, look where you were, move one step, are you in wall? No, keep track of that place, move next step, are you in wall?

[00:01:16]
No, move next step, are you in wall? Right? And so, we can do that, and then, you're in wall? Go back to the last place, right? And then we can use that for both cases, so at this point, by definition, how many times do we want to run this code?

[00:01:37]
Well, the number of steps we wanna take, right? Let's call this steps. So yeah, exactly, which is whatever number we pass in there, the same way for the move block, you can give it a given number, maybe it's 10, maybe it's 20, right? It could depend as we work on our gameplay, so in relative error are 10 hard coded well let them put in a number and that's what we'll do, because like moving might be different than jumping.

[00:02:05]
You know what I mean? So for here, we'll just go ahead and we will repeat this. You grab this one here, that number of times, so if they put in number 10, you know many times they're gonna repeat this loop. 10 times they put the number 20 You know many times we're gonna do it 20 And we're gonna make a new variable for this sprite only called last why?

[00:02:32]
Which is before we move you, we want to keep track about where you were right Because in case you end up with a wall and the next step, we need to know where you were previously. And then we'll put you back where you're supposed to be, so we've got this last Y.

[00:02:55]
And each time we're going to set that last Y to whatever the current Y position is, right, so like where are you now? Okay, now we're gonna move you, are you in wall? If so we kept track of where you were previously and we put you right back.

[00:03:15]
Well we're gonna do for X and Y, we'll do Y first I mean, yeah, I talk about walking into a wall 'cause it is easy, I guess I'm always on the ground, right? And like, I promise that I wouldn't jump and wave my arms in the air at the beginning of this, so but good ca good call out.

[00:03:33]
So, then whatever that number is and however, far the wide velocity is, that's how many times we need to check, so we do need a little bit of division this time.
>> Should we do run without screen refresh?
>> Yes, yep, and if you didn't do that, you can right click on it, go to edit, just make sure that run without Screen refresh button is checked.

[00:03:54]
Okay, so whatever the velocity that we're going is, we wanna divide it by the number of steps we were asked to go, right? So, this is just a nice way to do division, which is if we're attempting to go 10 steps or whatever velocity, divide it, and you should get the right number.

[00:04:14]
So we'll go ahead and we'll say change y. And we need to do the division, the current velocity by how many steps we're checking for, so we'll just some division. And this is just gonna be how much farther we have to versus how often we wanna check, so it's the by steps, that is probably the hardest math that we're gonna do in this section.

[00:04:45]
That's not sure there's an absolute value hiding somewhere later but, so we got that and then we'll go. If we're touching the danger color Then actually go back to the last good spot, right? You know what, [LAUGH] Don't go where you were going, we changed our minds. Right?

[00:05:19]
You go back to where the last known good spot was, that last Y, right? And you are done moving and we set last Y, or we set Y speed to zero, velocity Y. Alright, I'm going to talk through this one more time, because there's a lot going on, if I say move in ten small steps, do this ten times, right?

[00:05:43]
20 times whatever I say in there, where were you? Go the next whatever step was. If you somehow ended up in red, we're going to ignore the fact that we made you take a step forward and we're going to put you right back to where you were in the last known good spot.

[00:06:02]
And then you are done jumping at that point you are on the ground if going down was going to put you inside ground. It means that you are done jumping now, right and you shouldn't have to do that anymore, we get rid of fixed landing by and we move back in our new Our new version, now the only tricky part here, actually, we don't even need this, we've got our own if statement, we'll just move you, this will handle everything.

[00:06:34]
The only tricky part is, going down, we have a negative number, we can divide by a negative number, right? So even if we're going down 10 steps, we need a positive 10, and if we're going up 10 steps, we need a positive 10. Alaska kids versus anyone know how to always get a positive number no matter what, and then the adults get to like, go to high school math, yeah.

[00:06:58]
[INAUDIBLE].
>> Okay so one math operator that we need is this absolute value, the absolute value of 5 is 5, the absolute value of negative 5 is 5, right? It's just no matter what you get a positive number, and we'll do that for the absolute value of velocity Y, let's put our hero where he belongs to in the beginning.

[00:07:27]
I wonder if, because I'm doing it after the jump loop, I have a hypothesis, I don't know if it's right, no, we're still sinking into the ground, all right, let's see. We change by negative one, basically, we press, we're going to do the jump motion, you know what?

[00:07:45]
We're changing y twice, right? I change Y, and then in my code, I change Y again, so if you do the same thing twice, things will break on you. Look at that, and he jumps, okay now, the big question, I actually don't wanna start I'm in the right spot because I don't have the ability to walk left and right yet.

[00:08:09]
Until I have that ability that little like, I should do this to be nice to myself, I'm not being very nice to myself, okay, jumping, the big question Caleb can he jumped through ceilings? Right, because we're setting it to zero, because it was positive, now if I hold space, I'm holding space, he will continue to shoot his head into the wall and bang it repeatedly.

[00:08:34]
But even on the way up, if you're now touching red, right, then don't, then stop going up, set it to zero, which point he's no longer going up, now the act of holding. The Space key is trying to reset it to 10 over and over and over and over and over and over and over over over again.

[00:08:52]
Right but the mechanic that we have at the very end of do not go into walls had first taps in and says no, and it does without a screen refresh. So this hall he would have theoretically be going to the wall by 10 are getting pushed back down, right?

[00:09:07]
I got to see it, you don't have to say it, I have to say it. This is I slowed my code down to such a sad rate that we don't see it. [INAUDIBLE]
>> Yeah, I mean, that's doing a lot. It's doing a lot of calculations there But, not having to do the render each time, pretty happy.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now