Example #1
0
func (m *Map) GetCollisions2(worldPosition engine.Vector, width, height float32, xCollisions, yCollisions []int) (x, y []int) {
	_, x1, y1 := m.PositionToTile(worldPosition.Add(engine.Vector{width / 2, height / 2, 0}))
	_, x2, y2 := m.PositionToTile(worldPosition.Add(engine.Vector{-width / 2, -height / 2, 0}))
	if x1 > x2 {
		x1, x2 = x2, x1
	}
	if y1 > y2 {
		y1, y2 = y2, y1
	}
	for ; y1 <= y2; y1++ {
		for x := x1; x <= x2; x++ {
			if !m.IsTileWalkabke(x, y1) {
				if xCollisions == nil {
					xCollisions = make([]int, 0, 4)
				}
				if yCollisions == nil {
					yCollisions = make([]int, 0, 4)
				}
				xCollisions = append(xCollisions, x)
				yCollisions = append(yCollisions, y1)
			}
		}
	}

	return xCollisions, yCollisions
}
Example #2
0
func (m *Map) CheckCollision(worldPosition engine.Vector, width, height float32) bool {
	_, x1, y1 := m.PositionToTile(worldPosition.Add(engine.Vector{width / 2, height / 2, 0}))
	_, x2, y2 := m.PositionToTile(worldPosition.Add(engine.Vector{-width / 2, -height / 2, 0}))
	if x1 > x2 {
		x1, x2 = x2, x1
	}
	if y1 > y2 {
		y1, y2 = y2, y1
	}

	for ; y1 <= y2; y1++ {
		for x := x1; x <= x2; x++ {
			if !m.IsTileWalkabke(x, y1) {
				return true
			}
		}
	}

	return false
}