github.com/df-mc/dragonfly@v0.9.13/server/block/cube/trace/entity.go (about)

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