dimanche 22 novembre 2015

How to use trig on screen coordinates to calculate angle between points

I am working on a game. This game is top down, real-time, and must feature pathing. My game must calculate the angle between a player's current position and the one they click to walk to.
Problem is, I am using screen coordinates, as in "x increases to the right, y increases to the bottom"

Here's where I'm at with some code

type Position struct {
  X float64
  Y float64
}

type Vector struct {
  Radians  float64
  Distance float64
}

func CreatePathVector(pos1 *Position, pos2 *Position, speed int) *Vector {
  ydiff := pos2.Y - pos1.Y
  xdiff := pos2.X - pos1.X
  radians := math.Atan2(ydiff, xdiff)

  return &Vector{
    Radians:  radians,
    Distance: float64(speed),
  }
}

func (p *Position) Add(v *Vector) Position {
  return Position{
    X: p.X + math.Sin(v.Radians)*v.Distance,
    Y: p.Y + math.Cos(v.Radians)*v.Distance,
  }
}




Aucun commentaire:

Enregistrer un commentaire