100 lines
2 KiB
Go
100 lines
2 KiB
Go
package gg
|
|
|
|
// Implementing the interface lets
|
|
// the engine to work faster about
|
|
// collisions because it first checks
|
|
// if the the bigger collider that
|
|
// contains more complicated (accurate) structure
|
|
// do collide.
|
|
//type ColliderSimplifier interface {
|
|
//ColliderSimplify() Triangle
|
|
//}
|
|
|
|
type CollisionType int
|
|
const (
|
|
PhysCollision CollisionType = iota
|
|
TriggerCollision
|
|
)
|
|
|
|
// The structure contains collision information.
|
|
type Collision struct {
|
|
Type CollisionType
|
|
What, With Collider
|
|
// Points of Self contained in
|
|
Points Points
|
|
Crosses []LineCross
|
|
}
|
|
|
|
|
|
// Implementing the interface lets the engine
|
|
// to determine if the object collides with anything.
|
|
type Collider interface {
|
|
// Checks whether the object is collidable
|
|
CollisionType() CollisionType
|
|
IsCollidable() bool
|
|
Verticer
|
|
Edger
|
|
PointContainer
|
|
}
|
|
type Collidability struct {
|
|
Type CollisionType
|
|
Collidable bool
|
|
}
|
|
func (c Collidability) CollisionType() CollisionType {
|
|
return c.Type
|
|
}
|
|
func (c Collidability) IsCollidable() bool {
|
|
return c.Collidable
|
|
}
|
|
type PointContainer interface {
|
|
ContainedPoints(Points) Points
|
|
}
|
|
type Verticer interface {
|
|
Vertices() Vertices
|
|
}
|
|
type Edger interface {
|
|
Edges() Edges
|
|
}
|
|
|
|
// Implementing the type provides way
|
|
// to resolve collisions and other problems.
|
|
// The Resolve() is called right after the Update() of Updater.
|
|
type Resolver interface {
|
|
IsResolvable() bool
|
|
Resolve(c *Context)
|
|
Collider
|
|
}
|
|
type Resolvability struct {
|
|
Resolvable bool
|
|
}
|
|
func (r Resolvability) IsResolvable() bool {
|
|
return r.Resolvable
|
|
}
|
|
|
|
func Collide(c1, c2 Collider) (Collision, bool) {
|
|
vertices := c1.Vertices()
|
|
es1, es2 := c1.Edges(), c2.Edges()
|
|
crosses, doCross := es1.LineCrossWith(es2)
|
|
pts := c2.ContainedPoints(vertices)
|
|
return Collision{
|
|
Type: c2.CollisionType(),
|
|
What: c1,
|
|
With: c2,
|
|
Points: pts,
|
|
Crosses: crosses,
|
|
}, len(pts) != 0 || doCross
|
|
}
|
|
|
|
func GetCollisions(c Collider, cs []Collider) []Collision {
|
|
ret := []Collision{}
|
|
for _, ic := range cs {
|
|
if ic == c {
|
|
continue
|
|
}
|
|
col, has := Collide(c, ic)
|
|
if has {
|
|
ret = append(ret, col)
|
|
}
|
|
}
|
|
return ret
|
|
}
|