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 ) --創(chuàng)建 圓球
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 --創(chuàng)建 地面,并且設置 屬性
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 --創(chuàng)建 四面墻體
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 --創(chuàng)建樓梯
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 --循環(huán),輸出每一節(jié)樓梯
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 --創(chuàng)建攝像機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 --創(chuàng)建燈光
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.count) then
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.count) then
146 me.checkForCollision(tList[1])
147 end if
148
149 --以 負x為 中心軸
150 tList = scene.modelsUnderRay( mySphere.worldPosition,-mySphere.transform.xAxis,#detailed )
151 if (tList.count) then
152 me.checkForCollision(tList[1])
153 end if
154
155 go the frame
156 end exitFrame
157
158 -- 發(fā)生 與 墻壁、樓梯 碰撞 處理方法
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(126) or keyPressed("w") then --top 前進
190 mySphere.translate(0,20,0) --移動
191 end if
192
193 if keyPressed(125) or 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(123) then --left
206 mySphere.rotate(0,0,2) --旋轉
207 end if
208
209 if keyPressed(124) then
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