From e16a0218bb947a8ef49add158aabb3b096de25bf Mon Sep 17 00:00:00 2001 From: SolarLune Date: Mon, 22 Jan 2024 17:31:15 -0800 Subject: [PATCH] Object.Center() now returns a Vector. Adding Space.WorldToSpaceVec() variant function. Renaming Space.CheckCells > Space.CheckWorld. Adding Space.CheckAreaWorldVec(). --- collision.go | 6 +++--- object.go | 13 +++++-------- shape.go | 16 +--------------- space.go | 32 ++++++++++++++++++++++++-------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/collision.go b/collision.go index e8d830f..6f1f4b1 100755 --- a/collision.go +++ b/collision.go @@ -115,10 +115,10 @@ func (cc *Collision) SlideAgainstCell(cell *Cell, avoidTags ...string) (Vector, ccX += hX ccY += hY - oX, oY := cc.checkingObject.Center() + center := cc.checkingObject.Center() - diffX := oX - ccX - diffY := oY - ccY + diffX := center.X - ccX + diffY := center.Y - ccY left := sp.Cell(collidingCell.X-1, collidingCell.Y) right := sp.Cell(collidingCell.X+1, collidingCell.Y) diff --git a/object.go b/object.go index 39fedc7..48d01f4 100755 --- a/object.go +++ b/object.go @@ -172,8 +172,8 @@ func (obj *Object) SharesCellsTags(tags ...string) bool { } // Center returns the center position of the Object. -func (obj *Object) Center() (float64, float64) { - return obj.Position.X + (obj.Size.X / 2.0), obj.Position.Y + (obj.Size.Y / 2.0) +func (obj *Object) Center() Vector { + return Vector{obj.Position.X + (obj.Size.X / 2.0), obj.Position.Y + (obj.Size.Y / 2.0)} } // SetCenter sets the Object such that its center is at the X and Y position given. @@ -184,7 +184,7 @@ func (obj *Object) SetCenter(x, y float64) { // CellPosition returns the cellular position of the Object's center in the Space. func (obj *Object) CellPosition() (int, int) { - return obj.Space.WorldToSpace(obj.Center()) + return obj.Space.WorldToSpaceVec(obj.Center()) } // SetRight sets the X position of the Object so the right edge is at the X position given. @@ -286,14 +286,11 @@ func (obj *Object) Check(dx, dy float64, tags ...string) *Collision { // ox := cc.checkingObject.X + (cc.checkingObject.W / 2) // oy := cc.checkingObject.Y + (cc.checkingObject.H / 2) - ox, oy := cc.checkingObject.Center() - oc := Vector{ox, oy} + oc := cc.checkingObject.Center() sort.Slice(cc.Objects, func(i, j int) bool { - ix, iy := cc.Objects[i].Center() - jx, jy := cc.Objects[j].Center() - return Vector{ix, iy}.Sub(oc).Magnitude() < Vector{jx, jy}.Sub(oc).Magnitude() + return cc.Objects[i].Center().Sub(oc).Magnitude() < cc.Objects[j].Center().Sub(oc).Magnitude() }) diff --git a/shape.go b/shape.go index aea76c1..df6f799 100755 --- a/shape.go +++ b/shape.go @@ -1,7 +1,6 @@ package resolv import ( - "fmt" "math" "sort" ) @@ -378,7 +377,7 @@ func (cp *ConvexPolygon) Center() Vector { pos := Vector{0, 0} for _, v := range cp.Transformed() { - pos.Add(v) + pos = pos.Add(v) } pos.X /= float64(len(cp.Transformed())) @@ -441,18 +440,6 @@ func (polygon *ConvexPolygon) Rotation() float64 { } // SetRotation sets the rotation for the ConvexPolygon; note that the rotation goes counter-clockwise from 0 to pi, and then from -pi at 180 down, back to 0. -// This can be visualized as follows: -// -// (Pi / 2) -// | -// | -// -// (Pi / -Pi) ------------- (0) -// -// | -// | -// (-Pi / 2) -// // This rotation scheme follows the way math.Atan2() works. func (polygon *ConvexPolygon) SetRotation(radians float64) { polygon.rotation = radians @@ -461,7 +448,6 @@ func (polygon *ConvexPolygon) SetRotation(radians float64) { } else if polygon.rotation < -math.Pi { polygon.rotation += math.Pi * 2 } - fmt.Println(polygon.rotation) } // Rotate is a helper function to rotate a ConvexPolygon by the radians given. diff --git a/space.go b/space.go index 3b0c20d..21ab038 100755 --- a/space.go +++ b/space.go @@ -126,9 +126,12 @@ func (sp *Space) Cell(x, y int) *Cell { } -// CheckCells checks a set of cells (from x,y to x + w, y + h in cellular coordinates) and return the first object within those Cells that contains any of the tags given. -// If no tags are given, then CheckCells will return the first Object found in any Cell. -func (sp *Space) CheckCells(x, y, w, h int, tags ...string) *Object { +// CheckCells checks a set of cells (from x,y to x + w, y + h in cellular coordinates) and returns +// a slice of the objects found within those Cells. +// The objects must have any of the tags provided (if any are provided). +func (sp *Space) CheckCells(x, y, w, h int, tags ...string) []*Object { + + res := []*Object{} for ix := x; ix < x+w; ix++ { @@ -143,13 +146,13 @@ func (sp *Space) CheckCells(x, y, w, h int, tags ...string) *Object { if cell.ContainsTags(tags...) { for _, obj := range cell.Objects { if obj.HasTags(tags...) { - return obj + res = append(res, obj) } } } } else if cell.Occupied() { - return cell.Objects[0] + res = append(res, cell.Objects...) } } @@ -158,14 +161,14 @@ func (sp *Space) CheckCells(x, y, w, h int, tags ...string) *Object { } - return nil + return res } -// CheckCellsWorld checks the cells of the Grid with the given world coordinates. +// CheckWorld checks the cells of the Grid with the given world coordinates. // Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the // position and size given. -func (sp *Space) CheckCellsWorld(x, y, w, h float64, tags ...string) *Object { +func (sp *Space) CheckWorld(x, y, w, h float64, tags ...string) []*Object { sx, sy := sp.WorldToSpace(x, y) cw, ch := sp.WorldToSpace(w, h) @@ -174,6 +177,19 @@ func (sp *Space) CheckCellsWorld(x, y, w, h float64, tags ...string) *Object { } +// CheckWorldVec checks the cells of the Grid with the given world coordinates. +// This function takes vectors for the position and size of the checked area. +// Internally, this is just syntactic sugar for calling Space.WorldToSpace() on the +// position and size given. +func (sp *Space) CheckWorldVec(pos, size Vector, tags ...string) []*Object { + + sx, sy := sp.WorldToSpace(pos.X, pos.Y) + cw, ch := sp.WorldToSpace(size.X, size.Y) + + return sp.CheckCells(sx, sy, cw, ch, tags...) + +} + // UnregisterAllObjects unregisters all Objects registered to Cells in the Space. func (sp *Space) UnregisterAllObjects() {