在前一篇关于sprite的文章中,咱们介绍了它的基本状况和基本用法,在本文中,咱们将在前文的基础上,使用sprite实现一个小效果。这个效果就是旋转木马的特效,以前我也写过一篇文章,那是使用椭圆方程来实现的。app
经过以上两步就实现了旋转木马的效果,原理相信你们都明白,从不一样角度去观察sprite而形成选择的视觉感受。固然,你也能够构造更复杂的Camera运动路径来实现更多的效果。blog
1
namespace
BalderSpriteDemo
2
{
3
public
partial
class
MainPage : UserControl
4
{
5
Camera ca;
6
public
MainPage()
7
{
8
InitializeComponent();
9
DispatcherTimer timer
=
new
DispatcherTimer();
10
timer.Interval
=
TimeSpan.FromMilliseconds(
20
);
11
timer.Tick
+=
new
EventHandler(timer_Tick);
12
timer.Start();
13
Game game
=
new
Game()
//
构造一个640X480大小的场景
14
{
15
Width
=
640
,
16
Height
=
480
17
};
18
ca
=
new
Camera()
//
定义一个摄像机
19
{
20
Position
=
new
Balder.Math.Coordinate(
0
,
50
,
0
),
//
位置
21
Target
=
new
Balder.Math.Coordinate(
0
,
0
,
0
)
//
目标位置
22
};
23
24
game.Camera
=
ca;
25
OmniLight oml
=
new
OmniLight()
//
灯光
26
{
27
Position
=
new
Balder.Math.Coordinate(
-
100
,
100
,
0
)
28
};
29
Sprite sp1
=
new
Sprite()
//
初始化sprite
30
{
31
AssetName
=
new
Uri(
@"
/BalderSpriteDemo;component/Asset/man.png
"
, UriKind.Relative),
32
Position
=
new
Balder.Math.Coordinate(
-
30
,
0
,
0
)
33
};
34
Sprite sp2
=
new
Sprite()
35
{
36
AssetName
=
new
Uri(
@"
/BalderSpriteDemo;component/Asset/man.png
"
, UriKind.Relative),
37
Position
=
new
Balder.Math.Coordinate(
30
,
0
,
0
)
38
};
39
Sprite sp3
=
new
Sprite()
40
{
41
AssetName
=
new
Uri(
@"
/BalderSpriteDemo;component/Asset/man.png
"
, UriKind.Relative),
42
Position
=
new
Balder.Math.Coordinate(
0
,
0
,
-
30
)
43
};
44
Sprite sp4
=
new
Sprite()
45
{
46
AssetName
=
new
Uri(
@"
/BalderSpriteDemo;component/Asset/man.png
"
, UriKind.Relative),
47
Position
=
new
Balder.Math.Coordinate(
0
,
0
,
30
)
48
};
49
Box box
=
new
Box() {
50
Dimension
=
new
Balder.Math.Coordinate(
20
,
20
,
20
)
51
};
52
game.Children.Add(oml);
53
game.Children.Add(sp1);
54
game.Children.Add(sp2);
55
game.Children.Add(sp3);
56
game.Children.Add(sp4);
57
game.Children.Add(box);
58
LayoutRoot.Children.Add(game);
59
60
}
61
private
double
_sin;
62
void
timer_Tick(
object
sender, EventArgs e)
63
{
64
var x
=
System.Math.Cos(_sin)
*
130d;
//
构造摄像机坐标变换的路径
65
66
var z
=
System.Math.Sin(_sin)
*
130d;
67
var y
=
System.Math.Sin(_sin
*
2
)
*
30d;
68
69
70
ca.Position.X
=
x;
71
ca.Position.Z
=
z;
72
ca.Position.Y
=
y;
73
_sin
+=
0.015d
;
74
75
}
76
}
77
}
78