Skip to content

Commit

Permalink
#38 add basic collision resolution
Browse files Browse the repository at this point in the history
jevbelikov committed Jan 13, 2023
1 parent 0834605 commit 7e08f53
Showing 5 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/collision/main.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ func (demo *CollisionDemo) Init() error {
&systems.Controller{EntityManager: &demo.EntityManager},
&systems.Shaker{EntityManager: &demo.EntityManager},
&systems.CollisionDetection{EntityManager: &demo.EntityManager},
&systems.CollisionResolution{EntityManager: &demo.EntityManager},
&systems.DebugRenderer{EntityManager: &demo.EntityManager},
)

Binary file added docs/collision.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions game/ecs/components/collision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package components

import (
"github.com/co0p/tankism/lib/ecs"
)

const CollisionType = "Collision"

// Collision, omponent for filtering collision
type Collision struct {
Target *ecs.Entity
}

func (t Collision) Type() ecs.ComponentType {
return CollisionType
}
8 changes: 5 additions & 3 deletions game/ecs/systems/collisionDetection.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package systems

import (
"fmt"

"github.com/co0p/tankism/game/ecs/components"
"github.com/co0p/tankism/lib/ecs"
"github.com/hajimehoshi/ebiten/v2"
@@ -28,6 +26,7 @@ func (s *CollisionDetection) Update() error {
y: entityPos.Y,
width: entityDim.Width * entityPos.Scale,
height: entityDim.Height * entityPos.Scale,
e: entity,
}

boundingBoxes = append(boundingBoxes, entityBox)
@@ -42,7 +41,9 @@ func (s *CollisionDetection) Update() error {
rect2 := boundingBoxes[j]

if rect1.AABBCollision(rect2) {
fmt.Println("Collision detected!")

rect1.e.AddComponent(&components.Collision{Target: rect2.e})
rect2.e.AddComponent(&components.Collision{Target: rect1.e})
}
}
}
@@ -54,6 +55,7 @@ type boundingBox struct {
y float64
width float64
height float64
e *ecs.Entity
}

func (rect1 *boundingBox) AABBCollision(rect2 boundingBox) bool {
31 changes: 31 additions & 0 deletions game/ecs/systems/collisionResolution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package systems

import (
"github.com/co0p/tankism/game/ecs/components"
"github.com/co0p/tankism/lib/ecs"
"github.com/hajimehoshi/ebiten/v2"
)

type CollisionResolution struct {
EntityManager *ecs.EntityManager
}

func (s *CollisionResolution) Draw(screen *ebiten.Image) {}

func (s *CollisionResolution) Update() error {

entities := s.EntityManager.FindByComponents(components.CollisionType)

for _, e := range entities {
collision := e.GetComponent(components.CollisionType).(*components.Collision)
// bounce back -- basic resolution for all
// TODO resolve based on other types e.g. Health
if collision.Target.HasComponent(components.VelocityType) {
v := collision.Target.GetComponent(components.VelocityType).(*components.Velocity)
v.Intertia = -1
}
e.RemoveComponent(components.CollisionType)
}

return nil
}

0 comments on commit 7e08f53

Please sign in to comment.