From 8fcb1c2dc5e8fa5d80084c63b3b21c2306e62f33 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 16 Jan 2024 07:37:53 +0300 Subject: [PATCH] feat: added the collision groupping to increase performance and make resolving simpler. --- cmd/test/main.go | 3 +-- cmd/test/player.go | 26 ++++++++++++++++---------- cmd/test/rect.go | 4 ++++ cmd/test/text.go | 2 +- collider.go | 13 +++++++++++-- engine.go | 35 +++++++++++++++++++++++++++++------ go.mod | 1 - go.sum | 2 -- media/player.png | Bin 0 -> 3799 bytes 9 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 media/player.png diff --git a/cmd/test/main.go b/cmd/test/main.go index 2fb2c83..938928f 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -4,7 +4,7 @@ import ( "vultras.su/core/gg" "github.com/hajimehoshi/ebiten/v2/examples/resources/images" "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts" - _ "github.com/silbinarywolf/preferdiscretegpu" + //_ "github.com/silbinarywolf/preferdiscretegpu" "bytes" "log" "fmt" @@ -95,7 +95,6 @@ func main() { } e.Spawn(txt) - //fmt.Println(rect.GetLayer(), player.GetLayer()) fmt.Println("Starting...") err = e.Run() diff --git a/cmd/test/player.go b/cmd/test/player.go index 5368126..207e3d9 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -47,6 +47,7 @@ func (p *Player) Draw(c *Context) []gg.EVertex { } func (p *Player) Update(c *Context) { + p.Position.Y += 2 if p.Spawned { return } @@ -122,6 +123,7 @@ func (p *Player) Update(c *Context) { pp := *p counter++ p.Spawned = true + p.Collidable = false 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) { - 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) - switch col.Type{ - case gg.PhysCollision : + if !p.Spawned { + fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With) + } + for _, col := range c.Collisions {switch col.Type{ + case gg.CollisionStaticPhysics : LOOP: for { - p.Position.Y -= 0.2 - shit, collides := gg.Collide(p, col.With) - fmt.Println("shit:", shit) - fmt.Println("do:", collides) + p.Position.Y -= 1 + _, collides := gg.Collide(p, col.With) if !collides { break LOOP } } - } + }} } diff --git a/cmd/test/rect.go b/cmd/test/rect.go index 9f37d55..b4e1542 100644 --- a/cmd/test/rect.go +++ b/cmd/test/rect.go @@ -24,6 +24,10 @@ func NewRect() *Rect { return ret } +func (r *Rect) CollisionType() gg.CollisionType { + return gg.CollisionStaticPhysics +} + func (r *Rect) Update(c *Context) { //r.R += 0.3 * e.DT() //r.Position = c.AbsCursorPosition() diff --git a/cmd/test/text.go b/cmd/test/text.go index bab9183..3030a07 100644 --- a/cmd/test/text.go +++ b/cmd/test/text.go @@ -23,5 +23,5 @@ type Text struct{ } func (txt *Text) Update(c *Context) { - txt.Data += string(c.Runes()) + //txt.Data += string(c.Runes()) } diff --git a/collider.go b/collider.go index d1326c2..526cd20 100644 --- a/collider.go +++ b/collider.go @@ -10,10 +10,14 @@ package gg //ColliderSimplify() Triangle //} +type CollisionMap map[CollisionType] []Collision + type CollisionType int const ( - PhysCollision CollisionType = iota - TriggerCollision + CollisionNo CollisionType = iota + CollisionStaticPhysics + CollisionTrigger + CollisionLast ) // The structure contains collision information. @@ -62,14 +66,19 @@ type Edger interface { type Resolver interface { IsResolvable() bool Resolve(c *Context) + GetCollisionInterest() []CollisionType Collider } type Resolvability struct { Resolvable bool + CollisionInterest []CollisionType } func (r Resolvability) IsResolvable() bool { return r.Resolvable } +func (r Resolvability) GetCollisionInterest() []CollisionType { + return r.CollisionInterest +} func Collide(c1, c2 Collider) (Collision, bool) { vertices := c1.Vertices() diff --git a/engine.go b/engine.go index 4c211fa..6cd1bd7 100644 --- a/engine.go +++ b/engine.go @@ -335,14 +335,18 @@ func (e *engine) Resolve() { Resolver Resolver input chan *Context } - colliders := []Collider{} + colliders := map[CollisionType] []Collider{} resolvers := []resHold{} for _, object := range e.Objects.store { - if object.IsCollidable() { - colliders = append(colliders, object) - } if object.IsResolvable() { + interests := object.GetCollisionInterest() + for _, interest := range interests { + _, ok := colliders[interest] + if !ok { + colliders[interest] = []Collider{} + } + } resolvers = append(resolvers, resHold{ object, 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 { - 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 { c := &Context{ typ: resolveContext, @@ -458,10 +477,14 @@ func (e *Engine) Dframe() uint { } // Return the current fixed delta time. -func (e *Engine) Dt() Duration { +func (e *Engine) RealDt() Duration { return e.dt } +func (e *Engine) Dt() Duration { + return time.Second/60 +} + func (e *Engine) Frame() uint { return e.frame } diff --git a/go.mod b/go.mod index 5bb1743..5178d06 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ toolchain go1.21.3 require ( 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 vultras.su/core/gods v0.0.0-20240106154104-c280608f8de4 ) diff --git a/go.sum b/go.sum index dd09f23..c5ed443 100644 --- a/go.sum +++ b/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/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= 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= 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= diff --git a/media/player.png b/media/player.png new file mode 100644 index 0000000000000000000000000000000000000000..9f1a87d2efff9bf587240b7c9bebad3d9f1fdce5 GIT binary patch literal 3799 zcmZ{nX*ARiz7{i3fo{}YGEu@H)!5B;YWGkY6_N9?TA`uxx zWY=PCGD4>8BV?HQ{!iyT=l{HV-rjTWi+j#J_uNmCjg=`HC4&M00DZ=cWCs8c0001Y zBmw{c_F82k00`xuAra4o=Pu^C1?>OwrFF2wzqwxQ&~e?(`E>G>l(PIm5&6!4Wa1>U z#7hHJGm)BLoVh3e=?)F`f5KN`dzB2h zumLcM!p0B^#z>G4pdz*YD~j3bulYI4g1EY(^HJkoV26flSpGHqmz^XHw32xmZ6#2u zsT4eJst1&21)j(+h#%I`dD7E(y1pMFV0eJbeYr*yg2mD95Y`+FJQS~U#dO2@fwM(E zH+@7#qO?6DJjcH_zalh0ndtiA8ly)LfS!2;l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