-
Notifications
You must be signed in to change notification settings - Fork 6
/
body.rb
256 lines (220 loc) · 5.77 KB
/
body.rb
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
module CP
callback :cpBodyVelocityFunc, [:pointer, Vect.by_value, CP_FLOAT, CP_FLOAT], :void
callback :cpBodyPositionFunc, [:pointer, CP_FLOAT], :void
class BodyStruct < NiceFFI::Struct
layout(
:velocity_func, :cpBodyVelocityFunc,
:position_func, :cpBodyPositionFunc,
:m, CP_FLOAT,
:m_inv, CP_FLOAT,
:i, CP_FLOAT,
:i_inv, CP_FLOAT,
:p, Vect,
:v, Vect,
:f, Vect,
:a, CP_FLOAT,
:w, CP_FLOAT,
:t, CP_FLOAT,
:rot, Vect,
:data, :pointer,
:v_limit, CP_FLOAT,
:w_limit, CP_FLOAT,
:v_bias, Vect,
:w_bias, CP_FLOAT
)
def self.release(me)
# TODO is this right?
CP.cpBodyDestroy me
end
end
func :cpBodyNew, [CP_FLOAT, CP_FLOAT], BodyStruct
func :cpBodyDestroy, [BodyStruct], :void
func :cpBodyUpdateVelocity, [BodyStruct,Vect.by_value,CP_FLOAT,CP_FLOAT], :void
func :cpBodyUpdatePosition, [BodyStruct,CP_FLOAT], :void
func :cpBodyApplyForce, [:pointer, Vect.by_value, Vect.by_value], :void
func :cpBodyResetForces, [:pointer], :void
cp_static_inline :cpBodyLocal2World, [:pointer, Vect.by_value], Vect.by_value
cp_static_inline :cpBodyWorld2Local, [:pointer, Vect.by_value], Vect.by_value
cp_static_inline :cpBodyApplyImpulse, [:pointer, Vect.by_value, Vect.by_value], :void
func :cpBodySetMass, [:pointer, CP_FLOAT], :void
func :cpBodySetMoment, [:pointer, CP_FLOAT], :void
func :cpBodySetAngle, [:pointer, CP_FLOAT], :void
class Body
attr_reader :struct
def initialize(*args)
case args.size
when 1
@struct = args.first
when 2
ptr = CP.cpBodyNew(*args)
@struct = BodyStruct.new ptr
set_data_pointer
else
raise "wrong number of args for Body, got #{args.size}, but expected 2"
end
set_default_velocity_lambda
set_default_position_lambda
end
def m
@struct.m
end
def m=(pm)
CP.cpBodySetMass(@struct.pointer, pm)
end
alias :mass :m
alias :mass= :m=
def m_inv
@struct.m_inv
end
alias :mass_inv :m_inv
def i
@struct.i
end
def i=(pi)
CP.cpBodySetMoment(@struct.pointer, pi)
end
alias :moment :i
alias :moment= :i=
def i_inv
@struct.i_inv
end
alias :moment_inv :i_inv
def p
Vec2.new @struct.p
end
def p=(new_p)
@struct.p.pointer.put_bytes 0, new_p.struct.to_bytes, 0,Vect.size
self
end
alias :pos :p
alias :pos= :p=
def v
Vec2.new @struct.v
end
def v=(pv)
@struct.v.pointer.put_bytes 0, pv.struct.to_bytes, 0,Vect.size
self
end
alias :vel :v
alias :vel= :v=
def f
Vec2.new @struct.f
end
def f=(pf)
@struct.f.pointer.put_bytes 0, pf.struct.to_bytes, 0,Vect.size
self
end
alias :force :f
alias :force= :f=
def a
@struct.a
end
def a=(pa)
CP.cpBodySetAngle(@struct.pointer, pa)
end
alias :angle :a
alias :angle= :a=
def w
@struct.w
end
def w=(pw)
@struct.w = pw
end
alias :ang_vel :w
alias :ang_vel= :w=
def t
@struct.t
end
def t=(pt)
@struct.t = pt
end
alias :torque :t
alias :torque= :t=
def rot
Vec2.new @struct.rot
end
def v_limit
@struct.v_limit
end
def v_limit=(new_v_limit)
@struct.v_limit = new_v_limit
end
alias :vel_limit :v_limit
alias :vel_limit= :v_limit=
def w_limit
@struct.w_limit
end
def w_limit=(new_w_limit)
@struct.w_limit = new_w_limit
end
alias :ang_vel_limit :w_limit
alias :ang_vel_limit= :w_limit=
def local2world(v)
Vec2.new CP.cpBodyLocal2World(@struct.pointer,v.struct)
end
def world2local(v)
Vec2.new CP.cpBodyWorld2Local(@struct.pointer,v.struct)
end
def reset_forces
CP.cpBodyResetForces(@struct.pointer)
end
def apply_force(f,r)
CP.cpBodyApplyForce(@struct.pointer,f.struct,r.struct)
end
def apply_impulse(j,r)
CP.cpBodyApplyImpulse(@struct.pointer,j.struct,r.struct)
end
def update_velocity(g,dmp,dt)
CP.cpBodyUpdateVelocity(@struct.pointer,g.struct,dmp,dt)
end
def update_position(dt)
CP.cpBodyUpdatePosition(@struct.pointer,dt)
end
def velocity_func
@user_level_velocity_lambda
end
def velocity_func=(l)
@user_level_velocity_lambda = l
# We keep the lambda in an ivar to keep it from being GCed
@body_velocity_lambda = Proc.new do |body_ptr,g,dmp,dt|
body_struct = BodyStruct.new(body_ptr)
obj_id = body_struct.data.get_long(0)
body = ObjectSpace._id2ref(obj_id)
l.call(body,g,dmp,dt)
end
@struct.velocity_func = @body_velocity_lambda
end
def position_func
@user_level_position_lambda
end
def position_func=(l)
@user_level_position_lambda = l
# We keep the lambda in an ivar to keep it from being GCed
@body_position_lambda = Proc.new do |body_ptr,dt|
body_struct = BodyStruct.new(body_ptr)
obj_id = body_struct.data.get_long(0)
body = ObjectSpace._id2ref(obj_id)
l.call(body,dt)
end
@struct.position_func = @body_position_lambda
end
private
def set_data_pointer
mem = FFI::MemoryPointer.new(:long)
mem.put_long 0, object_id
# this is needed to prevent data corruption by GC
@body_pointer = mem
@struct.data = mem
end
def set_default_velocity_lambda
@user_level_velocity_lambda = Proc.new do |body,g,dmp,dt|
body.update_velocity(g,dmp,dt)
end
end
def set_default_position_lambda
@user_level_position_lambda = Proc.new do |body,dt|
body.update_position(dt)
end
end
end
end