This is a tutorial on animating a three-dimensional object using the stack method.
Creating the Costumes
The first step in making an animated 3D object is to create the costumes. This is done by imagining one is slicing the object so that it is a stack of slices. For example, a cylinder, when sliced, would be a stack of circles. A cone would be a stack of circles with a decreasing diameter, and a sphere would be a stack of circles whose diameter first increases, then decreases. Each of these imaginary slices must then be drawn with a paint editor. For the sake of simplicity when programming, the recommended number of slices is 16.[citation needed]
Writing the Script
This project will require only one script and one sprite, thus it will be a One Sprite One Script project.
How it Works
The script will draw the 3D object by Stamping each slice on top of the other, slightly displaced. This will cause the illusion of a 3D object by reconstructing the object which was "sliced" in the above section. The displacement will be determined by calculating the horizontal and vertical offset with a given angle, using trigonometry. The script will loop forever, slightly incrementing the angle each repetition to give the illusion of rotation. The drawing script must be Single Frame to prevent rendering lag.
Part 1: Setting up the Stage
Firstly, the project must be set up. This includes:
The script is shown here:
when gf clicked forever go to x: (0) y: (0)//center sprite turn ccw (1) degrees//rotate sprite erase all// erase all change [angle v] by (1)//increment angle
Next, the offsets must be calculated with a certain amount of trigonometry:
when gf clicked forever go to x: (0) y: (0) turn ccw (1) degrees erase all change [angle v] by (1) set [x-offset v] to ([sin v] of (angle))//x offset set [y-offset v] to ([cos v] of (angle))//y offset
Part 2: Moving and Stamping
Next, the sprite must move and stamp itself, and change its costume accordingly. This is done with the following code:
when gf clicked forever go to x: (0) y: (0) turn ccw (1) degrees erase all change [angle v] by (1) set [x-offset v] to ([sin v] of (angle)) set [y-offset v] to ([cos v] of (angle)) repeat (16)//repeat for each of the 16 slices change x by (x-offset)//move change y by (y-offset)//move stamp//stamp next costume//change costume end
However, this runs extremely slowly. To speed it up, the code needs to "run without screen refresh".
- Make a new custom block. Make sure to select "run without screen refresh" in the bottom-left corner.
Warning: | This script will not work if you don't select "run without screen refresh." |
- Drag the code from inside the forever loop under the new define block.
- Last, drag the new custom block into the forever loop:
define 3D go to x: (0) y: (0) turn ccw (1) degrees erase all change [angle v] by (1) set [x-offset v] to ([sin v] of (angle)) set [y-offset v] to ([cos v] of (angle)) repeat (16)//repeat for each of the 16 slices change x by (x-offset)//move change y by (y-offset)//move stamp//stamp next costume//change costume end when gf clicked forever 3D end
Following the Mouse
This code will add a small effect where the object follows the mouse. Add the code below instead of the other code in the tutorial.
Note: | The custom block in this script needs to have run without screen refresh turned on as well. |
define Second 3D repeat (16) turn cw (3) degrees change x by ((0.05) * (mouse x)) change y by ((0.05) * (mouse y)) stamp next costume end when gf clicked forever erase all go to x: [0] y: [0] Second 3D end