Example #1
0
// Since go has multiple return values, I just went ahead and made it return the view and perspective matrices (in that order) rather than messing with getter methods
func (c *Camera) ComputeViewPerspective() (mgl32.Mat4, mgl32.Mat4) {
	if mgl64.FloatEqual(-1.0, c.time) {
		c.time = glfw.GetTime()
	}

	currTime := glfw.GetTime()
	deltaT := currTime - c.time

	xPos, yPos := c.window.GetCursorPosition()
	c.window.SetCursorPosition(width/2.0, height/2.0)

	c.hAngle += mouseSpeed * ((width / 2.0) - float64(xPos))
	c.vAngle += mouseSpeed * ((height / 2.0) - float64(yPos))

	dir := mgl32.Vec3{
		float32(math.Cos(c.vAngle) * math.Sin(c.hAngle)),
		float32(math.Sin(c.vAngle)),
		float32(math.Cos(c.vAngle) * math.Cos(c.hAngle))}

	right := mgl32.Vec3{
		float32(math.Sin(c.hAngle - math.Pi/2.0)),
		0.0,
		float32(math.Cos(c.hAngle - math.Pi/2.0))}

	up := right.Cross(dir)

	if c.window.GetKey(glfw.KeyUp) == glfw.Press || c.window.GetKey('W') == glfw.Press {
		c.pos = c.pos.Add(dir.Mul(float32(deltaT * speed)))
	}

	if c.window.GetKey(glfw.KeyDown) == glfw.Press || c.window.GetKey('S') == glfw.Press {
		c.pos = c.pos.Sub(dir.Mul(float32(deltaT * speed)))
	}

	if c.window.GetKey(glfw.KeyRight) == glfw.Press || c.window.GetKey('D') == glfw.Press {
		c.pos = c.pos.Add(right.Mul(float32(deltaT * speed)))
	}

	if c.window.GetKey(glfw.KeyLeft) == glfw.Press || c.window.GetKey('A') == glfw.Press {
		c.pos = c.pos.Sub(right.Mul(float32(deltaT * speed)))
	}

	// Adding to the original tutorial, Space goes up
	if c.window.GetKey(glfw.KeySpace) == glfw.Press {
		c.pos = c.pos.Add(up.Mul(float32(deltaT * speed)))
	}

	// Adding to the original tutorial, left control goes down
	if c.window.GetKey(glfw.KeyLeftControl) == glfw.Press {
		c.pos = c.pos.Sub(up.Mul(float32(deltaT * speed)))
	}

	fov := initialFOV //- 5.0*float64(glfw.MouseWheel())

	proj := mgl32.Perspective(float32(fov), 4.0/3.0, 0.1, 100.0)
	view := mgl32.LookAtV(c.pos, c.pos.Add(dir), up)

	c.time = currTime

	return view, proj
}
Example #2
0
func (ai *AI) getClosestFiltered(p mgl32.Vec2, cells []*agario.Cell, filter filterFunc) *agario.Cell {
	/*var closest *agario.Cell
	closestDist := float32(math.MaxFloat32)
	for _, cell := range cells {
		if filter != nil && !filter(cell) {
			continue
		}

		dist := dist2(p, cell.Position)
		if closest == nil {
			closest = cell
			closestDist = dist
			continue
		}

		if dist < closestDist || (dist == closestDist && cell.ID < closest.ID) {
			closest = cell
			closestDist = dist
		}
	}
	return closest*/

	if len(cells) == 0 {
		return nil
	}

	var (
		shortest     *agario.Cell
		shortestCost = math.MaxFloat64
	)
	for _, c := range cells {
		if filter != nil && !filter(c) {
			continue
		}

		n := ai.Map.GetNode(gameToCostMap(c.Position.Elem()))
		cost := ai.DijkstraMap.WeightTo(n)
		/*if cost >= costDoNotPass {
			continue
		}*/
		if shortest == nil || cost < shortestCost || (mgl64.FloatEqual(cost, shortestCost) && c.ID < shortest.ID) {
			shortest = c
			shortestCost = cost
		}
	}

	return shortest

	/*bfs := new(traverse.BreadthFirst)
	visited := 0
	bfs.Visit = func(u, v graph.Node) {
		visited++
	}

	n := bfs.Walk(ai.Map, ai.Map.GetNode(gameToCostMap(p.X(), p.Y())), ai.cellsUntil(cells, filter))
	ai.addStatusMessage("BFS: visited " + strconv.Itoa(visited) + " nodes")
	if n == nil {
		return nil
	}

	mN := n.(*mapNode)

	minX, minY := costMapToGame(mN.X, mN.Y)

	//maxX := (minX + 1) * costMapReduction
	//maxY := (minY + 1) * costMapReduction
	maxX, maxY := costMapToGame(mN.X+1, mN.Y+1)

	for _, c := range cells {
		x := c.Position.X()
		y := c.Position.Y()

		if x >= minX && x < maxX && y >= minY && y < maxY {
			return c
		}
	}

	return nil*/
}