feat: added the collision groupping to increase performance and make resolving simpler.
This commit is contained in:
parent
31940fa899
commit
8fcb1c2dc5
9 changed files with 62 additions and 24 deletions
|
@ -4,7 +4,7 @@ import (
|
||||||
"vultras.su/core/gg"
|
"vultras.su/core/gg"
|
||||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
||||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
||||||
_ "github.com/silbinarywolf/preferdiscretegpu"
|
//_ "github.com/silbinarywolf/preferdiscretegpu"
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -95,7 +95,6 @@ func main() {
|
||||||
}
|
}
|
||||||
e.Spawn(txt)
|
e.Spawn(txt)
|
||||||
|
|
||||||
|
|
||||||
//fmt.Println(rect.GetLayer(), player.GetLayer())
|
//fmt.Println(rect.GetLayer(), player.GetLayer())
|
||||||
fmt.Println("Starting...")
|
fmt.Println("Starting...")
|
||||||
err = e.Run()
|
err = e.Run()
|
||||||
|
|
|
@ -47,6 +47,7 @@ func (p *Player) Draw(c *Context) []gg.EVertex {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Update(c *Context) {
|
func (p *Player) Update(c *Context) {
|
||||||
|
p.Position.Y += 2
|
||||||
if p.Spawned {
|
if p.Spawned {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -122,6 +123,7 @@ func (p *Player) Update(c *Context) {
|
||||||
pp := *p
|
pp := *p
|
||||||
counter++
|
counter++
|
||||||
p.Spawned = true
|
p.Spawned = true
|
||||||
|
p.Collidable = false
|
||||||
c.Spawn(&pp)
|
c.Spawn(&pp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,25 +155,29 @@ func (p *Player) Update(c *Context) {
|
||||||
))
|
))
|
||||||
}}
|
}}
|
||||||
|
|
||||||
p.Position.Y += 2
|
}
|
||||||
|
|
||||||
|
func (p *Player) GetCollisionInterest() []gg.CollisionType {
|
||||||
|
return []gg.CollisionType{
|
||||||
|
gg.CollisionStaticPhysics,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Resolve(c *Context) {
|
func (p *Player) Resolve(c *Context) {
|
||||||
|
|
||||||
col := c.Collisions[0]
|
col := c.Collisions[0]
|
||||||
fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With)
|
if !p.Spawned {
|
||||||
switch col.Type{
|
fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With)
|
||||||
case gg.PhysCollision :
|
}
|
||||||
|
for _, col := range c.Collisions {switch col.Type{
|
||||||
|
case gg.CollisionStaticPhysics :
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
p.Position.Y -= 0.2
|
p.Position.Y -= 1
|
||||||
shit, collides := gg.Collide(p, col.With)
|
_, collides := gg.Collide(p, col.With)
|
||||||
fmt.Println("shit:", shit)
|
|
||||||
fmt.Println("do:", collides)
|
|
||||||
if !collides {
|
if !collides {
|
||||||
break LOOP
|
break LOOP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,10 @@ func NewRect() *Rect {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Rect) CollisionType() gg.CollisionType {
|
||||||
|
return gg.CollisionStaticPhysics
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Rect) Update(c *Context) {
|
func (r *Rect) Update(c *Context) {
|
||||||
//r.R += 0.3 * e.DT()
|
//r.R += 0.3 * e.DT()
|
||||||
//r.Position = c.AbsCursorPosition()
|
//r.Position = c.AbsCursorPosition()
|
||||||
|
|
|
@ -23,5 +23,5 @@ type Text struct{
|
||||||
}
|
}
|
||||||
|
|
||||||
func (txt *Text) Update(c *Context) {
|
func (txt *Text) Update(c *Context) {
|
||||||
txt.Data += string(c.Runes())
|
//txt.Data += string(c.Runes())
|
||||||
}
|
}
|
||||||
|
|
13
collider.go
13
collider.go
|
@ -10,10 +10,14 @@ package gg
|
||||||
//ColliderSimplify() Triangle
|
//ColliderSimplify() Triangle
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
type CollisionMap map[CollisionType] []Collision
|
||||||
|
|
||||||
type CollisionType int
|
type CollisionType int
|
||||||
const (
|
const (
|
||||||
PhysCollision CollisionType = iota
|
CollisionNo CollisionType = iota
|
||||||
TriggerCollision
|
CollisionStaticPhysics
|
||||||
|
CollisionTrigger
|
||||||
|
CollisionLast
|
||||||
)
|
)
|
||||||
|
|
||||||
// The structure contains collision information.
|
// The structure contains collision information.
|
||||||
|
@ -62,14 +66,19 @@ type Edger interface {
|
||||||
type Resolver interface {
|
type Resolver interface {
|
||||||
IsResolvable() bool
|
IsResolvable() bool
|
||||||
Resolve(c *Context)
|
Resolve(c *Context)
|
||||||
|
GetCollisionInterest() []CollisionType
|
||||||
Collider
|
Collider
|
||||||
}
|
}
|
||||||
type Resolvability struct {
|
type Resolvability struct {
|
||||||
Resolvable bool
|
Resolvable bool
|
||||||
|
CollisionInterest []CollisionType
|
||||||
}
|
}
|
||||||
func (r Resolvability) IsResolvable() bool {
|
func (r Resolvability) IsResolvable() bool {
|
||||||
return r.Resolvable
|
return r.Resolvable
|
||||||
}
|
}
|
||||||
|
func (r Resolvability) GetCollisionInterest() []CollisionType {
|
||||||
|
return r.CollisionInterest
|
||||||
|
}
|
||||||
|
|
||||||
func Collide(c1, c2 Collider) (Collision, bool) {
|
func Collide(c1, c2 Collider) (Collision, bool) {
|
||||||
vertices := c1.Vertices()
|
vertices := c1.Vertices()
|
||||||
|
|
35
engine.go
35
engine.go
|
@ -335,14 +335,18 @@ func (e *engine) Resolve() {
|
||||||
Resolver Resolver
|
Resolver Resolver
|
||||||
input chan *Context
|
input chan *Context
|
||||||
}
|
}
|
||||||
colliders := []Collider{}
|
colliders := map[CollisionType] []Collider{}
|
||||||
|
|
||||||
resolvers := []resHold{}
|
resolvers := []resHold{}
|
||||||
for _, object := range e.Objects.store {
|
for _, object := range e.Objects.store {
|
||||||
if object.IsCollidable() {
|
|
||||||
colliders = append(colliders, object)
|
|
||||||
}
|
|
||||||
if object.IsResolvable() {
|
if object.IsResolvable() {
|
||||||
|
interests := object.GetCollisionInterest()
|
||||||
|
for _, interest := range interests {
|
||||||
|
_, ok := colliders[interest]
|
||||||
|
if !ok {
|
||||||
|
colliders[interest] = []Collider{}
|
||||||
|
}
|
||||||
|
}
|
||||||
resolvers = append(resolvers, resHold{
|
resolvers = append(resolvers, resHold{
|
||||||
object,
|
object,
|
||||||
object.Input(),
|
object.Input(),
|
||||||
|
@ -350,8 +354,23 @@ func (e *engine) Resolve() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, object := range e.Objects.store {
|
||||||
|
if object.IsCollidable() {
|
||||||
|
typ := object.CollisionType()
|
||||||
|
_, ok := colliders[typ]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
colliders[typ] = append(colliders[typ], object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, resolver := range resolvers {
|
for _, resolver := range resolvers {
|
||||||
cols := GetCollisions(resolver.Resolver, colliders)
|
interests := resolver.Resolver.GetCollisionInterest()
|
||||||
|
cols := []Collision{}
|
||||||
|
for _, interest := range interests {
|
||||||
|
cols = append(cols, GetCollisions(resolver.Resolver, colliders[interest])...)
|
||||||
|
}
|
||||||
if len(cols) > 0 {
|
if len(cols) > 0 {
|
||||||
c := &Context{
|
c := &Context{
|
||||||
typ: resolveContext,
|
typ: resolveContext,
|
||||||
|
@ -458,10 +477,14 @@ func (e *Engine) Dframe() uint {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the current fixed delta time.
|
// Return the current fixed delta time.
|
||||||
func (e *Engine) Dt() Duration {
|
func (e *Engine) RealDt() Duration {
|
||||||
return e.dt
|
return e.dt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Engine) Dt() Duration {
|
||||||
|
return time.Second/60
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Engine) Frame() uint {
|
func (e *Engine) Frame() uint {
|
||||||
return e.frame
|
return e.frame
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -6,7 +6,6 @@ toolchain go1.21.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b
|
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b
|
||||||
github.com/silbinarywolf/preferdiscretegpu v1.0.0
|
|
||||||
golang.org/x/image v0.7.0
|
golang.org/x/image v0.7.0
|
||||||
vultras.su/core/gods v0.0.0-20240106154104-c280608f8de4
|
vultras.su/core/gods v0.0.0-20240106154104-c280608f8de4
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -8,8 +8,6 @@ github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b h1
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI=
|
github.com/hajimehoshi/ebiten/v2 v2.6.0-alpha.3.0.20230521122940-90562ee84b9b/go.mod h1:+fFI6Ag5YvbX1ivNQD2TxNhpWFDPuxEoew421TTQAxI=
|
||||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
github.com/silbinarywolf/preferdiscretegpu v1.0.0 h1:tuvXLRCnoFMFyk74/8PFvO0B5rjDmXm0JgNTaOYAHT0=
|
|
||||||
github.com/silbinarywolf/preferdiscretegpu v1.0.0/go.mod h1:h3s2GkfAP2sWqoS7v/PxAlFOQ1azMRsZxUJNw47QhLc=
|
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
|
BIN
media/player.png
Normal file
BIN
media/player.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.7 KiB |
Loading…
Reference in a new issue