Пример #1
0
func (me Movable) ComputeMove(org d2.Vec2, dt time.Duration) d2.Vec2 {
	// update position on the player path
	if dst, exists := me.waypoints.Peek(); exists {
		// compute distance to be covered as time * speed
		distance := float32(dt.Seconds()) * me.Speed
		// compute translation and direction vectors
		dir := dst.Sub(org)
		b := dir.Len()
		dir.Normalize()

		// compute next position
		pos := org.Add(dir.Scale(distance))
		a := pos.Sub(org).Len()

		// check against edge-cases
		isNan := math32.IsNaN(a) || math32.IsNaN(b) || math32.IsNaN(dir.Len()) || math32.Abs(a-b) < 1e-3

		if a > b || isNan {
			return dst
		} else {
			return pos
		}
	}
	return org
}
Пример #2
0
/*
 * Move updates the movable position regarding a time delta
 *
 * Move returns true if the position has actually been modified
 */
func (me *Movable) Move(dt time.Duration) (hasMoved bool) {
	// update position on the player path
	if dst, exists := me.waypoints.Peek(); exists {
		// compute distance to be covered as time * speed
		distance := float32(dt.Seconds()) * me.Speed

		for {
			// compute translation and direction vectors
			dir := dst.Sub(me.Pos)
			b := dir.Len()
			dir.Normalize()

			// compute new position
			pos := me.Pos.Add(dir.Scale(distance))
			a := pos.Sub(me.Pos).Len()

			// check against edge-cases
			isNan := math32.IsNaN(a) || math32.IsNaN(b) || math32.IsNaN(dir.Len()) || math32.Abs(a-b) < 1e-3

			hasMoved = true
			if a > b || isNan {
				me.Pos = dst
				if _, exists = me.waypoints.Peek(); exists {
					me.waypoints.Pop()
					break
				} else {
					break
				}

				if isNan {
					break
				}

				distance = a - b
			} else {
				me.Pos = pos
				break
			}
		}
	}
	return
}