Functions and Expressions

Objects in 3D Space

Objects in Alice have a centre point.

Pasted image 20230214135931.png|300

That centre point is the intersection of three axes:

For people, this centre point is often positioned at the bottom of the model:

Pasted image 20230214140415.png|300

Distances between objects are measured between their centre points:

Pasted image 20230214140546.png

Exercise: Shrine Time

With that in mind, consider the following scene:

Pasted image 20230214140638.png

What if we wanted the person to move over to the shrine, but not go inside?

Consider this carefully: remember our discussion earlier this year about algorithms.

Instructions must be precise – computers are not able to “guess” at what we want them to do.

Todo

If you are present in class, pair up to your randomly assigned partner.

If you are reading this outside of class, pause and make a point form plan before continuing to read.

Thinking about the centre points of objects, what are the steps involved in getting the person over to the shrine, but not having them go inside?

Discussion: An initial plan

After some thought, you may written something like the following:

If your plan looks like this – it is not detailed enough! 😅

Remember, objects in a 3D space like Alice have distances between them measured from their centre points:

Pasted image 20230214140546.png

Discussion: A more detailed plan

A revised plan might look like this:

Find edge of shrine by:

Let's program that together now.

You can download the starter world here.

Note

If you are reading this outside of class time, you can watch this short video to see how this mini-task was completed.

In completing this task, we made use of two new ideas: functions and expressions.

Functions

In Alice, a function is used to get information we need about:

When a function is used to ask a question or perform a computation, an answer is returned.

This answer is called a value.

The type of the value depends on what the function does for us.

In the example we completed earlier, we expected to get a numeric value because the function retrieved a distance.

That distance could be a whole number, such as 3 meters.

It could also be a fractional value, such as 1.2 meters.

Other value types include:

Expressions

An expression is a math or logical operation on numbers or other types of values.

Alice provides math operators for common mathematical expressions:

We can combine functions and expressions to do calculations that are responsive to the location of objects in a scene.

Return to Alice now and move the shrine to a new position, something like what is shown in the animation below:

Moving the Shrine.gif

Run the program again. Did the person still go to the edge of the shrine?

This works because we used a procedure to turn toward the shrine – whereever it happens to be in the world:

Screenshot 2023-02-14 at 3.06.17 PM.png|300

Additionally, the person always moves the correct distance because we used a function within an expression to find that distance:

Screenshot 2023-02-14 at 3.09.45 PM.png

Try moving the shrine a few more times.

Every time you run the program, you should find the person moves to the edge of the shrine, but doesn't go inside.

Exercise: Lost and Found

Oh dear! You have lost your AirPods case... again. 😭

Pasted image 20230214151500.png

Download this Lost and Found world and open it in Alice.

Run the world a few times. What do you notice about the position of the objects?

Now, add a person to the world.

Make your person:

Repeat this process for each object in the room.

Tip

There is an origin object marker that you can use to find the middle of the room in this scene.

Note

Using the clipboard in Alice might be helpful once you get your person doing the correct sequence of steps with a single object.

Here is a 20-second video on how to use the clipboard in Alice.