我正在做一个游戏。这个游戏是自上而下的,实时的,并且必须具有路径。我的游戏必须计算玩家当前位置与他们点击步行到达的位置之间的角度。
问题是,我正在使用屏幕坐标,如“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}
如您所见,在两个示例中,重复添加向量的步骤并没有转向目的地
一只名叫tom的猫
相关分类