Class: CP::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/chipmunk-ffi/space.rb

Defined Under Namespace

Classes: SegmentQueryInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



80
81
82
83
84
85
86
87
88
89
# File 'lib/chipmunk-ffi/space.rb', line 80

def initialize
  @struct = SpaceStruct.new(CP.cpSpaceNew)
  @static_shapes = []
  @active_shapes = []
  @bodies = []
  @constraints = []
  @blocks = {}
  @callbacks = {}
  @test_callbacks = Hash.new {|h,k| h[k] = {:begin => nil, :pre => nil, :post => nil, :sep => nil}}
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



79
80
81
# File 'lib/chipmunk-ffi/space.rb', line 79

def struct
  @struct
end

Instance Method Details

#active_shapes_hashObject



328
329
330
# File 'lib/chipmunk-ffi/space.rb', line 328

def active_shapes_hash
  SpaceHash.new(SpaceHashStruct.new(@struct.active_shapes))
end

#add_body(body) ⇒ Object



251
252
253
254
255
# File 'lib/chipmunk-ffi/space.rb', line 251

def add_body(body)
  CP.cpSpaceAddBody(@struct.pointer, body.struct.pointer)
  @bodies << body
  body
end

#add_collision_func(a, b, type = :pre, &block) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/chipmunk-ffi/space.rb', line 210

def add_collision_func(a,b,type=:pre,&block)
  arity = block.arity
  callback = Proc.new do |arb_ptr,space_ptr,data_ptr|
    arbiter = Arbiter.new(arb_ptr)
    ret = case arity
    when 1 then block.call(arbiter)
    when 2 then block.call(*arbiter.shapes)
    when 3 then block.call(arbiter,*arbiter.shapes)
    else raise ArgumentError
    end
    ret ? 1 : 0
  end
  @test_callbacks[[a,b]][type] = callback
  setup_callbacks(a,b)
end

#add_collision_handler(a, b, handler) ⇒ Object

handler should have methods [beg,pre,post,sep] defined



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chipmunk-ffi/space.rb', line 196

def add_collision_handler(a,b,handler)
  a_id = a.object_id
  b_id = b.object_id

  beg = handler.respond_to?(:begin) ? wrap_collision_callback(a, b, :begin, handler) : nil
  pre = handler.respond_to?(:pre) ? wrap_collision_callback(a, b, :pre, handler) : nil
  post = handler.respond_to?(:post) ? wrap_collision_callback(a, b, :post, handler) : nil
  sep = handler.respond_to?(:sep) ? wrap_collision_callback(a, b, :sep, handler) : nil
  data = nil

  CP.cpSpaceAddCollisionHandler(@struct.pointer, 
    a_id, b_id, beg,pre,post,sep,data)
end

#add_constraint(con) ⇒ Object



257
258
259
260
261
# File 'lib/chipmunk-ffi/space.rb', line 257

def add_constraint(con)
  CP.cpSpaceAddConstraint(@struct.pointer, con.struct.pointer)
  @constraints << con
  con
end

#add_shape(shape) ⇒ Object



239
240
241
242
243
# File 'lib/chipmunk-ffi/space.rb', line 239

def add_shape(shape)
  CP.cpSpaceAddShape(@struct.pointer, shape.struct.pointer)
  @active_shapes << shape
  shape
end

#add_static_shape(shape) ⇒ Object



245
246
247
248
249
# File 'lib/chipmunk-ffi/space.rb', line 245

def add_static_shape(shape)
  CP.cpSpaceAddStaticShape(@struct.pointer, shape.struct.pointer)
  @static_shapes << shape
  shape
end

#dampingObject



105
106
107
# File 'lib/chipmunk-ffi/space.rb', line 105

def damping
  @struct.damping
end

#damping=(damp) ⇒ Object



108
109
110
# File 'lib/chipmunk-ffi/space.rb', line 108

def damping=(damp)
  @struct.damping = damp
end

#each_body(&block) ⇒ Object



303
304
305
306
307
# File 'lib/chipmunk-ffi/space.rb', line 303

def each_body(&block)
  @bodies.each &block
#      typedef void (*cpSpaceBodyIterator)(cpBody *body, void *data);
#      void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data);
end

#elastic_iterationsObject



98
99
100
# File 'lib/chipmunk-ffi/space.rb', line 98

def elastic_iterations
  @struct.elastic_iterations
end

#elastic_iterations=(elastic_its) ⇒ Object



101
102
103
# File 'lib/chipmunk-ffi/space.rb', line 101

def elastic_iterations=(elastic_its)
  @struct.elastic_iterations = elastic_its
end

#gravityObject



112
113
114
# File 'lib/chipmunk-ffi/space.rb', line 112

def gravity
  Vec2.new @struct.gravity
end

#gravity=(v) ⇒ Object



115
116
117
# File 'lib/chipmunk-ffi/space.rb', line 115

def gravity=(v)
  @struct.gravity.pointer.put_bytes 0, v.struct.to_bytes, 0,Vect.size
end

#info_segment_query(a, b, layers = ALL_ONES, group = 0) ⇒ Object



360
361
362
# File 'lib/chipmunk-ffi/space.rb', line 360

def info_segment_query(a,b,layers=ALL_ONES,group=0)
  segment_query_first(a,b,layers,group)
end

#iterationsObject



91
92
93
# File 'lib/chipmunk-ffi/space.rb', line 91

def iterations
  @struct.iterations
end

#iterations=(its) ⇒ Object



94
95
96
# File 'lib/chipmunk-ffi/space.rb', line 94

def iterations=(its)
  @struct.iterations = its
end

#point_query(point, layers, group, &block) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/chipmunk-ffi/space.rb', line 332

def point_query(point, layers, group, &block)
  return nil unless block_given?

  query_proc = Proc.new do |shape_ptr,data|
    shape = ShapeStruct.new(shape_ptr)
    obj_id = shape.data.get_long 0
    shape = ObjectSpace._id2ref obj_id
    block.call shape
  end

  CP.cpSpacePointQuery(@struct.pointer, point.struct, layers, group,query_proc,nil)
end

#point_query_first(point, layers, group) ⇒ Object



317
318
319
320
321
322
323
324
325
326
# File 'lib/chipmunk-ffi/space.rb', line 317

def point_query_first(point, layers, group)
  shape_ptr = CP.cpSpacePointQueryFirst(@struct.pointer, point.struct, layers, group)
  if shape_ptr.null?
    nil
  else
    shape = ShapeStruct.new(shape_ptr)
    obj_id = shape.data.get_long 0
    ObjectSpace._id2ref obj_id
  end
end

#rehash_staticObject



295
296
297
# File 'lib/chipmunk-ffi/space.rb', line 295

def rehash_static
  CP.cpSpaceRehashStatic @struct.pointer
end

#remove_body(body) ⇒ Object



275
276
277
278
279
# File 'lib/chipmunk-ffi/space.rb', line 275

def remove_body(body)
  CP.cpSpaceRemoveBody(@struct.pointer, body.struct.pointer)
  @bodies.delete body
  body
end

#remove_collision_func(a, b, type = :pre) ⇒ Object



226
227
228
229
# File 'lib/chipmunk-ffi/space.rb', line 226

def remove_collision_func(a,b,type=:pre)
  @test_callbacks[[a,b]][type] = nil
  setup_callbacks(a,b)
end

#remove_constraint(con) ⇒ Object



281
282
283
284
285
# File 'lib/chipmunk-ffi/space.rb', line 281

def remove_constraint(con)
  CP.cpSpaceRemoveConstraint(@struct.pointer, con.struct.pointer)
  @constraints.delete con
  con
end

#remove_shape(shape) ⇒ Object



263
264
265
266
267
# File 'lib/chipmunk-ffi/space.rb', line 263

def remove_shape(shape)
  CP.cpSpaceRemoveShape(@struct.pointer, shape.struct.pointer)
  @active_shapes.delete shape
  shape
end

#remove_static_shape(shape) ⇒ Object



269
270
271
272
273
# File 'lib/chipmunk-ffi/space.rb', line 269

def remove_static_shape(shape)
  CP.cpSpaceRemoveStaticShape(@struct.pointer, shape.struct.pointer)
  @static_shapes.delete shape
  shape
end

#resize_active_hash(dim, count) ⇒ Object



291
292
293
# File 'lib/chipmunk-ffi/space.rb', line 291

def resize_active_hash(dim, count)
  CP.cpSpaceResizeActiveHash @struct.pointer, dim, count
end

#resize_static_hash(dim, count) ⇒ Object



287
288
289
# File 'lib/chipmunk-ffi/space.rb', line 287

def resize_static_hash(dim, count)
  CP.cpSpaceResizeStaticHash @struct.pointer, dim, count
end

#segment_query(a, b, layers, group, &block) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
# File 'lib/chipmunk-ffi/space.rb', line 380

def segment_query(a,b,layers,group,&block)
  return nil unless block_given?
  query_proc = Proc.new do |shape_ptr,t,n,data|
    shape_struct = ShapeStruct.new(shape_ptr)
    obj_id = shape_struct.data.get_long(0)
    shape = ObjectSpace._id2ref(obj_id)
    block.call(shape,t,n)
  end
  
  CP.cpSpaceSegmentQuery(@struct.pointer, a.struct, b.struct, layers, group, query_proc, nil)
end

#segment_query_first(a, b, layers, group) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/chipmunk-ffi/space.rb', line 364

def segment_query_first(a,b,layers,group)
  out_ptr = FFI::MemoryPointer.new(SegmentQueryInfoStruct.size)
  info = SegmentQueryInfoStruct.new out_ptr

  shape_ptr = CP.cpSpaceSegmentQueryFirst(@struct.pointer, a.struct.pointer, b.struct.pointer,layers,group,out_ptr)
  if shape_ptr.null?
    SegmentQueryInfo.new(false,nil,nil,nil,info)
  else
    shape_struct = ShapeStruct.new(shape_ptr)
    obj_id = shape_struct.data.get_long(0)
    shape = ObjectSpace._id2ref(obj_id)
    n_vec = Vec2.new(info.n)
    SegmentQueryInfo.new(true,shape,info.t,n_vec,info)
  end
end

#set_default_collision_func(&block) ⇒ Object

def remove_collision_func(a,b)

a_id = a.object_id
b_id = b.object_id
CP.cpSpaceRemoveCollisionHandler(@struct.pointer, a_id, b_id)
@blocks.delete [a_id,b_id]
nil

end



172
173
174
175
# File 'lib/chipmunk-ffi/space.rb', line 172

def set_default_collision_func(&block)
  raise "Not Implmented yet"
  @blocks[:default] = block 
end

#setup_callbacks(a, b) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/chipmunk-ffi/space.rb', line 231

def setup_callbacks(a,b)
  a_id = a.object_id
  b_id = b.object_id
  cbs = @test_callbacks[[a,b]]
  CP.cpSpaceAddCollisionHandler(@struct.pointer,a_id,b_id,
  cbs[:begin],cbs[:pre],cbs[:post],cbs[:sep],nil)
end

#shape_point_query(pt) ⇒ Object



309
310
311
# File 'lib/chipmunk-ffi/space.rb', line 309

def shape_point_query(pt)
  point_query_first pt, ALL_ONES, 0
end

#shape_segment_query(a, b, layers = ALL_ONES, group = 0) ⇒ Object



356
357
358
# File 'lib/chipmunk-ffi/space.rb', line 356

def shape_segment_query(a,b,layers=ALL_ONES,group=0)
  segment_query_first(a,b,layers,group).shape
end

#static_shape_point_query(pt) ⇒ Object



313
314
315
# File 'lib/chipmunk-ffi/space.rb', line 313

def static_shape_point_query(pt)
  raise "Not Implmented yet"
end

#step(dt) ⇒ Object



299
300
301
# File 'lib/chipmunk-ffi/space.rb', line 299

def step(dt)
  CP.cpSpaceStep @struct.pointer, dt
end

#wrap_collision_callback(a, b, type, handler) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chipmunk-ffi/space.rb', line 177

def wrap_collision_callback(a,b,type,handler)
  arity = handler.method(type).arity
  callback = Proc.new do |arb_ptr,space_ptr,data_ptr|
    arbiter = Arbiter.new(arb_ptr)
    
    ret = case arity
    when 1 then handler.send type, arbiter
    when 2 then handler.send type, *arbiter.shapes
    when 3 then handler.send type, arbiter, *arbiter.shapes
    else raise ArgumentError
    end
    ret ? 1 : 0
  end
  @callbacks[[a,b,type]] = [handler,callback]
  @test_callbacks[[a,b]][type] = callback
  callback
end