-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2dEntity.monkey
226 lines (172 loc) · 5.65 KB
/
box2dEntity.monkey
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'Set Strict Mode
Strict
'Import Box2d Modules
Import box2d.common
Import box2d.collision
Import box2d.dynamics
Import box2dPolygon
'Box2d Entity Class
Class Entity
'Fields
Field world:b2World
Field body:b2Body
Field bodyDef:b2BodyDef
Field bodyShape:b2PolygonShape
Field bodyShapeCircle:b2CircleShape
Field fixtureDef:b2FixtureDef
Field scale:Float
Field img:Image
Field frame:Int
Field imgOffsetX:Float
Field imgOffsetY:Float
Field colliding:Bool
'Creates a box shape for the entity
Method CreateBox:Void(world:b2World, x:Float, y:Float, width:Float, height:Float, scale:Float = 64.0, restitution:Float=0.3, density:Float = 1.0, friction:Float = 0.3, static:Bool = False, sleep:Bool = True)
'Set up box body
Self.fixtureDef = New b2FixtureDef()
Self.bodyShape = New b2PolygonShape()
Self.bodyDef = New b2BodyDef()
'Determine if box is static or not
If static = True
Self.bodyDef.type = b2Body.b2_staticBody
Else
Self.bodyDef.type = b2Body.b2_Body
endif
'Set box properties
Self.fixtureDef.density = density
Self.fixtureDef.friction = friction
Self.fixtureDef.restitution = restitution
Self.fixtureDef.shape = Self.bodyShape
'Set box position
Self.bodyDef.position.Set(x, y)
'Set box scale
Self.scale = scale
'Set box state
Self.bodyDef.allowSleep = sleep
Self.bodyDef.awake = True
'Set box world
Self.world = world
'Set shape as box
Self.bodyShape.SetAsBox(width, height)
'Create box
Self.body = Self.world.CreateBody(Self.bodyDef)
Self.body.CreateFixture(Self.fixtureDef)
End
'Creates a circle shape for the entity
Method CreateCircle:Void(world:b2World, x:Float, y:Float, radius:Float, scale:Float = 64.0, restitution:Float=0.2, density:Float = 1.0, friction:Float = 0.3, static:Bool = False, sleep:Bool = True)
'Set up the circle body
Self.fixtureDef = New b2FixtureDef()
Self.bodyShapeCircle = New b2CircleShape(radius)
Self.bodyDef = New b2BodyDef()
'Determine if circle is static or not
If static = True
Self.bodyDef.type = b2Body.b2_staticBody
Else
Self.bodyDef.type = b2Body.b2_Body
endif
'Set circle properties
Self.fixtureDef.density = density
Self.fixtureDef.friction = friction
Self.fixtureDef.restitution = restitution
Self.fixtureDef.shape = Self.bodyShapeCircle
'Set cirlce position
Self.bodyDef.position.Set(x, y)
'Set circle scale
Self.scale = scale
'Set circle state
Self.bodyDef.allowSleep = sleep
Self.bodyDef.awake = True
'Set circle world
Self.world = world
'Create circle
Self.body = world.CreateBody(Self.bodyDef)
Self.body.CreateFixture(Self.fixtureDef)
End Method
'Creates a multi-polygon shape for the entity
Method CreateMultiPolygon:Void(world:b2World, x:Float, y:Float, polygonList:List<Polygon>, scale:Float = 64.0, restitution:Float=0.3, density:Float = 1.0, friction:Float = 0.3, static:Bool = False, sleep:Bool = True)
'Set up polygon body
Self.bodyDef = New b2BodyDef()
'Determine if box is static or not
If static = True
Self.bodyDef.type = b2Body.b2_staticBody
Else
Self.bodyDef.type = b2Body.b2_Body
Endif
'Set position of body
Self.bodyDef.position.Set(x, y)
'Create initial body
Self.body = world.CreateBody(Self.bodyDef)
'Create a fixture for each polygon shape
For Local p:Polygon = Eachin polygonList
'Scale Vertices
For Local v:Int = 0 To (p.count - 1)
p.vertices[v].x = (p.vertices[v].x/scale)
p.vertices[v].y = (p.vertices[v].y/scale)
Next
Self.bodyShape = New b2PolygonShape()
Self.bodyShape.SetAsArray(p.vertices, p.count)
Self.fixtureDef = New b2FixtureDef()
Self.fixtureDef.shape = Self.bodyShape
Self.fixtureDef.density = density
Self.fixtureDef.friction = friction
Self.fixtureDef.restitution = restitution
Self.bodyDef.position.Set(x, y)
Self.body.CreateFixture(Self.fixtureDef)
Next
'Set polygon body properties
Self.bodyDef.position.Set(x, y)
Self.world = world
Self.bodyDef.allowSleep = sleep
Self.bodyDef.awake = True
Self.scale = scale
End
'Draw the entity
Method Draw:Void(offsetX:Float = 0.0, offsetY:Float=0.0)
'If entity has an image attached then draw it
If Self.img <> Null
'Determine Image Location
Local x:Float = Self.GetPositionX()
Local y:Float = Self.GetPositionY()
Local r:Float = RadToDeg(Self.body.GetAngle()) * -1
'Draw Image
DrawImage(Self.img, x, y, r, 1.0, 1.0, Self.frame)
Endif
End
'Converts Radians to Degrees
Method RadToDeg:Int(rad:Float)
If rad <> 0.0 Then Return (rad * 180.0) / PI Else Return 0
End Method
'Converts Degrees to Radians
Method DegToRad:Int(deg:Float)
If deg <> 0.0 Then Return (deg * PI) / 180.0 Else Return 0
End Method
'Applies Impluse to body
Method ApplyImpulse:Void(impulse:b2Vec2, point:b2Vec2)
Self.body.ApplyImpulse(impulse, point)
End Method
'Returns the position of body (Position is scaled to physics!!)
Method GetPosition:b2Vec2()
Return Self.body.GetPosition()
End Method
'Returns scaled X position (Position is scaled up)
Method GetPositionX:Float()
Return (Self.GetPosition().x * Self.scale)
End Method
'Returns scaled Y position (Position is scaled up)
Method GetPositionY:Float()
Return (Self.GetPosition().y * Self.scale)
End Method
'Sets the position of body
Method SetPosition:Void(x:Float, y:Float)
Self.body.SetPosition(New b2Vec2((x/Self.scale), (y/Self.scale)))
End Method
'Sets an image for the entity
Method SetImage:Void(img:Image)
Self.img = img
End
'Clears forces from body
Method ClearForces:Void()
Self.body.SetLinearVelocity(New b2Vec2(0.0, 0.0))
Self.body.SetAngularVelocity(0.0)
End Method
End Class