GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — coll/box_tilemap_sweep.go at v1.6.0 · setanarut/coll Expand file tree/ Copy pathbox_tilemap_sweep.goMore file actions 213 lines (182 loc) · 5.56 KB / Copy pathbox_tilemap_sweep.goFile metadata and controls213 lines (182 loc) · 5.56 KB 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 package coll import ( "image" "math" "github.com/setanarut/v" ) type Tile interface { IsSolid() bool } // TileHitInfo stores information about a collision with a tile type TileHitInfo struct { TileCoords image.Point // X,Y coordinates of the tile in the tilemap Normal v.Vec // Normal vector of the collision (-1/0/1) } // TileCollider handles collision detection between AABB and [][]uint8 2D tilemap type TileCollider struct { Collisions []TileHitInfo // List of collisions from last check CellSize image.Point // Width and height of tiles TileMap [][]Tile // 2D grid of tile interface } // NewTileCollider creates a new tile collider with the given tilemap and tile dimensions func NewTileCollider(tileMap [][]Tile, tileWidth, tileHeight int) *TileCollider { return &TileCollider{ TileMap: tileMap, CellSize: image.Point{tileWidth, tileHeight}, } } // TileCollisionCallback is called when collisions occur, receiving collision info and final movement type TileCollisionCallback func([]TileHitInfo, float64, float64) // Collide checks for collisions when a moving aabb and returns the allowed movement func (c *TileCollider) Collide(box AABB, delta v.Vec, onCollide TileCollisionCallback) v.Vec { c.Collisions = c.Collisions[:0] if delta.X == 0 && delta.Y == 0 { return delta } if math.Abs(delta.X) > math.Abs(delta.Y) { if delta.X != 0 { delta.X = c.CollideX(&box, delta.X) } if delta.Y != 0 { box.Pos.X += delta.X delta.Y = c.CollideY(&box, delta.Y) } } else { if delta.Y != 0 { delta.Y = c.CollideY(&box, delta.Y) } if delta.X != 0 { box.Pos.Y += delta.Y delta.X = c.CollideX(&box, delta.X) } } if onCollide != nil { onCollide(c.Collisions, delta.X, delta.Y) } return delta } // CollideX checks for collisions along the X axis and returns the allowed X movement func (c *TileCollider) CollideX(aabb *AABB, deltaX float64) float64 { checkLimit := max(1, int(math.Ceil(math.Abs(deltaX)/float64(c.CellSize.Y)))+1) rectTop := aabb.Top() rectBottom := aabb.Bottom() rectTileTopCoord := int(math.Floor(rectTop / float64(c.CellSize.Y))) rectTileBottomCoord := int(math.Ceil((rectBottom)/float64(c.CellSize.Y))) - 1 if deltaX > 0 { startRightX := int(math.Floor((aabb.Pos.X + aabb.Half.X) / float64(c.CellSize.X))) endX := startRightX + checkLimit endX = min(endX, len(c.TileMap[0])) for y := rectTileTopCoord; y <= rectTileBottomCoord; y++ { if y < 0 || y >= len(c.TileMap) { continue } for x := startRightX; x < endX; x++ { if x < 0 || x >= len(c.TileMap[0]) { continue } if c.TileMap[y][x] != nil && c.TileMap[y][x].IsSolid() { tileLeft := float64(x * c.CellSize.X) collision := tileLeft - (aabb.Pos.X + aabb.Half.X) if collision <= deltaX { deltaX = collision c.Collisions = append(c.Collisions, TileHitInfo{ TileCoords: image.Point{x, y}, Normal: v.Left, }) } } } } } if deltaX < 0 { rectLeft := aabb.Left() endX := int(math.Floor(rectLeft / float64(c.CellSize.X))) startX := endX - checkLimit startX = max(startX, 0) for y := rectTileTopCoord; y <= rectTileBottomCoord; y++ { if y < 0 || y >= len(c.TileMap) { continue } for x := startX; x <= endX; x++ { if x < 0 || x >= len(c.TileMap[0]) { continue } if c.TileMap[y][x] != nil && c.TileMap[y][x].IsSolid() { tileRight := float64((x + 1) * c.CellSize.X) collision := tileRight - rectLeft if collision >= deltaX { deltaX = collision c.Collisions = append(c.Collisions, TileHitInfo{ TileCoords: image.Point{x, y}, Normal: v.Right, }) } } } } } return deltaX } // CollideY checks for collisions along the Y axis and returns the allowed Y movement func (c *TileCollider) CollideY(rect *AABB, deltaY float64) float64 { checkLimit := max(1, int(math.Ceil(math.Abs(deltaY)/float64(c.CellSize.Y)))+1) rectLeft := rect.Left() rectRight := rect.Right() rectTileLeftCoord := int(math.Floor(rectLeft / float64(c.CellSize.X))) rectTileRightCoord := int(math.Ceil(rectRight/float64(c.CellSize.X))) - 1 if deltaY > 0 { rectBottom := rect.Pos.Y + rect.Half.Y startBottomY := int(math.Floor(rectBottom / float64(c.CellSize.Y))) endY := startBottomY + checkLimit endY = min(endY, len(c.TileMap)) for x := rectTileLeftCoord; x <= rectTileRightCoord; x++ { if x < 0 || x >= len(c.TileMap[0]) { continue } for y := startBottomY; y < endY; y++ { if y < 0 || y >= len(c.TileMap) { continue } if c.TileMap[y][x] != nil && c.TileMap[y][x].IsSolid() { tileTop := float64(y * c.CellSize.Y) collision := tileTop - rectBottom if collision <= deltaY { deltaY = collision c.Collisions = append(c.Collisions, TileHitInfo{ TileCoords: image.Point{x, y}, Normal: v.Up, }) } } } } } if deltaY < 0 { rectTop := rect.Top() endY := int(math.Floor(rectTop / float64(c.CellSize.Y))) startY := endY - checkLimit startY = max(startY, 0) for x := rectTileLeftCoord; x <= rectTileRightCoord; x++ { if x < 0 || x >= len(c.TileMap[0]) { continue } for y := startY; y <= endY; y++ { if y < 0 || y >= len(c.TileMap) { continue } if c.TileMap[y][x] != nil && c.TileMap[y][x].IsSolid() { tileBottom := float64((y + 1) * c.CellSize.Y) collision := tileBottom - rectTop if collision >= deltaY { deltaY = collision c.Collisions = append(c.Collisions, TileHitInfo{ TileCoords: image.Point{x, y}, Normal: v.Down, }) } } } } } return deltaY } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |