-
Notifications
You must be signed in to change notification settings - Fork 140
/
entity.go
51 lines (41 loc) · 1.37 KB
/
entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package trace
import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)
// EntityResult is the result of a ray trace collision with an entities bounding box.
type EntityResult struct {
bb cube.BBox
pos mgl64.Vec3
face cube.Face
entity world.Entity
}
// BBox returns the entities bounding box that was collided with.
func (r EntityResult) BBox() cube.BBox {
return r.bb
}
// Position ...
func (r EntityResult) Position() mgl64.Vec3 {
return r.pos
}
// Face ...
func (r EntityResult) Face() cube.Face {
return r.face
}
// Entity returns the entity that was collided with.
func (r EntityResult) Entity() world.Entity {
return r.entity
}
// EntityIntercept performs a ray trace and calculates the point on the entities bounding box's edge nearest to the start position
// that the ray collided with.
// EntityIntercept returns an EntityResult with the entity collided with and with the colliding vector closest to the start position,
// if no colliding point was found, a zero BlockResult is returned ok is false.
func EntityIntercept(e world.Entity, start, end mgl64.Vec3) (result EntityResult, ok bool) {
bb := e.Type().BBox(e).Translate(e.Position()).Grow(0.3)
r, ok := BBoxIntercept(bb, start, end)
if !ok {
return
}
return EntityResult{bb: bb, pos: r.Position(), face: r.Face(), entity: e}, true
}