Listing 1
1 on beginSprite
2 s = member( "HelloWorld" )
3 s.resetWorld()
4 hk = member( "Havok" )
5 hk.gravity = vector(0, -10, 0)
6 -- create ground
7 m = s.model("floor")
8 m.addModifier(#meshdeform)
9 hk.makeFixedRigidBody(m.name)
10 -- create text
11 m = s.model("hello")
12 m.addModifier(#meshdeform)
13 hk.makeMovableRigidBody(m.name, 100.0, false)
14 end
Listing 2
1 on enterFrame
2 checkkeys
3
4 boardSpeed = boardRB.linearVelocity.magnitude
5 boardDY = pPrevY - board.worldPosition.y
6 pPrevY = board.worldPosition.y
7
8 if boardDY<0 then
9 pWrongWayCount = pWrongWayCount + 1
10 else if pWrongWayCount>0 then
11 pWrongWayCount = pWrongWayCount - 1
12 end if
13
14 if pWrongWayCount>50 and pBoost>10000 then
15 pBoost = pBoost - 200
16 else if pBoost<40000 then
17 pBoost = pBoost + 200
18 end if
19
20 if leftkey then
21 boardRB.applyAngularImpulse(vector(0,pTurn,0))
22 end if
23 if rightkey then
24 boardRB.applyAngularImpulse(vector(0,-pTurn,0))
25 end if
26 if upkey and boardSpeed<7000 and pUpsideCount = 0 then
27 hitV = board.transform.zAxis * -pBoost
28 boardRB.applyImpulse(hitV)
29 end if
30 if downkey then
31 hitV = board.transform.zAxis * pBoost
32 boardRB.applyImpulse(hitV)
33 end if
34 if not spacekey then
35 dampenPhysics
36 end if
37 end
Listing 3
1 on dampenPhysics
2 -- check height from snow
3 boardHeight = 0
4 wpoint = board.worldPosition
5 wdown = -1.0 * board.transform.yAxis
6 idetails = w3d.modelsUnderRay(wpoint, wdown, 2, #detailed)
7 repeat with j = 1 to idetails.count
8 if idetails[j].model = landscape then
9 boardHeight = idetails[j].distance
10 exit repeat
11 end if
12 end repeat
13
14 -- reduce heading rotation
15 rbForce = boardRB.angularVelocity * -pRotationDamping
16 boardRB.applyAngularImpulse(rbForce)
17
18 -- only reduce slide when on or near the ground
19 if boardHeight<100 then
20 -- reduce sideways slide
21 rbForce = boardRB.linearVelocity
22 rbForceMag = rbForce.magnitude
23 rbForce.normalize()
24 forceXfactor = rbForce.dot(board.transform.xAxis)
25 forceXfactor = forceXfactor * board.transform.xAxis
26 xDampen = -rbForceMag * forceXfactor * pSlideDamping
27 boardRB.applyImpulse(xDampen)
28 end if
29
30 -- flip if upside down
31 tn = transform()
32 pUp = tn.yAxis
33 wUp = board.transform.yAxis
34 cosine = wUp.dot(pUp)
35
36 if cosine < 0.5 then
37 pUpsideCount = pUpsideCount + 1
38 else
39 pUpsideCount = 0
40 end if
41
42 if pUpsideCount>20 then
43 tn = transform()
44 tn.position = board.worldPosition
45 tn.position.y = tn.position.y + 50
46 boardRB.interpolatingMoveTo(tn.position, [vector(0,1,0), 0])
47 pUpsideCount = 0
48 end if
49
50 end