Class: CP::SpaceHash

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &bb_func) ⇒ SpaceHash

Returns a new instance of SpaceHash.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chipmunk-ffi/space_hash.rb', line 24

def initialize(*args, &bb_func)
  case args.size
  when 1
    @struct = args.first
  when 2
    raise "need bb func" unless block_given?
    cell_dim = args[0]
    cells = args[1]
    @struct = SpaceHashStruct.new(CP.cpSpaceHashNew(cell_dim, cells, bb_func))
  end
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



23
24
25
# File 'lib/chipmunk-ffi/space_hash.rb', line 23

def struct
  @struct
end

Instance Method Details

#cell_dimObject



37
# File 'lib/chipmunk-ffi/space_hash.rb', line 37

def cell_dim;@struct.cell_dim;end

#insert(obj, bb) ⇒ Object



39
40
41
# File 'lib/chipmunk-ffi/space_hash.rb', line 39

def insert(obj, bb)
  CP.cpSpaceHashInsert(@struct.pointer, obj.struct.pointer, obj.struct.hash_value, bb.struct)
end

#num_cellsObject



36
# File 'lib/chipmunk-ffi/space_hash.rb', line 36

def num_cells;@struct.num_cells;end

#query_by_bb(bb) ⇒ Object



55
56
57
58
59
# File 'lib/chipmunk-ffi/space_hash.rb', line 55

def query_by_bb(bb)
  @shapes = []
  CP.cpSpaceHashQuery(@struct.pointer, nil, bb.struct, query_func, nil)
  @shapes
end

#query_funcObject



47
48
49
50
51
52
53
# File 'lib/chipmunk-ffi/space_hash.rb', line 47

def query_func
  @query_func ||= Proc.new do |obj,other,data|
    s = ShapeStruct.new(other)
    obj_id = s.data.get_long 0
    @shapes <<  ObjectSpace._id2ref(obj_id)
  end
end

#remove(obj) ⇒ Object



43
44
45
# File 'lib/chipmunk-ffi/space_hash.rb', line 43

def remove(obj)
  CP.cpSpaceHashRemove(@struct.pointer, obj.struct.pointer, obj.struct.hash_value)
end