Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Function: distance to (http://www.alice.org/community/showthread.php?t=1953)

 runcmd 11-25-2008 03:33 PM

Function: distance to

1 Attachment(s)
Good day,
I'm currently making a project and I'm looking to make the car, which will be commanded by the arrow keys, stop when it gets to a certain distance from an object, in this case, a building.

I think the most viable way to do this is to use an IF statement:
if
car distance to building < 1
stop car

But when trying to manipulate the car's functions I see myself troubled because I can't drag the items to a method. Also, I don't understand the IF statements under Alice.

Any orientation on how to use these functions under IF statements will be very appreciated.

Thank you.

1 Attachment(s)
The reason you cannot drag that function into the method by itself is because it is only used to calculate the distance of the car to another object. Move car forward {1} meters, for example, is a method that you can use the "Car distance to" function in-by draging said function to the {1} part in the Move car forward method. Its useful in an instance that you want to know the exact distance to something no matter where the two objects involved end up while the world is playing.

To achieve the affect you want, you will have to create new methods to make the car move. To stop the car when it reaches the gazebo, you were on the right track with the If/Else statement.

Note* If you're going for a collision effect, there is a different way to do this

Make the left, right, and backward movements as new methods of the car, each made with the standard "Car move" and "Car turn" methods. But for the forward method, use "Car move at speed", found close to the bottom of the list. Select any speed for the forward movement speed for the moment, because thats about to be changed. Click on the Properties tab under the Car's details and click Create a new Variable. Name this property something appropriate, like "speed" and select the bullet next to Number in the list below the name. The value should be set to 1 by default(you can change this number later if you want) and click OK. Go back to the Car's forward method you made earlier, and, from the cars Properties tag, drag the speed variable at the top of the list to the "Speed= meters per second" part of the "Car move at speed method."

Next, go back to the "world.myfirst method" and drag a Loop statement there. Open the loop and drag in an If/Else statement. Bring up the Car's Functions tab and drag "Car is within threshold of object" to the part of the If/Else statement that says True (or False, depending on what you selected when you dragged in the If/Else Statement) and select 5 meters of Gazebo. Go back to the Car's Properties tab and drag speed under the IF part of the If/else statement. Set the car.speed to 0. Do the same thing for the ELSE part of the statement, except set the car.speed to 1.

Lastly Create New Events for the cars directional control. Select "When a key is typed" from the "Create new event" button and then right-click on the new event in the list. Change it to "While a key is pressed". Set the key pressed to the Up arrow key and set the During part of that event to the car.forward method. Repeat for the left, right and backward methods of the car. Make sure that there is also a "When the world starts do {world.myfirst method}" event in that list. That should do it.

Just for kicks, I've included a completed example of the steps above. I hope this helps.

 All times are GMT -5. The time now is 05:13 AM.