/
arbiter.go
199 lines (165 loc) · 6.67 KB
/
arbiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package chipmunk
/*
Copyright © 2012 Serge Zirukin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// #include <chipmunk/chipmunk.h>
import "C"
import (
"fmt"
"unsafe"
)
////////////////////////////////////////////////////////////////////////////////
// Arbiter is a type of colliding pair of shapes.
type Arbiter uintptr
// ContactPoint is a contact point type of collision.
type ContactPoint struct {
// Point is position normal of the contact point.
Point Vect
// Normal is the normal of the contact point.
Normal Vect
// Dist is the depth of the contact point.
Dist float64
}
////////////////////////////////////////////////////////////////////////////////
// Bodies returns the colliding bodies involved for this arbiter.
// The order of the CollisionType the bodies are associated with values will match
// the order set when the collision handler was registered.
func (arb Arbiter) Bodies() (Body, Body) {
var a, b *C.cpBody
C.cpArbiterGetBodies(arb.c(), (**C.cpBody)(unsafe.Pointer(&a)), (**C.cpBody)(unsafe.Pointer(&b)))
return cpBody(a), cpBody(b)
}
// ContactPoints returns a contact set from an arbiter.
func (a Arbiter) ContactPoints() []ContactPoint {
set := C.cpArbiterGetContactPointSet(a.c())
c := make([]ContactPoint, int(set.count))
for i := range c {
c[i] = ContactPoint{
cpVect(set.points[i].point),
cpVect(set.points[i].normal),
float64(set.points[i].dist),
}
}
return c
}
// SetContactPoints replaces the contact point set for an arbiter.
// This can be a very powerful feature, but use it with caution!
func (a Arbiter) SetContactPoints(cp []ContactPoint) {
if len(cp) > C.CP_MAX_CONTACTS_PER_ARBITER {
cp = cp[:C.CP_MAX_CONTACTS_PER_ARBITER]
}
set := &C.cpContactPointSet{count: C.int(len(cp))}
for i := range cp {
set.points[i].point = cp[i].Point.c()
set.points[i].normal = cp[i].Normal.c()
set.points[i].dist = C.cpFloat(cp[i].Dist)
}
C.cpArbiterSetContactPointSet(a.c(), set)
}
// Count returns the number of contact points for this arbiter.
func (a Arbiter) Count() int {
return int(C.cpArbiterGetCount(a.c()))
}
// Depth returns the depth of specific contact point.
func (a Arbiter) Depth(i int) float64 {
return float64(C.cpArbiterGetDepth(a.c(), C.int(i)))
}
// Elasticity returns a calculated value to use for the elasticity coefficient.
// Override in a pre-solve collision handler for custom behavior.
func (a Arbiter) Elasticity() float64 {
return float64(C.cpArbiterGetElasticity(a.c()))
}
// Friction returns a calculated value to use for the friction coefficient.
// Override in a pre-solve collision handler for custom behavior.
func (a Arbiter) Friction() float64 {
return float64(C.cpArbiterGetFriction(a.c()))
}
// Ignore causes a collision pair to be ignored as if you returned false from a begin callback.
// If called from a pre-step callback, you will still need to return false
// if you want it to be ignored in the current step.
func (a Arbiter) Ignore() {
C.cpArbiterIgnore(a.c())
}
// IsFirstContact returns true if this is the first step a pair of objects started colliding.
func (a Arbiter) IsFirstContact() bool {
return cpBool(C.cpArbiterIsFirstContact(a.c()))
}
// Normal returns the normal of specific contact point.
func (a Arbiter) Normal(i int) Vect {
return cpVect(C.cpArbiterGetNormal(a.c(), C.int(i)))
}
// Point returns the position of specific contact point.
func (a Arbiter) Point(i int) Vect {
return cpVect(C.cpArbiterGetPoint(a.c(), C.int(i)))
}
// SetElasticity sets elasticity coefficient.
func (a Arbiter) SetElasticity(e float64) {
C.cpArbiterSetElasticity(a.c(), C.cpFloat(e))
}
// SetFriction sets friction coefficient.
func (a Arbiter) SetFriction(f float64) {
C.cpArbiterSetFriction(a.c(), C.cpFloat(f))
}
// SetSurfaceVelocity sets calculated value to use for applying surface velocities.
func (a Arbiter) SetSurfaceVelocity(v Vect) {
C.cpArbiterSetSurfaceVelocity(a.c(), v.c())
}
// Shapes returns the colliding shapes involved for this arbiter.
// The order of their CollisionType values will match the order set when the collision
// handler was registered.
func (arb Arbiter) Shapes() (Shape, Shape) {
var a, b *C.cpShape
C.cpArbiterGetShapes(arb.c(), (**C.cpShape)(unsafe.Pointer(&a)), (**C.cpShape)(unsafe.Pointer(&b)))
return cpShape(a), cpShape(b)
}
// String converts an arbiter to a human-readable string.
func (a Arbiter) String() string {
return fmt.Sprintf("(Arbiter)%+v", a.c())
}
// SurfaceVelocity returns a calculated value to use for applying surface velocities.
// Override in a pre-solve collision handler for custom behavior.
func (a Arbiter) SurfaceVelocity() Vect {
return cpVect(C.cpArbiterGetSurfaceVelocity(a.c()))
}
// TotalImpulse returns the total impulse that was applied by this arbiter.
// This function should only be called from a post-solve, post-step or
// body.EachArbiter callback.
func (a Arbiter) TotalImpulse() Vect {
return cpVect(C.cpArbiterTotalImpulse(a.c()))
}
// TotalImpulseWithFriction returns the total impulse including the friction that
// was applied by this arbiter. This function should only be called from a post-solve,
// post-step or body.EachArbiter callback.
func (a Arbiter) TotalImpulseWithFriction() Vect {
return cpVect(C.cpArbiterTotalImpulseWithFriction(a.c()))
}
// TotalKE returns the amount of energy lost in a collision including static,
// but not dynamic friction. This function should only be called from a post-solve,
// post-step or body.EachArbiter callback.
func (a Arbiter) TotalKE() float64 {
return float64(C.cpArbiterTotalKE(a.c()))
}
// c converts Arbiter to c.cpArbiter pointer.
func (a Arbiter) c() *C.cpArbiter {
return (*C.cpArbiter)(unsafe.Pointer(a))
}
// cpArbiter converts C.cpArbiter pointer to Arbiter.
func cpArbiter(a *C.cpArbiter) Arbiter {
return Arbiter(unsafe.Pointer(a))
}