Skip to content

adi_screen tutorial 4: Transforms

Jeron Aldaron Lau edited this page May 18, 2017 · 1 revision

Tutorial #4: Transforms

Sprites don't do much on their own, but they can move around or change size with the help of Transforms.

Rotation transform

First go to the line that looks like this:

use adi_screen::{ Window, Input, Sprite, Style };

And add Transform:

use adi_screen::{ Window, Input, Sprite, Style, Transform };

Next, after this line:

	let disp2 = context.window.pulse_full_linear(2.0);

Add,

	Transform::create()
		.rotate(0.0, 0.0, disp2)
		.orthographic(&context.window)
		.apply(&mut context.window, &context.sprite, 0);

And try cargo run. If it looks kind of crazy, it worked. It's rotating all of the way around the Z axis (.rotate(0.0, 0.0, disp2)) and back once every 2 seconds. It's using an orthographic projection (.orthographic(&context.window)), which means that Z coordinates don't change how far away a point appears to be. The apply() function takes three parameters: mutable reference to window, immutable reference to a sprite, and which instance of the sprite ( we only have 1 instance, so it has to be 0 ).

Perspective

Replace,

	Transform::create()
		.rotate(0.0, 0.0, disp2)
		.orthographic(&context.window)
		.apply(&mut context.window, &context.sprite, 0);

With,

	Transform::create()
		.translate(0.0, 0.0, disp2)
		.perspective(&context.window, 90.0)
		.apply(&mut context.window, &context.sprite, 0);

This time we're using a transform to make it go farther away ( greater Z coordinate ). We are using a perspective projection instead of an orthographic projection this time with a 90 degree angle. 90 degrees will probably look the best.

If you want, you can change the perspective projection to orthographic, and see that the transform is apparently pointless in orthographic, and z translations need a perspective projection.