From 459d587b7bb1d670ce0a0f17e01f6a628a82930e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F?= Date: Fri, 8 Nov 2024 06:38:59 +0300 Subject: [PATCH] make some fields public --- arbiter.go | 46 ++++++++++++++++++++++---------------------- collision.go | 28 +++++++++++++-------------- everything.go | 12 ++++++------ hashset_arbiter.go | 2 +- idrawer.go | 8 ++++---- polyshape.go | 48 +++++++++++++++++++++++----------------------- shape.go | 2 +- shape_factory.go | 2 +- space.go | 12 ++++++------ 9 files changed, 80 insertions(+), 80 deletions(-) diff --git a/arbiter.go b/arbiter.go index c416a6c..e8443ad 100644 --- a/arbiter.go +++ b/arbiter.go @@ -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. @@ -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 @@ -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)) } } @@ -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)) @@ -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 } } @@ -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 { @@ -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 @@ -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})) } @@ -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 @@ -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) } } } diff --git a/collision.go b/collision.go index f427800..cfd3ab2 100644 --- a/collision.go +++ b/collision.go @@ -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 { @@ -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 @@ -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, } } @@ -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 diff --git a/everything.go b/everything.go index de73b80..2584dd3 100644 --- a/everything.go +++ b/everything.go @@ -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 @@ -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, @@ -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++ @@ -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. diff --git a/hashset_arbiter.go b/hashset_arbiter.go index dfa79b6..1ee0ef0 100644 --- a/hashset_arbiter.go +++ b/hashset_arbiter.go @@ -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 diff --git a/idrawer.go b/idrawer.go index 6403a83..726b219 100644 --- a/idrawer.go +++ b/idrawer.go @@ -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: @@ -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)) diff --git a/polyshape.go b/polyshape.go index bf56c67..719d3ba 100644 --- a/polyshape.go +++ b/polyshape.go @@ -10,14 +10,14 @@ type PolyShape struct { *Shape Radius float64 count int - // The untransformed planes are appended at the end of the transformed planes. - planes []SplittingPlane + // The untransformed Planes are appended at the end of the transformed Planes. + Planes []SplittingPlane } func (ps *PolyShape) CacheData(transform Transform) BB { count := ps.count - dst := ps.planes[0:count] - src := ps.planes[count:] + dst := ps.Planes[0:count] + src := ps.Planes[count:] l := infinity r := -infinity @@ -25,11 +25,11 @@ func (ps *PolyShape) CacheData(transform Transform) BB { t := -infinity for i := 0; i < count; i++ { - v := transform.Apply(src[i].v0) - n := transform.ApplyVector(src[i].n) + v := transform.Apply(src[i].V0) + n := transform.ApplyVector(src[i].N) - dst[i].v0 = v - dst[i].n = n + dst[i].V0 = v + dst[i].N = n l = math.Min(l, v.X) r = math.Max(r, v.X) @@ -44,19 +44,19 @@ func (ps *PolyShape) CacheData(transform Transform) BB { func (ps *PolyShape) PointQuery(p vec.Vec2, info *PointQueryInfo) { count := ps.count - planes := ps.planes + planes := ps.Planes r := ps.Radius - v0 := planes[count-1].v0 + v0 := planes[count-1].V0 minDist := infinity closestPoint := vec.Vec2{} closestNormal := vec.Vec2{} outside := false for i := 0; i < count; i++ { - v1 := planes[i].v0 + v1 := planes[i].V0 if !outside { - outside = planes[i].n.Dot(p.Sub(v1)) > 0 + outside = planes[i].N.Dot(p.Sub(v1)) > 0 } closest := closestPointOnSegment(p, v0, v1) @@ -65,7 +65,7 @@ func (ps *PolyShape) PointQuery(p vec.Vec2, info *PointQueryInfo) { if dist < minDist { minDist = dist closestPoint = closest - closestNormal = planes[i].n + closestNormal = planes[i].N } v0 = v1 @@ -91,15 +91,15 @@ func (ps *PolyShape) PointQuery(p vec.Vec2, info *PointQueryInfo) { } func (ps *PolyShape) SegmentQuery(a, b vec.Vec2, r2 float64, info *SegmentQueryInfo) { - planes := ps.planes + planes := ps.Planes count := ps.count r := ps.Radius rsum := r + r2 for i := 0; i < count; i++ { - n := planes[i].n + n := planes[i].N an := a.Dot(n) - d := an - planes[i].v0.Dot(n) - rsum + d := an - planes[i].V0.Dot(n) - rsum if d < 0 { continue } @@ -112,8 +112,8 @@ func (ps *PolyShape) SegmentQuery(a, b vec.Vec2, r2 float64, info *SegmentQueryI point := a.Lerp(b, t) dt := n.Cross(point) - dtMin := n.Cross(planes[(i-1+count)%count].v0) - dtMax := n.Cross(planes[i].v0) + dtMin := n.Cross(planes[(i-1+count)%count].V0) + dtMax := n.Cross(planes[i].V0) if dtMin <= dt && dt <= dtMax { info.Shape = ps.Shape @@ -127,7 +127,7 @@ func (ps *PolyShape) SegmentQuery(a, b vec.Vec2, r2 float64, info *SegmentQueryI if rsum > 0 { for i := 0; i < count; i++ { circleInfo := SegmentQueryInfo{nil, b, vec.Vec2{}, 1} - CircleSegmentQuery(ps.Shape, planes[i].v0, r, a, b, r2, &circleInfo) + CircleSegmentQuery(ps.Shape, planes[i].V0, r, a, b, r2, &circleInfo) if circleInfo.Alpha < info.Alpha { *info = circleInfo } @@ -140,24 +140,24 @@ func (ps *PolyShape) Count() int { } func (ps *PolyShape) Vert(vertIndex int) vec.Vec2 { - return ps.planes[vertIndex+ps.count].v0 + return ps.Planes[vertIndex+ps.count].V0 } func (ps *PolyShape) TransformVert(i int) vec.Vec2 { - return ps.planes[i].v0 + return ps.Planes[i].V0 } func (ps *PolyShape) SetVerts(count int, verts []vec.Vec2) { ps.count = count - ps.planes = make([]SplittingPlane, count*2) + ps.Planes = make([]SplittingPlane, count*2) for i := 0; i < count; i++ { a := verts[(i-1+count)%count] b := verts[i] n := b.Sub(a).ReversePerp().Unit() - ps.planes[i+count].v0 = b - ps.planes[i+count].n = n + ps.Planes[i+count].V0 = b + ps.Planes[i+count].N = n } } diff --git a/shape.go b/shape.go index af26a7f..d871965 100644 --- a/shape.go +++ b/shape.go @@ -178,7 +178,7 @@ func (sh *Shape) Point(i uint32) SupportPoint { if i < uint32(poly.count) { index = int(i) } - return NewSupportPoint(poly.planes[index].v0, uint32(index)) + return NewSupportPoint(poly.Planes[index].V0, uint32(index)) default: return NewSupportPoint(vec.Vec2{}, 0) } diff --git a/shape_factory.go b/shape_factory.go index d8f33b3..0df192c 100644 --- a/shape_factory.go +++ b/shape_factory.go @@ -121,7 +121,7 @@ func NewPolyShapeRaw( poly := &PolyShape{ Radius: roundingRadius, count: count, - planes: []SplittingPlane{}, + Planes: []SplittingPlane{}, } poly.Shape = NewShape(poly, body, PolyShapeMassInfo(0, count, verts, roundingRadius)) poly.SetVerts(count, verts) diff --git a/space.go b/space.go index 6baa001..78716fd 100644 --- a/space.go +++ b/space.go @@ -184,11 +184,11 @@ func (s *Space) Activate(body *Body) { // If the static body is bodyB then all is good. If the static body is bodyA, that can easily be checked. if body == bodyA || bodyA.Type() == Static { numContacts := arbiter.count - contacts := arbiter.contacts + contacts := arbiter.Contacts // Restore contact values back to the space's contact buffer memory - arbiter.contacts = s.ContactBufferGetArray()[:numContacts] - copy(arbiter.contacts, contacts) + arbiter.Contacts = s.ContactBufferGetArray()[:numContacts] + copy(arbiter.Contacts, contacts) s.PushContacts(numContacts) // reinsert the arbiter into the arbiter cache @@ -232,8 +232,8 @@ func (s *Space) Deactivate(body *Body) { s.UncacheArbiter(arb) // Save contact values to a new block of memory so they won't time out contacts := make([]Contact, arb.count) - copy(contacts, arb.contacts[:arb.count]) - arb.contacts = contacts + copy(contacts, arb.Contacts[:arb.count]) + arb.Contacts = contacts } } @@ -1114,7 +1114,7 @@ func SpaceCollideShapesFunc(obj any, b *Shape, collisionId uint32, vspace any) u space.Arbiters = append(space.Arbiters, arb) } else { space.PopContacts(info.count) - arb.contacts = nil + arb.Contacts = nil arb.count = 0 // Normally arbiters are set as used after calling the post-solve callback.