RicEditor tutorial: Create a cannon game

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: check_for_collision 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.

stone_pixels So lets start with the programming. We first have to create a new while loop:
While_loop_collision

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: add_game_over_local_varTo check out for collisions we have to use the function “In Range and Coerce”: helpinrangeandcoerce We find this in NXT Robotics/Numeric:
Numeric menu

We insert this into the While loop:
insert_coerce_in_while

The variable we want to check is Xbullet. We first insert a local variable. We find it in the menu NXT Programming/Structures:

localvariable menu We insert this in our program: insert local variable We then configure it to Xbullet. press on the house and select Xbullet in the list: select_XBullet So we right-click Xbullet and select changer to read: Xbullet_Change_to_read So we wire this local variable to what is called x on the variable In range and coerce: xbullet_wired_to_in-range

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:

 

localvariable menu We insert this one as shown below:

localvariableadded2 We configure it to Xstone and read. Now it should look like this: localvariableadded3 We wire this to the lower limit on the In range and coerce command: lowerlimitOK 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:

 

NumericAdd We insert this: plusAdded 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: XstoneUpper We wire this to the add command: wiretoadd So we move the mouse pointer to the lower left of the add command. We right/click and select create constant…creatconstanttoadd

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: in-range-coercefixed 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: helpinrangeandcoerce

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:

Inrage_Ybullet

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:

AND_menu

We insert this command as shown below: insertAND 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):

ANDHelp

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:

CaseMenu We insert this:

CaseAdded So we must finish the wiring:

 

finishwiring

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:

Addscore_in_CASE

So we insert a add command, and a constant with the value 1, and do the wiring as shown below:

scorefixed

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:

createindicatorHIT

No it looks like this:

booleaninserted

Change the text or name of this indicator to Hit?:

HittextfixedWe 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:

sound_In_menu

We insert this into the CASE structure:

Sound_In_Case

So let’s look at the help for the Sound command:

Tone_help

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:

duration50ms

So it is good to wait a bit before continuing. We insert a wait command found in NXT robotics/NXT I/O:

 

Wait_In_menu

 

We insert this behind the sound command:

wati_inserted

I would recommend to wait for 100ms. Press the down arrow on the command and select Wait for msec:

 

Waitformsec

So you can right-click the Time connector, select create constant, and set he constant to 100:

wait_100msec

Finally we wire The Tone and msec command:

Wire_Tone_and_Wait

Congratulations, the loop for collisions are now fixed! Lets move on to moving the stone.

The following two tabs change content below.
Svein-Tore Narvestad

Svein-Tore Narvestad

I have used LEGO Mindstorms as a teacher since 1999, and have used it widely. For example, to measure noise in kindergartens, to have the students create lemonade blenders using small pumps connected to the NXT, and in process control with industrial equipment: pneumatics, industrial sensors, etc.