Skip to content

Commit

Permalink
make some fields public
Browse files Browse the repository at this point in the history
  • Loading branch information
setanarut committed Nov 8, 2024
1 parent 2305145 commit 459d587
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 80 deletions.
46 changes: 23 additions & 23 deletions arbiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Arbiter struct {
e, u float64
count int
state int // Arbiter state enum
contacts []Contact // a slice onto the current buffer array of contacts
Contacts []Contact // a slice onto the current buffer array of contacts
surfaceVr vec.Vec2
normal vec.Vec2
handler, handlerA, handlerB *CollisionHandler // Regular, wildcard A and wildcard B collision handlers.
Expand All @@ -40,7 +40,7 @@ func (arbiter *Arbiter) Init(a, b *Shape) *Arbiter {
arbiter.u = 0
arbiter.surfaceVr = vec.Vec2{}
arbiter.count = 0
arbiter.contacts = nil
arbiter.Contacts = nil
arbiter.shapeA = a
arbiter.bodyA = a.Body
arbiter.shapeB = b
Expand Down Expand Up @@ -108,9 +108,9 @@ func (arbiter *Arbiter) ApplyCachedImpulse(dtCoef float64) {
}

for i := 0; i < arbiter.count; i++ {
contact := arbiter.contacts[i]
contact := arbiter.Contacts[i]
j := arbiter.normal.RotateComplex(vec.Vec2{contact.jnAcc, contact.jtAcc})
applyImpulses(arbiter.bodyA, arbiter.bodyB, contact.r1, contact.r2, j.Scale(dtCoef))
applyImpulses(arbiter.bodyA, arbiter.bodyB, contact.R1, contact.R2, j.Scale(dtCoef))
}
}

Expand All @@ -122,10 +122,10 @@ func (arbiter *Arbiter) ApplyImpulse() {
friction := arbiter.u

for i := 0; i < arbiter.count; i++ {
con := &arbiter.contacts[i]
con := &arbiter.Contacts[i]
nMass := con.nMass
r1 := con.r1
r2 := con.r2
r1 := con.R1
r2 := con.R2

vb1 := a.vBias.Add(r1.Perp().Scale(a.wBias))
vb2 := b.vBias.Add(r2.Perp().Scale(b.wBias))
Expand Down Expand Up @@ -167,19 +167,19 @@ func (arb *Arbiter) PreStep(dt, slop, bias float64) {
bodyDelta := b.position.Sub(a.position)

for i := 0; i < arb.count; i++ {
con := &arb.contacts[i]
con := &arb.Contacts[i]

// Calculate the mass normal and mass tangent.
con.nMass = 1.0 / kScalar(a, b, con.r1, con.r2, n)
con.tMass = 1.0 / kScalar(a, b, con.r1, con.r2, n.Perp())
con.nMass = 1.0 / kScalar(a, b, con.R1, con.R2, n)
con.tMass = 1.0 / kScalar(a, b, con.R1, con.R2, n.Perp())

// Calculate the target bias velocity.
dist := con.r2.Sub(con.r1).Add(bodyDelta).Dot(n)
dist := con.R2.Sub(con.R1).Add(bodyDelta).Dot(n)
con.bias = -bias * math.Min(0, dist+slop) / dt
con.jBias = 0.0

// Calculate the target bounce velocity.
con.bounce = normalRelativeVelocity(a, b, con.r1, con.r2, n) * arb.e
con.bounce = normalRelativeVelocity(a, b, con.R1, con.R2, n) * arb.e
}
}

Expand All @@ -200,15 +200,15 @@ func (arb *Arbiter) Update(info *CollisionInfo, space *Space) {

// r1 and r2 store absolute offsets at init time.
// Need to convert them to relative offsets.
con.r1 = con.r1.Sub(a.Body.position)
con.r2 = con.r2.Sub(b.Body.position)
con.R1 = con.R1.Sub(a.Body.position)
con.R2 = con.R2.Sub(b.Body.position)

// Cached impulses are not zeroed at init time.
con.jnAcc = 0
con.jtAcc = 0

for j := 0; j < arb.count; j++ {
old := arb.contacts[j]
old := arb.Contacts[j]

// This could trigger false positives, but is fairly unlikely nor serious if it does.
if con.hash == old.hash {
Expand All @@ -219,7 +219,7 @@ func (arb *Arbiter) Update(info *CollisionInfo, space *Space) {
}
}

arb.contacts = info.arr[:info.count]
arb.Contacts = info.arr[:info.count]
arb.count = info.count
arb.normal = info.n

Expand Down Expand Up @@ -407,7 +407,7 @@ func (arb *Arbiter) TotalImpulse() vec.Vec2 {

count := arb.Count()
for i := 0; i < count; i++ {
con := arb.contacts[i]
con := arb.Contacts[i]
sum = sum.Add(arb.normal.RotateComplex(vec.Vec2{con.jnAcc, con.jtAcc}))
}

Expand Down Expand Up @@ -481,8 +481,8 @@ func (arb *Arbiter) ContactPointSet() ContactPointSet {

for i := 0; i < set.Count; i++ {
// Contact points are relative to body CoGs;
p1 := arb.bodyA.position.Add(arb.contacts[i].r1)
p2 := arb.bodyB.position.Add(arb.contacts[i].r2)
p1 := arb.bodyA.position.Add(arb.Contacts[i].R1)
p2 := arb.bodyB.position.Add(arb.Contacts[i].R2)

if swapped {
set.Points[i].PointA = p2
Expand Down Expand Up @@ -518,11 +518,11 @@ func (arb *Arbiter) SetContactPointSet(set *ContactPointSet) {
p2 := set.Points[i].PointB

if swapped {
arb.contacts[i].r1 = p2.Sub(arb.bodyA.position)
arb.contacts[i].r2 = p1.Sub(arb.bodyB.position)
arb.Contacts[i].R1 = p2.Sub(arb.bodyA.position)
arb.Contacts[i].R2 = p1.Sub(arb.bodyB.position)
} else {
arb.contacts[i].r1 = p1.Sub(arb.bodyA.position)
arb.contacts[i].r2 = p2.Sub(arb.bodyB.position)
arb.Contacts[i].R1 = p1.Sub(arb.bodyA.position)
arb.Contacts[i].R2 = p2.Sub(arb.bodyB.position)
}
}
}
28 changes: 14 additions & 14 deletions collision.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type SupportPointFunc func(shape *Shape, n vec.Vec2) SupportPoint

func PolySupportPoint(shape *Shape, n vec.Vec2) SupportPoint {
poly := shape.Class.(*PolyShape)
planes := poly.planes
planes := poly.Planes
i := PolySupportPointIndex(poly.count, planes, n)
return NewSupportPoint(planes[i].v0, uint32(i))
return NewSupportPoint(planes[i].V0, uint32(i))
}

func SegmentSupportPoint(shape *Shape, n vec.Vec2) SupportPoint {
Expand All @@ -49,7 +49,7 @@ func PolySupportPointIndex(count int, planes []SplittingPlane, n vec.Vec2) int {
max := -infinity
var index int
for i := 0; i < count; i++ {
v := planes[i].v0
v := planes[i].V0
d := v.Dot(n)
if d > max {
max = d
Expand Down Expand Up @@ -285,28 +285,28 @@ func SupportEdgeForSegment(seg *Segment, n vec.Vec2) Edge {

func SupportEdgeForPoly(poly *PolyShape, n vec.Vec2) Edge {
count := poly.count
i1 := PolySupportPointIndex(poly.count, poly.planes, n)
i1 := PolySupportPointIndex(poly.count, poly.Planes, n)

i0 := (i1 - 1 + count) % count
i2 := (i1 + 1) % count

planes := poly.planes
planes := poly.Planes
hashId := poly.hashid

if n.Dot(planes[i1].n) > n.Dot(planes[i2].n) {
if n.Dot(planes[i1].N) > n.Dot(planes[i2].N) {
return Edge{
EdgePoint{planes[i0].v0, HashPair(hashId, HashValue(i0))},
EdgePoint{planes[i1].v0, HashPair(hashId, HashValue(i1))},
EdgePoint{planes[i0].V0, HashPair(hashId, HashValue(i0))},
EdgePoint{planes[i1].V0, HashPair(hashId, HashValue(i1))},
poly.Radius,
planes[i1].n,
planes[i1].N,
}
}

return Edge{
EdgePoint{planes[i1].v0, HashPair(hashId, HashValue(i1))},
EdgePoint{planes[i2].v0, HashPair(hashId, HashValue(i2))},
EdgePoint{planes[i1].V0, HashPair(hashId, HashValue(i1))},
EdgePoint{planes[i2].V0, HashPair(hashId, HashValue(i2))},
poly.Radius,
planes[i2].n,
planes[i2].N,
}
}

Expand Down Expand Up @@ -536,8 +536,8 @@ func ShapesCollideInfo(a, b *Shape) ContactPointSet {
}

for i := 0; i < info.count; i++ {
p1 := contacts[i].r1
p2 := contacts[i].r2
p1 := contacts[i].R1
p2 := contacts[i].R2

if swapped {
set.Points[i].PointA = p2
Expand Down
12 changes: 6 additions & 6 deletions everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type CollisionHandler struct {
}

type Contact struct {
r1, r2 vec.Vec2
R1, R2 vec.Vec2

nMass, tMass float64
bounce float64 // TODO: look for an alternate bounce solution
Expand All @@ -99,8 +99,8 @@ type Contact struct {

func (c *Contact) Clone() Contact {
return Contact{
r1: c.r1,
r2: c.r2,
R1: c.R1,
R2: c.R2,
nMass: c.nMass,
tMass: c.tMass,
bounce: c.bounce,
Expand All @@ -125,8 +125,8 @@ type CollisionInfo struct {
func (info *CollisionInfo) PushContact(p1, p2 vec.Vec2, hash HashValue) {

con := &info.arr[info.count]
con.r1 = p1
con.r2 = p2
con.R1 = p1
con.R2 = p2
con.hash = hash

info.count++
Expand Down Expand Up @@ -165,7 +165,7 @@ type SegmentQueryInfo struct {
}

type SplittingPlane struct {
v0, n vec.Vec2
V0, N vec.Vec2
}

// ShapeFilter is fast collision filtering type that is used to determine if two objects collide before calling collision or query callbacks.
Expand Down
2 changes: 1 addition & 1 deletion hashset_arbiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func SpaceArbiterSetFilter(arb *Arbiter, space *Space) bool {
}

if ticks >= space.CollisionPersistence {
arb.contacts = nil
arb.Contacts = nil
arb.count = 0
space.pooledArbiters.Put(arb)
return false
Expand Down
8 changes: 4 additions & 4 deletions idrawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func DrawShape(shape *Shape, drawer IDrawer) {
poly := shape.Class.(*PolyShape)

count := poly.count
planes := poly.planes
planes := poly.Planes
verts := make([]vec.Vec2, count)

for i := 0; i < count; i++ {
verts[i] = planes[i].v0
verts[i] = planes[i].V0
}
drawer.DrawPolygon(count, verts, poly.Radius, outline, fill, data)
default:
Expand Down Expand Up @@ -182,8 +182,8 @@ func DrawSpace(space *Space, drawer IDrawer) {
n := arb.normal

for j := 0; j < arb.count; j++ {
p1 := arb.bodyA.position.Add(arb.contacts[j].r1)
p2 := arb.bodyB.position.Add(arb.contacts[j].r2)
p1 := arb.bodyA.position.Add(arb.Contacts[j].R1)
p2 := arb.bodyB.position.Add(arb.Contacts[j].R2)

a := p1.Add(n.Scale(-2))
b := p2.Add(n.Scale(2))
Expand Down
Loading

0 comments on commit 459d587

Please sign in to comment.