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"
|
||||
"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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -23,5 +23,5 @@ type Text struct{
|
|||
}
|
||||
|
||||
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
|
||||
//}
|
||||
|
||||
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()
|
||||
|
|
35
engine.go
35
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
|
||||
}
|
||||
|
|
1
go.mod
1
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
|
||||
)
|
||||
|
|
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/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=
|
||||
|
|
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