Skip to content

Commit

Permalink
Object.Center() now returns a Vector.
Browse files Browse the repository at this point in the history
Adding Space.WorldToSpaceVec() variant function.
Renaming Space.CheckCells > Space.CheckWorld.
Adding Space.CheckAreaWorldVec().
  • Loading branch information
SolarLune committed Jan 23, 2024
1 parent e2864dc commit e16a021
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
6 changes: 3 additions & 3 deletions collision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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()

})

Expand Down
16 changes: 1 addition & 15 deletions shape.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resolv

import (
"fmt"
"math"
"sort"
)
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
32 changes: 24 additions & 8 deletions space.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {

Expand All @@ -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...)
}

}
Expand All @@ -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)
Expand All @@ -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() {

Expand Down

0 comments on commit e16a021

Please sign in to comment.