This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.go
102 lines (91 loc) · 2.33 KB
/
world.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
package phys
import (
"fmt"
"math"
"github.com/tanema/amore/gfx"
)
type World struct {
width float32
height float32
cell_width float32
cell_height float32
numWidth float32
numHeight float32
grid [][]*Cell
}
func NewWorld(width, height, cell_size float32) *World {
cell_width := nextEqualyDividable(width, cell_size)
cell_height := nextEqualyDividable(height, cell_size)
numWidth := width / cell_width
numHeight := height / cell_height
//initialize grid
grid := make([][]*Cell, int(numWidth))
for i := 0; i < int(numWidth); i++ {
grid[i] = make([]*Cell, int(numHeight))
for j := 0; j < int(numHeight); j++ {
grid[i][j] = &Cell{
x: float32(i) * cell_width,
y: float32(j) * cell_height,
width: cell_width,
height: cell_height,
}
}
}
return &World{
width: width,
height: height,
cell_width: cell_width,
cell_height: cell_height,
numWidth: numWidth,
numHeight: numHeight,
grid: grid,
}
}
func nextEqualyDividable(unit, size float32) float32 {
for ; math.Mod(float64(unit), float64(size)) != 0; size++ {
}
return size
}
func (world *World) AddBody(collidable Collidable, name string, x, y, scale float32, points []float32) *Body {
new_body := newBody(world, collidable, name, points)
new_body.Move(x, y, 0, scale)
return new_body
}
func (world *World) CellAt(x, y float32) *Cell {
x = float32(math.Floor(float64(x / world.cell_width)))
y = float32(math.Floor(float64(y / world.cell_height)))
if x >= world.numWidth {
x -= world.numWidth
}
if x < 0 {
x = world.numWidth - 1
}
if y >= world.numHeight {
y = 0
}
if y < 0 {
y = world.numHeight - 1
}
return world.grid[int(x)][int(y)]
}
func (world *World) DrawGrid() {
// This really sucks and isn't the best but it is for debugging so it shouldnt
// be used all the time
count := map[*Body]bool{}
for _, row := range world.grid {
for _, cell := range row {
for _, body := range cell.bodies {
count[body] = true
}
}
}
gfx.Print(fmt.Sprintf("physical objects: %v", len(count)), 0, 30)
gfx.SetColor(100, 100, 100, 100)
for i := world.cell_width; i < world.width; i += world.cell_width {
gfx.Line(i, 0, i, world.height)
}
for i := world.cell_height; i < world.height; i += world.cell_height {
gfx.Line(0, i, world.width, i)
}
gfx.SetColor(255, 255, 255, 255)
}