make prism collision...better?

This commit is contained in:
Josh Deprez 2021-09-07 21:55:05 +10:00
parent d73715f3e2
commit dff9419d9b
2 changed files with 25 additions and 12 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"image" "image"
"log"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@ -55,20 +54,30 @@ func (m *PrismMap) CollidesWith(b Box) bool {
// Step 1: subtract whatever the translation component of PosToWorld is, // Step 1: subtract whatever the translation component of PosToWorld is,
// reducing the rest of the problem to the 3x3 submatrix. // reducing the rest of the problem to the 3x3 submatrix.
b = b.Sub(m.PosToWorld.Translation()) rb := b.Sub(m.PosToWorld.Translation())
// Step 2: invert the rest of the fucking matrix. // Step 2: invert the rest of the fucking matrix.
// (Spoilers: I did this already in Prepare) // (Spoilers: I did this already in Prepare)
b.Min = m.pwinverse.IntApply(b.Min) rb.Min = m.pwinverse.IntApply(rb.Min)
b.Max = m.pwinverse.IntApply(b.Max.Sub(Int3{1, 1, 1})) rb.Max = m.pwinverse.IntApply(rb.Max) //.Sub(Int3{1, 1, 1}))
b = b.Canon() // inverse might flip the corners around...
log.Printf("b.Max = %v", b.Max) rb = rb.Canon() // inverse might flip the corners around...
for k := b.Min.Z; k <= b.Max.Z; k++ { // Check neighboring prisms too because there's a fencepost somewhere here
for j := b.Min.Y; j <= b.Max.Y; j++ { rb.Min = rb.Min.Sub(Int3{1, 1, 1})
for i := b.Min.X; i <= b.Max.X; i++ { rb.Max = rb.Max.Add(Int3{1, 1, 1})
var pp Int3
for pp.Z = rb.Min.Z; pp.Z <= rb.Max.Z; pp.Z++ {
for pp.Y = rb.Min.Y; pp.Y <= rb.Max.Y; pp.Y++ {
for pp.X = rb.Min.X; pp.X <= rb.Max.X; pp.X++ {
// TODO: take into account the prism shape... // TODO: take into account the prism shape...
if _, found := m.Map[Int3{i, j, k}]; found { if _, found := m.Map[pp]; !found {
continue
}
// Map it back to worldspace to get a bounding box for the prism
wp := m.PosToWorld.Apply(pp)
cb := Box{Min: wp, Max: wp.Add(m.PrismSize)}
if b.Overlaps(cb) {
return true return true
} }
} }

View file

@ -141,10 +141,13 @@ func level1() *engine.Scene {
ID: "hexagons", ID: "hexagons",
DrawOrderBias: image.Pt(0, -1), // draw higher Y after lower Y DrawOrderBias: image.Pt(0, -1), // draw higher Y after lower Y
PosToWorld: engine.IntMatrix3x4{ PosToWorld: engine.IntMatrix3x4{
// For each tile in the X direction, go right by 24 and
// forward by 8, etc
0: [4]int{24, 0, 0, 0}, 0: [4]int{24, 0, 0, 0},
1: [4]int{0, 16, 0, 0}, 1: [4]int{0, 16, 0, 0},
2: [4]int{8, 0, 16, 0}, 2: [4]int{8, 0, 16, 0},
}, },
PrismSize: engine.Int3{X: 32, Y: 16, Z: 16},
Sheet: engine.Sheet{ Sheet: engine.Sheet{
CellSize: image.Pt(32, 32), CellSize: image.Pt(32, 32),
Src: engine.ImageRef{Path: "assets/hexprism32.png"}, Src: engine.ImageRef{Path: "assets/hexprism32.png"},
@ -275,6 +278,7 @@ func level1() *engine.Scene {
engine.Pt3(4, 0, 5): {}, engine.Pt3(4, 0, 5): {},
engine.Pt3(5, 0, 5): {}, engine.Pt3(5, 0, 5): {},
engine.Pt3(6, 0, 5): {}, engine.Pt3(6, 0, 5): {},
engine.Pt3(6, -1, 5): {},
engine.Pt3(7, 0, 5): {}, engine.Pt3(7, 0, 5): {},
engine.Pt3(8, 0, 5): {}, engine.Pt3(8, 0, 5): {},
engine.Pt3(9, 0, 5): {}, engine.Pt3(9, 0, 5): {},
@ -396,8 +400,8 @@ func level1() *engine.Scene {
Sprite: engine.Sprite{ Sprite: engine.Sprite{
Actor: engine.Actor{ Actor: engine.Actor{
CollisionDomain: "level_1", CollisionDomain: "level_1",
Pos: engine.Pt3(100, 0, 100), Pos: engine.Pt3(100, -32, 100),
Size: engine.Pt3(8, 16, 1), Size: engine.Pt3(8, 16, 2),
}, },
DrawOffset: image.Pt(-1, 0), DrawOffset: image.Pt(-1, 0),
Sheet: engine.Sheet{ Sheet: engine.Sheet{