2023-10-23 15:45:18 +03:00
|
|
|
package gg
|
2023-05-11 12:28:34 +03:00
|
|
|
|
|
|
|
// Implementing the interface lets
|
|
|
|
// the engine to work faster about
|
|
|
|
// collisions because it first checks
|
|
|
|
// if the the bigger collider that
|
2023-12-14 22:01:07 +03:00
|
|
|
// contains more complicated (accurate) structure
|
2023-05-11 12:28:34 +03:00
|
|
|
// do collide.
|
2024-01-16 01:08:21 +03:00
|
|
|
//type ColliderSimplifier interface {
|
|
|
|
//ColliderSimplify() Triangle
|
|
|
|
//}
|
|
|
|
|
2024-01-16 07:37:53 +03:00
|
|
|
type CollisionMap map[CollisionType] []Collision
|
|
|
|
|
2024-01-16 01:08:21 +03:00
|
|
|
type CollisionType int
|
|
|
|
const (
|
2024-01-16 07:37:53 +03:00
|
|
|
CollisionNo CollisionType = iota
|
|
|
|
CollisionStaticPhysics
|
|
|
|
CollisionTrigger
|
|
|
|
CollisionLast
|
2024-01-16 01:08:21 +03:00
|
|
|
)
|
2023-05-11 12:28:34 +03:00
|
|
|
|
2024-01-16 01:08:21 +03:00
|
|
|
// The structure contains collision information.
|
2023-05-11 12:28:34 +03:00
|
|
|
type Collision struct {
|
2024-01-16 01:08:21 +03:00
|
|
|
Type CollisionType
|
2024-01-16 04:11:42 +03:00
|
|
|
What, With Collider
|
2024-01-16 01:08:21 +03:00
|
|
|
// Points of Self contained in
|
|
|
|
Points Points
|
|
|
|
Crosses []LineCross
|
2023-05-11 12:28:34 +03:00
|
|
|
}
|
|
|
|
|
2023-12-14 22:01:07 +03:00
|
|
|
|
2023-05-22 23:39:01 +03:00
|
|
|
// Implementing the interface lets the engine
|
|
|
|
// to determine if the object collides with anything.
|
2023-05-11 12:28:34 +03:00
|
|
|
type Collider interface {
|
2024-01-16 01:08:21 +03:00
|
|
|
// Checks whether the object is collidable
|
2024-01-16 04:11:42 +03:00
|
|
|
CollisionType() CollisionType
|
2024-01-16 01:08:21 +03:00
|
|
|
IsCollidable() bool
|
|
|
|
Verticer
|
|
|
|
Edger
|
|
|
|
PointContainer
|
|
|
|
}
|
|
|
|
type Collidability struct {
|
2024-01-16 04:11:42 +03:00
|
|
|
Type CollisionType
|
2024-01-16 01:08:21 +03:00
|
|
|
Collidable bool
|
|
|
|
}
|
2024-01-16 04:11:42 +03:00
|
|
|
func (c Collidability) CollisionType() CollisionType {
|
|
|
|
return c.Type
|
|
|
|
}
|
2024-01-16 01:08:21 +03:00
|
|
|
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)
|
2024-01-16 07:37:53 +03:00
|
|
|
GetCollisionInterest() []CollisionType
|
2024-01-16 01:08:21 +03:00
|
|
|
Collider
|
|
|
|
}
|
|
|
|
type Resolvability struct {
|
|
|
|
Resolvable bool
|
2024-01-16 07:37:53 +03:00
|
|
|
CollisionInterest []CollisionType
|
2024-01-16 01:08:21 +03:00
|
|
|
}
|
|
|
|
func (r Resolvability) IsResolvable() bool {
|
|
|
|
return r.Resolvable
|
2023-05-11 12:28:34 +03:00
|
|
|
}
|
2024-01-16 07:37:53 +03:00
|
|
|
func (r Resolvability) GetCollisionInterest() []CollisionType {
|
|
|
|
return r.CollisionInterest
|
|
|
|
}
|
2023-05-11 12:28:34 +03:00
|
|
|
|
2024-01-16 01:08:21 +03:00
|
|
|
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{
|
2024-01-16 04:11:42 +03:00
|
|
|
Type: c2.CollisionType(),
|
|
|
|
What: c1,
|
|
|
|
With: c2,
|
2024-01-16 01:08:21 +03:00
|
|
|
Points: pts,
|
|
|
|
Crosses: crosses,
|
|
|
|
}, len(pts) != 0 || doCross
|
|
|
|
}
|
2023-05-22 23:39:01 +03:00
|
|
|
|
2024-01-16 01:08:21 +03:00
|
|
|
func GetCollisions(c Collider, cs []Collider) []Collision {
|
|
|
|
ret := []Collision{}
|
|
|
|
for _, ic := range cs {
|
2024-01-16 04:11:42 +03:00
|
|
|
if ic == c {
|
2024-01-16 01:08:21 +03:00
|
|
|
continue
|
|
|
|
}
|
2024-01-16 04:11:42 +03:00
|
|
|
col, has := Collide(c, ic)
|
|
|
|
if has {
|
|
|
|
ret = append(ret, col)
|
|
|
|
}
|
2024-01-16 01:08:21 +03:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|