悟心

成功不是将来才有的,而是从决定去做的那一刻起,持续累积而成。 上人生的旅途罢。前途很远,也很暗。然而不要怕。不怕的人的面前才有路。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  93 随笔 :: 1 文章 :: 103 评论 :: 0 Trackbacks
  1 ------------ 变量申明 --------------------------
  2 property mySphere  --代理球
  3 property mySprite  --当前精灵
  4 property pCam   --摄像机1
  5 property pCam2  --摄像机2
  6 property scene  --场景
  7 property pSphere --球体
  8 property ground  --地面
  9 
 10 property wall  --墙壁
 11 property wall01  --墙壁1
 12 property wall02  --墙壁2
 13 property wall03  --墙壁3
 14 property wall04  --墙壁4
 15 
 16 property stair  --楼梯
 17 
 18 -------------方法、事件----------------------
 19 on beginSprite me
 20   
 21   scene = member("3d")  --设置 场景=3d 成员 
 22   scene.resetWorld()   --重置
 23   mySprite = sprite(me.spriteNum)  --设置精灵
 24   put mySprite
 25   pSphere = scene.newModelResource( "pSphere",#sphere )  --创建 圆球
 26   pSphere.radius = 50  --设置 圆球 半径
 27   
 28   mySphere = scene.newModel( "pSphere",pSphere )  --将 圆球 加到 3D场景 中
 29   mySphere.translate(0,-1500,50)  --设置 球 的位置
 30   --mySp.rotate(10,0,0)
 31   
 32   --创建 地面,并且设置 属性
 33   ground = scene.newModelResource("ground",#plane)
 34   ground.length = 5000
 35   ground.width = 5000
 36   ground.widthVertices = 10
 37   ground.lengthVertices = 10
 38   
 39   scene.newModel("ground",ground)  --加到 3D场景中
 40   --设置 地面 的贴图 的比例
 41   scene.shader("defaultShader").textureTransform.scale(0.1,0.1,1)
 42   
 43   --创建 四面墙体
 44   wall = scene.newModelResource( "wall",#plane,#front )
 45   wall.length = 200
 46   wall.width = 5000
 47   
 48   --添加到 3D场景 中
 49   wall01 = scene.newModel("wall01",wall)
 50   wall02 = scene.newModel("wall02",wall)
 51   wall03 = scene.newModel("wall03",wall)
 52   wall04 = scene.newModel("wall04",wall)
 53   
 54   --设置4面墙的位置
 55   wall01.translate(0,-2500,100,#world)
 56   wall01.rotate(90,0,0)
 57   
 58   wall02.translate(0,2500,100,#world)
 59   wall02.rotate(-90,0,0)
 60   
 61   wall03.translate(2500,0,100,#world)
 62   wall03.rotate(90,0,90)
 63   
 64   wall04.translate(-2500,0,100,#world)
 65   wall04.rotate(90,0,-90)
 66   
 67   --创建楼梯
 68   stair = scene.newModelResource("stair",#box)
 69   stair.height = 60
 70   stair.width = 300
 71   stair.length = 30
 72   
 73   scene.newModel("stair",stair)
 74   scene.model("stair").translate(0,0,0)
 75   
 76   --循环,输出每一节楼梯
 77   repeat with i=1 to 20
 78     stairI=scene.newModel( "b0"&string(i),stair )
 79     stairI.translate(0,60*i,30*i)
 80   end repeat
 81   
 82   --创建摄像机1
 83   pCam = scene.newCamera("cam01")
 84   pCam.transform.position = mySphere.transform.position
 85   pCam.rotate(90,0,0)
 86   pCam.translate(0,50,0)
 87   
 88   --摄像机2
 89   pCam2= scene.newCamera("cam02")
 90   pCam2.transform.position = mySphere.transform.position
 91   pCam2.translate(0,-2000,2000)
 92   pCam2.rotate(45,0,0)
 93   
 94   mySprite.camera=pCam  --设置当前默认相机
 95   --mySprite.camera=pCam2
 96   
 97   --将相机绑定到 球体 中
 98   mySphere.addChild(pCam)
 99   mySphere.addChild(pCam2)
100   
101   --创建灯光
102   lightx = scene.newLight("lightx",#point)
103   
104   lightx.translate(2000,1500,1000)
105   mySphere.translate(0,0,50)   --设置球的位置
106   
107 end beginSprite
108 -------------------------------------------------------------------
109 on exitFrame me
110   --简单重力
111   mySphere.translate(0,0,-50)
112   
113   myKeyPressed  --调用键盘按下事件
114   
115   ---模拟碰撞测试程序------
116   -- modelsUnderRay 函数将返回关于射线经过模型的一些数据。
117   --碰撞处理程序将对这些数据进行分析。
118   
119   --以 负z 为中心轴
120   tList = scene.modelsUnderRay( mySphere.worldPosition,-mySphere.transform.zAxis,#detailed )
121   if tList.count then
122     me.checkGroundCollsision(tList[1])  --检查对 地面 碰撞
123   end if
124   
125   --以 正z 为中心轴
126   tList = scene.modelsUnderRay( mySphere.worldPosition,mySphere.transform.zAxis,#detailed )
127   if (tList.countthen
128     me.checkForCollision(tList[1])
129   end if   
130   
131   --以 正y 为中心轴 
132   tList = scene.modelsUnderRay( mySphere.worldPosition,mySphere.transform.yAxis,#detailed )
133   if tList.count then
134     me.checkForCollision(tList[1])
135   end if
136   
137   --以 负y 为中心轴
138   tList = scene.modelsUnderRay( mySphere.worldPosition,-mySphere.transform.yAxis,#detailed )
139   if tList.count then
140     me.checkForCollision(tList[1])
141   end if
142   
143   --以 正x 为中心轴
144   tList = scene.modelsUnderRay( mySphere.worldPosition,mySphere.transform.xAxis,#detailed )  
145   if (tList.countthen
146     me.checkForCollision(tList[1])
147   end if  
148   
149   --以 负x为 中心轴
150   tList = scene.modelsUnderRay( mySphere.worldPosition,-mySphere.transform.xAxis,#detailed )  
151   if (tList.countthen
152     me.checkForCollision(tList[1])
153   end if 
154   
155   go the frame
156 end exitFrame
157 
158 -- 发生 与 墙壁、楼梯 碰撞  处理方法 
159 on checkForCollision (me,thisData)
160   
161   thisDistance = thisData.distance  --距离、间距
162   
163   mySphereRadius = mySphere.resource.radius+5  --圆球半径+5
164   
165   --如果当前 距离 小于 球体的半径+25
166   if thisDistance < mySphereRadius then
167     
168     tVector = thisData.isectNormal * (mySphereRadius-thisDistance)
169     
170     mySphere.translate(tVector,#world)  --移动 圆球
171   end if
172 end checkForCllision
173 
174 --与 地面 碰撞处理,防止“下沉”
175 on checkGroundCollsision(me,thisData)
176   
177   thisDistance = thisData.distance
178   mySphereRadius = mySphere.resource.radius+70
179   
180   if thisDistance < mySphereRadius then
181     tVector = thisData.isectNormal * ( mySphereRadius-thisDistance )
182     mySphere.translate( tVector,#world )
183   end if
184   
185 end checkGroundCollsision
186 
187 --------键盘按下事件--------------
188 on myKeyPressed 
189   if keyPressed(126or keyPressed("w") then  --top 前进
190     mySphere.translate(0,20,0)  --移动
191   end if
192   
193   if keyPressed(125or keyPressed("s") then  --botton 后退
194     mySphere.translate(0,-20,0)  --移动
195   end if
196   
197   if keyPressed("a") then  --
198     mySphere.translate(-10,0,0)  --移动
199   end if
200   
201   if keyPressed("d") then  --
202     mySphere.translate(10,0,0)  --移动
203   end if
204   
205   if keyPressed(123then  --left
206     mySphere.rotate(0,0,2)  --旋转
207   end if
208   
209   if keyPressed(124then
210     mySphere.rotate(0,0,-2)  --旋转
211   end if
212   
213   if keyPressed("1") then
214     mySprite.camera = pCam  --如果是按1,切换到1号视角
215   end if
216   
217   if keyPressed("2") then
218     mySprite.camera = pCam2  --切换2号视角
219   end if
220 end myKeyPressed
221 
posted on 2009-11-27 12:30 艾波 阅读(332) 评论(0)  编辑  收藏 所属分类: Other

只有注册用户登录后才能发表评论。


网站导航: