-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-falling-ball.lua
110 lines (83 loc) · 2.67 KB
/
03-falling-ball.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'util'
local scale = 1/2
local priority = 1
local size = { width = 640*scale, height = 960*scale }
Util:window('Falling Ball', size)
viewport = Util:viewport(size)
-- Make a layer and partition for the balls
local balls_layer = Util:layer(viewport, true)
local partition = MOAIPartition.new()
balls_layer:setPartition(partition)
-- Making the ball
local ball = Util:character({ file = 'images/soccer-ball.png', scale = 0.1*scale, name = "Football"})
partition:insertProp(ball)
-- Setup 2D World
local world = MOAIBox2DWorld.new ()
world:setGravity ( 0, -10)
world:setUnitsToMeters ( 1/50 )
world:start ()
balls_layer:setBox2DWorld ( world )
-- Setup Physics Box
body = world:addBody ( MOAIBox2DBody.DYNAMIC )
body:setAngularVelocity(2)
-- Box of the physics boundary
-- poly = {
-- 0, -75,
-- 75, 0,
-- 0, 75,
-- -75, 0,
-- }
-- fixture = body:addPolygon ( poly )
diameter = 600*ball.scale
fixture = body:addCircle( size.width/2-diameter, size.height/2-diameter, diameter )
fixture:setDensity ( 1 )
fixture:setFriction ( 0.3 )
fixture:setRestitution( 0.7 )
fixture:setFilter ( 0x01 )
fixture:setCollisionHandler ( onCollide, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END, 0x02 )
body:resetMassData ()
body:applyAngularImpulse ( 2 )
ball:setParent ( body )
ball:setLoc(size.width/2-diameter, size.height/2-diameter, diameter)
-- adding ground
-- local ground_layer = Util:layer(viewport, true)
-- local ground = Util:character({ name = "ground", scale = 0.7, file = "images/ground.png", width = 480, height = 86 })
-- ground_layer:insertProp(ground)
-- ground:setLoc(0,-size.height/2)
ground_body = world:addBody(MOAIBox2DBody.STATIC)
ground_fixture = ground_body:addRect(-size.width/2, -size.height/2,size.width/2+1,40-size.height/2)
ground_fixture:setFilter ( 0x02 )
ground_fixture:setLoc(0,-200)
-- ground:setParent(ground_body)
-- Input Manager
mouse = {}
function onClick(down)
if down then
pick = partition:propForPoint ( mouseX, mouseY )
if pick then
pick:setPriority ( priority )
priority = priority + 1
pick:moveScl ( 0.01, 0.01, 0.125, MOAIEaseType.EASE_IN )
print(pick.name)
end
else
if pick then
pick:moveScl ( -0.01, -0.01, 0.125, MOAIEaseType.EASE_IN )
pick:moveLoc (-10, 0, 1, MOAIEaseType.EASE_IN )
pick:moveRot (10, 1, MOAIEaseType.EASE_IN )
pick = nil
end
end
end
function onMove(x,y)
local oldX = mouseX
local oldY = mouseY
mouseX, mouseY = balls_layer:wndToWorld(x, y)
if pick then
pick:addLoc ( mouseX - oldX, mouseY - oldY )
end
end
if MOAIInputMgr.device.pointer then
MOAIInputMgr.device.pointer:setCallback(onMove)
MOAIInputMgr.device.mouseLeft:setCallback(onClick)
end