Balder 3D开发系列之——sprite结合Camera旋转木马特效

一、前言

 

在前一篇关于sprite的文章中,我们介绍了它的基本情况和基本用法,在本文中,我们将在前文的基础上,使用sprite实现一个小效果。这个效果就是旋转木马的特效,之前我也写过一篇文章,那是使用椭圆方程来实现的。

 

 

二、基本原理

 

1.通过使用sprite的Position,在屏幕中适当放置几个sprite。

2.使用一个DispatcherTimer来定时的改变Camera的位置。

通过以上两步就实现了旋转木马的效果,原理相信大家都明白,从不同角度去观察sprite而造成选择的视觉感觉。当然,你也可以构造更复杂的Camera运动路径来实现更多的效果。

 

三、实现过程

在前面一篇关于sprite的文章中,我们全部是使用xaml来进行的,这次就换一下吧,我们来用C#代码来实现整个过程。

 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 

 

 

最后看下效果如何:

 

转载于:https://www.cnblogs.com/vimsk/archive/2011/01/16/1936637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值