/
tank.go
332 lines (299 loc) · 9 KB
/
tank.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package other
import (
"github.com/g3n/engine/core"
"github.com/g3n/engine/geometry"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/gui"
"github.com/g3n/engine/light"
"github.com/g3n/engine/material"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/texture"
"github.com/g3n/engine/window"
"github.com/g3n/g3nd/app"
"github.com/g3n/g3nd/demos"
)
func init() {
demos.Map["other.tank"] = &TankTest{}
}
type TankTest struct {
a *app.App
velocity float32 // linear velocity (m/s)
rotvel float32 // rotation velocity (rad/s)
model *TankModel // tank model
commands [CMD_LAST]bool // commands states
}
const (
CMD_FORWARD = iota
CMD_BACKWARD
CMD_LEFT
CMD_RIGHT
CMD_CANNON_LEFT
CMD_CANNON_RIGHT
CMD_CANNON_UP
CMD_CANNON_DOWN
CMD_LAST
)
func (t *TankTest) Initialize(a *app.App) {
t.a = a
a.Orbit().EnableKeys = false
// Add directional white light
l1 := light.NewDirectional(&math32.Color{1, 1, 1}, 1.0)
l1.SetPosition(10, 10, 10)
a.Scene().Add(l1)
// Show grid helper
grid := graphic.NewGridHelper(100, 1, &math32.Color{0.4, 0.4, 0.4})
a.Scene().Add(grid)
// Sets camera position
a.Camera().GetCamera().SetPosition(0, 4, 10)
a.Camera().GetCamera().LookAt(&math32.Vector3{0,0,0})
// Add help label
label1 := gui.NewLabel("Use ASDW to drive tank\nUse JKLI to move cannon")
label1.SetFontSize(16)
label1.SetPosition(10, 10)
a.GuiPanel().Add(label1)
// Creates tank model
t.model = t.newTankModel()
t.velocity = 5.0
t.rotvel = 0.8
a.Scene().Add(t.model.node)
// Subscribe to key events
a.Window().Subscribe(window.OnKeyDown, t.onKey)
a.Window().Subscribe(window.OnKeyUp, t.onKey)
}
func (t *TankTest) Render(a *app.App) {
if t.commands[CMD_LEFT] || t.commands[CMD_RIGHT] {
// Calculates angle delta to rotate
angle := t.rotvel * t.a.FrameDeltaSeconds()
if t.commands[CMD_RIGHT] {
angle = -angle
}
t.model.node.RotateY(angle)
// Rotate the wheel caps
for i := 0; i < len(t.model.caps); i++ {
wcap := t.model.caps[i]
if i%2 == 0 {
wcap.RotateZ(3 * angle)
} else {
wcap.RotateZ(-3 * angle)
}
}
}
if t.commands[CMD_FORWARD] || t.commands[CMD_BACKWARD] {
// Calculates the distance to move
dist := t.velocity * float32(t.a.FrameDeltaSeconds())
// Calculates wheel rotation
var rot = -dist / 0.5
// Get tank world direction
var quat math32.Quaternion
t.model.node.WorldQuaternion(&quat)
direction := math32.Vector3{1, 0, 0}
direction.ApplyQuaternion(&quat)
direction.Normalize()
direction.MultiplyScalar(dist)
if t.commands[CMD_BACKWARD] {
direction.Negate()
rot = -rot
}
// Get tank world position
var position math32.Vector3
t.model.node.WorldPosition(&position)
position.Add(&direction)
t.model.node.SetPositionVec(&position)
// Rotate whell caps
for _, wcap := range t.model.caps {
wcap.RotateZ(rot)
}
}
if t.commands[CMD_CANNON_LEFT] {
t.model.nodeTop.RotateY(0.01)
}
if t.commands[CMD_CANNON_RIGHT] {
t.model.nodeTop.RotateY(-0.01)
}
if t.commands[CMD_CANNON_UP] || t.commands[CMD_CANNON_DOWN] {
// Get cannon world direction
var quat math32.Quaternion
t.model.meshCannon.WorldQuaternion(&quat)
direction := math32.Vector3{1, 0, 0}
direction.ApplyQuaternion(&quat)
direction.Normalize()
// Calculates angle with Y vector
cosElevation := direction.Dot(&math32.Vector3{0, 1, 0})
elevation := math32.Acos(cosElevation)
if t.commands[CMD_CANNON_UP] {
if elevation <= math32.Pi/4 {
return
}
t.model.meshCannon.RotateZ(0.01)
} else {
if elevation >= math32.Pi/2-0.01 {
return
}
t.model.meshCannon.RotateZ(-0.01)
}
}
}
// Process key events
func (t *TankTest) onKey(evname string, ev interface{}) {
var state bool
if evname == window.OnKeyDown {
state = true
} else {
state = false
}
kev := ev.(*window.KeyEvent)
switch kev.Keycode {
case window.KeyW:
t.commands[CMD_FORWARD] = state
case window.KeyS:
t.commands[CMD_BACKWARD] = state
case window.KeyA:
t.commands[CMD_LEFT] = state
case window.KeyD:
t.commands[CMD_RIGHT] = state
case window.KeyJ:
t.commands[CMD_CANNON_LEFT] = state
case window.KeyL:
t.commands[CMD_CANNON_RIGHT] = state
case window.KeyI:
t.commands[CMD_CANNON_UP] = state
case window.KeyK:
t.commands[CMD_CANNON_DOWN] = state
}
}
type TankModel struct {
node *core.Node // node with all tank meshes
meshBase *graphic.Mesh
caps []*graphic.Mesh
nodeTop *core.Node
meshCannon *graphic.Mesh
}
// Builds and returns a new tank model with separate meshes
// for the wheel caps, base, turret and cannon
func (t *TankTest) newTankModel() *TankModel {
const EPS = 0.01
const BASE_WIDTH = 3.0
const BASE_HEIGHT = 0.6
const BASE_LENGTH = 4.4
var BASE_COLOR = math32.NewColorHex(0x10c010)
const WHEEL_RADIUS = 0.5
const WHEEL_WIDTH = (BASE_WIDTH / 4) * 0.9
var WHEEL_COLOR = math32.NewColorHex(0x808080)
const EMBED = 0.5
const CAP_RADIUS = WHEEL_RADIUS - 0.1
const CAP_DZ = 0.01
var CAP_COLOR = math32.NewColorHex(0x707070)
const TRACK_HEIGHT = 0.05
var TRACK_COLOR = WHEEL_COLOR
const TUR_TOP_RADIUS = 1
const TUR_BOTTOM_RADIUS = BASE_WIDTH / 2
const TUR_HEIGHT = 1.0
var TUR_COLOR = math32.NewColorHex(0x10f010)
const CANNON_TOP_RADIUS = 0.2
const CANNON_BOTTOM_RADIUS = 0.3
const CANNON_LENGTH = 3
var CANNON_COLOR = TUR_COLOR
var CANNON_TOP_COLOR = math32.NewColorHex(0x102010)
// Creates tank model
model := new(TankModel)
model.node = core.NewNode()
// Loads tank wheel texture
texfile := t.a.DirData() + "/images/wheel.png"
tex, err := texture.NewTexture2DFromImage(texfile)
if err != nil {
t.a.Log().Fatal("Error:%s loading texture:%s", err, texfile)
}
// Create the tank wheels and add them to the group
matWheel := material.NewPhong(WHEEL_COLOR)
matCap := material.NewPhong(CAP_COLOR)
matCap.AddTexture(tex)
//defer matWheel.Dispose()
//defer matCap.Dispose()
var zWheel float32 = BASE_WIDTH/2 - WHEEL_WIDTH/2 - 0.1
for i := 0; i < 4; i++ {
for j := 0; j < 2; j++ {
// Creates wheel mesh
geomWheel := geometry.NewCylinder(WHEEL_RADIUS, WHEEL_RADIUS, WHEEL_WIDTH+EPS, 20, 20, 0, 2*math32.Pi, true, true)
meshWheel := graphic.NewMesh(geomWheel, matWheel)
var zdir float32 = 1.0
if j%2 == 0 {
zdir = -1.0
}
meshWheel.SetPosition(
-(BASE_LENGTH/2)+WHEEL_RADIUS+0.2+float32(i)*(WHEEL_RADIUS*2),
WHEEL_RADIUS,
zdir*zWheel,
)
meshWheel.SetRotationX(math32.Pi / 2)
model.node.Add(meshWheel)
// Creates wheel cap
geomCap := geometry.NewCircle(CAP_RADIUS, 20)
meshCap := graphic.NewMesh(geomCap, matCap)
meshCap.SetPositionX(-(BASE_LENGTH / 2) + WHEEL_RADIUS + 0.2 + float32(i)*(WHEEL_RADIUS*2))
meshCap.SetPositionY(WHEEL_RADIUS)
zWheelCap := zWheel + WHEEL_WIDTH/2 + CAP_DZ
if j%2 != 0 {
meshCap.SetPositionZ(zWheelCap)
} else {
meshCap.SetPositionZ(-zWheelCap)
}
// Rotate the wheel cap circle geometry
if j%2 == 0 {
geomCap.ApplyMatrix(math32.NewMatrix4().MakeRotationX(-math32.Pi))
}
model.caps = append(model.caps, meshCap)
model.node.Add(meshCap)
}
}
// Creates the wheel tracks
for i := 0; i < 2; i++ {
geomTrack := geometry.NewBox(WHEEL_RADIUS*6, WHEEL_WIDTH+EPS, TRACK_HEIGHT)
matTrack := material.NewPhong(TRACK_COLOR)
meshTrack := graphic.NewMesh(geomTrack, matTrack)
var zdir float32 = 1.0
if i%2 == 0 {
zdir = -1.0
}
meshTrack.SetPosition(0, TRACK_HEIGHT/2, zdir*zWheel)
meshTrack.SetRotationX(math32.Pi / 2)
model.node.Add(meshTrack)
}
// Creates the tank base chassis and adds it to the group
geomBase := geometry.NewBox(BASE_LENGTH, BASE_WIDTH, BASE_HEIGHT)
matBase := material.NewPhong(BASE_COLOR)
model.meshBase = graphic.NewMesh(geomBase, matBase)
model.meshBase.SetPosition(
0,
2*WHEEL_RADIUS+(BASE_HEIGHT/2)-EMBED,
0,
)
model.meshBase.SetRotationX(math32.Pi / 2)
model.node.Add(model.meshBase)
// Create node top for the turret and cannon
model.nodeTop = core.NewNode()
// Create the turret mesh and add it to the top group
geomTurret := geometry.NewCylinder(TUR_TOP_RADIUS, TUR_BOTTOM_RADIUS, TUR_HEIGHT, 32, 32, 0, 2*math32.Pi, true, true)
matTurret := material.NewPhong(TUR_COLOR)
meshTurret := graphic.NewMesh(geomTurret, matTurret)
meshTurret.SetPositionY(model.meshBase.Position().Y + TUR_HEIGHT/2)
model.nodeTop.Add(meshTurret)
// Create the cannon geometry
geomCannon := geometry.NewCylinder(CANNON_TOP_RADIUS, CANNON_BOTTOM_RADIUS, CANNON_LENGTH, 20, 20, 0, 2*math32.Pi, true, true)
matCannon := material.NewPhong(CANNON_COLOR)
matCannonTop := material.NewPhong(CANNON_TOP_COLOR)
model.meshCannon = graphic.NewMesh(geomCannon, nil)
model.meshCannon.AddGroupMaterial(matCannon, 0)
model.meshCannon.AddGroupMaterial(matCannonTop, 1)
// translate the cannon geometry so its origin is at its start (not the middle)
// from which it will rotate.
geomCannon.ApplyMatrix(math32.NewMatrix4().MakeTranslation(0, CANNON_LENGTH/2, 0))
model.meshCannon.SetPosition(
0,
meshTurret.Position().Y+0.1,
0,
)
geomCannon.ApplyMatrix(math32.NewMatrix4().MakeRotationZ(-math32.Pi / 2))
model.nodeTop.Add(model.meshCannon)
model.node.Add(model.nodeTop)
return model
}