如何在屏幕坐标上使用三角函数来计算点之间的角度

我正在做一个游戏。这个游戏是自上而下的,实时的,并且必须具有路径。我的游戏必须计算玩家当前位置与他们点击步行到达的位置之间的角度。

问题是,我正在使用屏幕坐标,如“x 向右增加,y 向底部增加”


这是我使用一些代码的地方


package main


import (

  "fmt"

  "math"

)


func main() {

  position1 := &Position{550, 200}

  position2 := &Position{700, 500}

  vector1 := CreatePathVector(position1, position2, 50)


  fmt.Printf("position1: %v\nposition2: %v\n", position1, position2)


  position := position1

  for i := 0; i < 5; i++ {

    position = position.Add(vector1)

    fmt.Printf("next position: %v\n", position)

  }


  position3 := &Position{400, 500}

  position4 := &Position{700, 400}

  vector2 := CreatePathVector(position3, position4, 50)


  fmt.Printf("position3: %v\nposition4: %v\n", position3, position4)


  position = position3

  for i := 0; i < 5; i++ {

    position = position.Add(vector2)

    fmt.Printf("next position: %v\n", position)

  }

}


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,

  }

}

这是输出


position1: &{550 200}

position2: &{700 500}

next position: &{594.7213595499958 222.3606797749979}

next position: &{639.4427190999916 244.72135954999578}

next position: &{684.1640786499873 267.0820393249937}

next position: &{728.8854381999831 289.44271909999156}

next position: &{773.6067977499789 311.80339887498945}

position3: &{400 500}

position4: &{700 400}

next position: &{384.1886116991581 547.4341649025257}

next position: &{368.37722339831623 594.8683298050514}

next position: &{352.56583509747435 642.3024947075771}

next position: &{336.75444679663246 689.7366596101028}

next position: &{320.9430584957906 737.1708245126285}

如您所见,在两个示例中,重复添加向量的步骤并没有转向目的地


慕尼黑5688855
浏览 114回答 1
1回答

一只名叫tom的猫

如果您选择使用笛卡尔坐标,就像我在评论中建议的那样,这就是您的代码的样子:package mainimport (&nbsp; "fmt"&nbsp; "math")func main() {&nbsp; position1 := &Position{550, 200}&nbsp; position2 := &Position{700, 500}&nbsp; vector1 := CreatePathVector(position1, position2, 70)&nbsp; fmt.Printf("position1: %v\nposition2: %v\n", position1, position2)&nbsp; position := position1&nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; position = position.Add(vector1)&nbsp; &nbsp; fmt.Printf("next position: %v\n", position)&nbsp; }&nbsp; position3 := &Position{400, 500}&nbsp; position4 := &Position{700, 400}&nbsp; vector2 := CreatePathVector(position3, position4, 50)&nbsp; fmt.Printf("position3: %v\nposition4: %v\n", position3, position4)&nbsp; position = position3&nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; position = position.Add(vector2)&nbsp; &nbsp; fmt.Printf("next position: %v\n", position)&nbsp; }}type Position struct {&nbsp; X float64&nbsp; Y float64}type Vector struct {&nbsp; dX&nbsp; float64&nbsp; dY float64}func CreatePathVector(pos1 *Position, pos2 *Position, speed int) *Vector {&nbsp; ydiff := pos2.Y - pos1.Y&nbsp; xdiff := pos2.X - pos1.X&nbsp; mag := math.Sqrt(xdiff*xdiff+ydiff*ydiff)&nbsp; return &Vector{&nbsp; &nbsp; dX:&nbsp; xdiff/mag*float64(speed),&nbsp; &nbsp; dY:&nbsp; ydiff/mag*float64(speed),&nbsp; }}func (p *Position) Add(v *Vector) *Position {&nbsp; return &Position{&nbsp; &nbsp; X: p.X + v.dX,&nbsp; &nbsp; Y: p.Y + v.dY,&nbsp; }}如果你想坚持的角度,只是切换Cos和Sin中Add。这是因为屏幕的方向并不重要:如果你把t = arctan(y/x)你y的背部sin(t)和x从背部cos(t)不管是什么x和y代表。所以添加应该是这样的:func (p *Position) Add(v *Vector) *Position {&nbsp; return &Position{&nbsp; &nbsp; X: p.X + math.Cos(v.Radians)*v.Distance,&nbsp; &nbsp; Y: p.Y + math.Sin(v.Radians)*v.Distance,&nbsp; }}我之前做过一些小游戏,我也尝试过使用角度来移动。我的建议是不要尝试。如果您想为游戏添加更逼真的物理效果,向量和线性代数将是您最好的朋友。在我看来,角度和三角函数太混乱了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go