Check for collisions between the falling stone and the bullet
The goal for this game i to shoot the falling stone. For that reason it is of course important to find out if they have collided. At a first glance this sounds easy, they will collide when their coordinates are the same. But the picture below show this is not a good solution: Clearly the bullet and stone will collide, but their x coordinates are not the same, and will never be. Therefore we have to check if the bullets x coordinates are within the falling stones x coordinate and the x coordinate plus its width. The stone is 9 pixels wide.
So lets start with the programming. We first have to create a new while loop:
We must fix the loop condition, add a local variable, set it to read and select game over. if you have done it correct the loop now looks like this: To check out for collisions we have to use the function “In Range and Coerce”: We find this in NXT Robotics/Numeric:
We insert this into the While loop:
The variable we want to check is Xbullet. We first insert a local variable. We find it in the menu NXT Programming/Structures:
We insert this in our program: We then configure it to Xbullet. press on the house and select Xbullet in the list: So we right-click Xbullet and select changer to read: So we wire this local variable to what is called x on the variable In range and coerce:
So we must insert upper and lower limit. Lower limit will be Xstone. We inserts a local variable which is in the menu NXT Programming/Structures:
We insert this one as shown below:
We configure it to Xstone and read. Now it should look like this: We wire this to the lower limit on the In range and coerce command: So we must fix the upper limit. This should be Xstone+9 as mentioned before. We first insert an add command. It is in the NXT Programming/Numeric menu:
We insert this: We have to add a local variable Xstone here also. Unfortunately it is not possible to use copy/paste here, so we must insert a new local variable and configure it. But that we have done a lot of times now? After this is done our program looks like this: We wire this to the add command: So we move the mouse pointer to the lower left of the add command. We right/click and select create constant…
In the constant we write the number 9. After that we wire the result of the add command to the upper limit on the in/range and coerce command. Now it should look like this: The command In-Range and coerce will now check if the bullets x coordinate lies in between the upper and lower value of the stones X coordinate. If it is is, the connector In-Range? will become true:
So when we have a collision the In Range connector becomes true. But, at the same time, the y coordinates must also be In Range. For that reason we must create a similar code for the y coordinates. But remember, we can not copy/paste the code, it is not possible for local variables- I guess you have become familiar with this now, so the code will look like this when you are finished:
But here you might see a problem. If there should be a collision, both Xstone and Ystone must be In Range at the same time. To fix this we must use the boolean operator AND, found in NXT Programming/Boolean:
We insert this command as shown below: Before we to the wiring lets explain the AND command. The help text is shown below (press CTRL+H and move the mouse pointer over the AND command):
We see this command has two inputs. If both of them are true, the output is true. If one is TRUE and the other is FALSE, the output is FALSE. Or more exactly, IF x AND y is TRUE THEN the output is TRUE. For instance, for the OR command, IF X OR Y is TRUE, then the output is TRUE. For the OR command only one of them must be true. So, what do we want to do when we get the result TRUE, meaning the stone and bullet has collided? The first thing is obvious, the player’s score must increase, for example by one point.
But another thing to be done is to stop the bullet and the stone. We can also add a sound here, a beep to indicate the collision. To get all this done we need a CASE structure found in NXT Programming/Structures:
So we can fix the score. Add two local variables and place them inside the TRUE part of the CASE structure. Select Score for both, one is set to read, the other to write as shown below:
So we insert a add command, and a constant with the value 1, and do the wiring as shown below:
We also want to stop the bullet and stone, but this must be done in separate loops. We can do this by adding a new indicator called hit?. It will get its value from the AND command. Move the mouse to the green question mark on the CASE structure, and be sure to be inside the CASE structure. Then right-click and select create indicator:
No it looks like this:
Change the text or name of this indicator to Hit?:
We now have an indicator to keep track of the bullet hitting the stone. And of course, since this is an indicator we can create local variables elsewhere in the program to read/write to this indicator. More on this when programming the movement of the stone.
Finally we must add the sound that indicates the collision. We insert the command Sound found in the menu NXT Robotics/NXT I/O:
We insert this into the CASE structure:
So let’s look at the help for the Sound command:
The duration is by default set to 500 ms. A better value is 50ms. We move the mouse-pointer over this connector, right-click and select create constant. So we set the value to 50 ms. Now it looks like this:
So it is good to wait a bit before continuing. We insert a wait command found in NXT robotics/NXT I/O:
We insert this behind the sound command:
I would recommend to wait for 100ms. Press the down arrow on the command and select Wait for msec:
So you can right-click the Time connector, select create constant, and set he constant to 100:
Finally we wire The Tone and msec command:
Congratulations, the loop for collisions are now fixed! Lets move on to moving the stone.
Svein-Tore Narvestad
Latest posts by Svein-Tore Narvestad (see all)
- Create apps for NXT with MIT App Inventor - 3 August 2016
- RicEditor tutorial: Create a cannon game - 28 October 2013
- Create your own games with “RIC” files - 27 August 2013
- How to update the NXT firmware in LabVIEW - 19 August 2013
- Datalogging in LabVIEW: Timed datalogging - 2 June 2013