1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-07-22 13:04:18 -04:00
OpenDiablo2/d2common/d2math/d2vector/position.go
danhale-git 029cb62972
Vector float64 (#565)
* Fixed nil pointer in Copy()

* Position added

Added Floor() and String() methods to Vector.

Also added Position which declares an embedded Vector2 and returns various forms of it.

* d2vector.Vector2 renamed to d2vector.BigFloat

* vector.go renamed to big_float.go

* Float64 stub and more renaming

* Vector value getters

* Separate vector types with initial methods.

* Divide and lint warnings.

* Distance and Length.

* Scale, Abs and Negate.

* CompareFloat64Fuzzy delta direction reversed.

* Refactor vector_test.go.

* Renamed Approx methods.

* Distance and Length.

* Distance and Length.

* Removed BigFloat and Vector, renamed Float64 to Vector, simplified tests.

* Angle, SignedAngle and other small functions.

* Receiver rename.

* SingedAngle and test fixed

* Rotate.

* SetLength.

* Cross.

* NinetyAnti and NinetyClock.

* Lerp and Clamp.

* Reflect and ReflectSurface.

* Cardinal convenience functions.

* Comments.

* Panic on NaN and Inf in Position.

* Lint warnings and comments.
2020-07-09 08:30:55 -04:00

73 lines
1.6 KiB
Go

package d2vector
import (
"fmt"
"math"
)
const subTilesPerTile float64 = 5
// Position is a vector in world space. The stored value is the one returned by Position.World()
type Position struct {
Vector
}
// NewPosition creates a new Position at the given float64 world position.
func NewPosition(x, y float64) *Position {
p := &Position{NewVector(x, y)}
p.checkValues()
return p
}
// Set sets this position to the given x and y values.
func (p *Position) Set(x, y float64) {
p.x, p.y = x, y
p.checkValues()
}
func (p *Position) checkValues() {
if math.IsNaN(p.x) || math.IsNaN(p.y) {
panic(fmt.Sprintf("float value is NaN: %s", p.Vector))
}
if math.IsInf(p.x, 0) || math.IsInf(p.y, 0) {
panic(fmt.Sprintf("float value is Inf: %s", p.Vector))
}
}
// World is the position, where 1 = one map tile.
func (p *Position) World() *Vector {
return &p.Vector
}
// Tile is the tile position, always a whole number.
func (p *Position) Tile() *Vector {
c := p.World().Clone()
return c.Floor()
}
// TileOffset is the offset from the tile position, always < 1.
func (p *Position) TileOffset() *Vector {
c := p.World().Clone()
return c.Subtract(p.Tile())
}
// SubWorld is the position, where 5 = one map tile.
func (p *Position) SubWorld() *Vector {
c := p.World().Clone()
return c.Scale(subTilesPerTile)
}
// SubTile is the tile position in sub tiles, always a multiple of 5.
func (p *Position) SubTile() *Vector {
c := p.Tile().Clone()
return c.Scale(subTilesPerTile)
}
// SubTileOffset is the offset from the sub tile position in sub tiles, always < 1.
func (p *Position) SubTileOffset() *Vector {
c := p.SubWorld().Clone()
return c.Subtract(p.SubTile())
}