Class: CP::Space
- Inherits:
-
Object
- Object
- CP::Space
- Defined in:
- lib/chipmunk-ffi/space.rb
Instance Attribute Summary collapse
-
#struct ⇒ Object
readonly
Returns the value of attribute struct.
Instance Method Summary collapse
- #active_shapes_hash ⇒ Object
- #add_body(body) ⇒ Object
- #add_collision_func(a, b, &block) ⇒ Object
-
#add_collision_handler(a, b, handler) ⇒ Object
handler should have methods [beg,pre,post,sep] defined.
- #add_constraint(con) ⇒ Object
- #add_shape(shape) ⇒ Object
- #add_static_shape(shape) ⇒ Object
- #damping ⇒ Object
- #damping=(damp) ⇒ Object
- #each_body(&block) ⇒ Object
- #elastic_iterations ⇒ Object
- #elastic_iterations=(elastic_its) ⇒ Object
- #gravity ⇒ Object
- #gravity=(v) ⇒ Object
-
#initialize ⇒ Space
constructor
A new instance of Space.
- #iterations ⇒ Object
- #iterations=(its) ⇒ Object
- #point_query(point, layers, group, &block) ⇒ Object
- #point_query_first(point, layers, group) ⇒ Object
- #rehash_static ⇒ Object
- #remove_body(body) ⇒ Object
- #remove_collision_func(a, b) ⇒ Object
- #remove_constraint(con) ⇒ Object
- #remove_shape(shape) ⇒ Object
- #remove_static_shape(shape) ⇒ Object
- #resize_active_hash(dim, count) ⇒ Object
- #resize_static_hash(dim, count) ⇒ Object
- #set_default_collision_func(&block) ⇒ Object
- #shape_point_query(pt) ⇒ Object
- #static_shape_point_query(pt) ⇒ Object
- #step(dt) ⇒ Object
- #wrap_collision_callback(a, b, type, handler) ⇒ Object
Constructor Details
#initialize ⇒ Space
Returns a new instance of Space.
76 77 78 79 80 81 82 83 84 |
# File 'lib/chipmunk-ffi/space.rb', line 76 def initialize @struct = SpaceStruct.new(CP.cpSpaceNew) @static_shapes = [] @active_shapes = [] @bodies = [] @constraints = [] @blocks = {} @callbacks = {} end |
Instance Attribute Details
#struct ⇒ Object (readonly)
Returns the value of attribute struct.
75 76 77 |
# File 'lib/chipmunk-ffi/space.rb', line 75 def struct @struct end |
Instance Method Details
#active_shapes_hash ⇒ Object
293 294 295 |
# File 'lib/chipmunk-ffi/space.rb', line 293 def active_shapes_hash SpaceHash.new(SpaceHashStruct.new(@struct.active_shapes)) end |
#add_body(body) ⇒ Object
216 217 218 219 220 |
# File 'lib/chipmunk-ffi/space.rb', line 216 def add_body(body) CP.cpSpaceAddBody(@struct.pointer, body.struct.pointer) @bodies << body body end |
#add_collision_func(a, b, &block) ⇒ Object
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 |
# File 'lib/chipmunk-ffi/space.rb', line 114 def add_collision_func(a,b,&block) beg = nil pre = nil unless block.nil? pre = Proc.new do |arb_ptr,space_ptr,data_ptr| begin arb = ArbiterStruct.new(arb_ptr) swapped = arb.swapped_col == 0 ? false : true arba = swapped ? arb.b : arb.a arbb = swapped ? arb.a : arb.b as = ShapeStruct.new(arba) a_obj_id = as.data.get_long 0 rb_a = ObjectSpace._id2ref a_obj_id bs = ShapeStruct.new(arbb) b_obj_id = bs.data.get_long 0 rb_b = ObjectSpace._id2ref b_obj_id block.call rb_a, rb_b 1 rescue Exception => ex puts ex. puts ex.backtrace 0 end end else # needed for old chipmunk style pre = Proc.new do |arb_ptr,space_ptr,data_ptr| 0 end end post = nil sep = nil data = nil a_id = a.object_id b_id = b.object_id CP.cpSpaceAddCollisionHandler(@struct.pointer, a_id, b_id, beg,pre,post,sep,data) @blocks[[a_id,b_id]] = pre nil end |
#add_collision_handler(a, b, handler) ⇒ Object
handler should have methods [beg,pre,post,sep] defined
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/chipmunk-ffi/space.rb', line 190 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
222 223 224 225 226 |
# File 'lib/chipmunk-ffi/space.rb', line 222 def add_constraint(con) CP.cpSpaceAddConstraint(@struct.pointer, con.struct.pointer) @constraints << con con end |
#add_shape(shape) ⇒ Object
204 205 206 207 208 |
# File 'lib/chipmunk-ffi/space.rb', line 204 def add_shape(shape) CP.cpSpaceAddShape(@struct.pointer, shape.struct.pointer) @active_shapes << shape shape end |
#add_static_shape(shape) ⇒ Object
210 211 212 213 214 |
# File 'lib/chipmunk-ffi/space.rb', line 210 def add_static_shape(shape) CP.cpSpaceAddStaticShape(@struct.pointer, shape.struct.pointer) @static_shapes << shape shape end |
#damping ⇒ Object
100 101 102 |
# File 'lib/chipmunk-ffi/space.rb', line 100 def damping @struct.damping end |
#damping=(damp) ⇒ Object
103 104 105 |
# File 'lib/chipmunk-ffi/space.rb', line 103 def damping=(damp) @struct.damping = damp end |
#each_body(&block) ⇒ Object
268 269 270 271 272 |
# File 'lib/chipmunk-ffi/space.rb', line 268 def each_body(&block) @bodies.each &block # typedef void (*cpSpaceBodyIterator)(cpBody *body, void *data); # void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIterator func, void *data); end |
#elastic_iterations ⇒ Object
93 94 95 |
# File 'lib/chipmunk-ffi/space.rb', line 93 def elastic_iterations @struct.elastic_iterations end |
#elastic_iterations=(elastic_its) ⇒ Object
96 97 98 |
# File 'lib/chipmunk-ffi/space.rb', line 96 def elastic_iterations=(elastic_its) @struct.elastic_iterations = elastic_its end |
#gravity ⇒ Object
107 108 109 |
# File 'lib/chipmunk-ffi/space.rb', line 107 def gravity Vec2.new @struct.gravity end |
#gravity=(v) ⇒ Object
110 111 112 |
# File 'lib/chipmunk-ffi/space.rb', line 110 def gravity=(v) @struct.gravity.pointer.put_bytes 0, v.struct.to_bytes, 0,Vect.size end |
#iterations ⇒ Object
86 87 88 |
# File 'lib/chipmunk-ffi/space.rb', line 86 def iterations @struct.iterations end |
#iterations=(its) ⇒ Object
89 90 91 |
# File 'lib/chipmunk-ffi/space.rb', line 89 def iterations=(its) @struct.iterations = its end |
#point_query(point, layers, group, &block) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/chipmunk-ffi/space.rb', line 297 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
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/chipmunk-ffi/space.rb', line 282 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_static ⇒ Object
260 261 262 |
# File 'lib/chipmunk-ffi/space.rb', line 260 def rehash_static CP.cpSpaceRehashStatic @struct.pointer end |
#remove_body(body) ⇒ Object
240 241 242 243 244 |
# File 'lib/chipmunk-ffi/space.rb', line 240 def remove_body(body) CP.cpSpaceRemoveBody(@struct.pointer, body.struct.pointer) @bodies.delete body body end |
#remove_collision_func(a, b) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/chipmunk-ffi/space.rb', line 159 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 |
#remove_constraint(con) ⇒ Object
246 247 248 249 250 |
# File 'lib/chipmunk-ffi/space.rb', line 246 def remove_constraint(con) CP.cpSpaceRemoveConstraint(@struct.pointer, con.struct.pointer) @constraints.delete con con end |
#remove_shape(shape) ⇒ Object
228 229 230 231 232 |
# File 'lib/chipmunk-ffi/space.rb', line 228 def remove_shape(shape) CP.cpSpaceRemoveShape(@struct.pointer, shape.struct.pointer) @active_shapes.delete shape shape end |
#remove_static_shape(shape) ⇒ Object
234 235 236 237 238 |
# File 'lib/chipmunk-ffi/space.rb', line 234 def remove_static_shape(shape) CP.cpSpaceRemoveStaticShape(@struct.pointer, shape.struct.pointer) @static_shapes.delete shape shape end |
#resize_active_hash(dim, count) ⇒ Object
256 257 258 |
# File 'lib/chipmunk-ffi/space.rb', line 256 def resize_active_hash(dim, count) CP.cpSpaceResizeActiveHash @struct.pointer, dim, count end |
#resize_static_hash(dim, count) ⇒ Object
252 253 254 |
# File 'lib/chipmunk-ffi/space.rb', line 252 def resize_static_hash(dim, count) CP.cpSpaceResizeStaticHash @struct.pointer, dim, count end |
#set_default_collision_func(&block) ⇒ Object
167 168 169 170 |
# File 'lib/chipmunk-ffi/space.rb', line 167 def set_default_collision_func(&block) raise "Not Implmented yet" @blocks[:default] = block end |
#shape_point_query(pt) ⇒ Object
274 275 276 |
# File 'lib/chipmunk-ffi/space.rb', line 274 def shape_point_query(pt) point_query_first pt, ALL_ONES, 0 end |
#static_shape_point_query(pt) ⇒ Object
278 279 280 |
# File 'lib/chipmunk-ffi/space.rb', line 278 def static_shape_point_query(pt) raise "Not Implmented yet" end |
#step(dt) ⇒ Object
264 265 266 |
# File 'lib/chipmunk-ffi/space.rb', line 264 def step(dt) CP.cpSpaceStep @struct.pointer, dt end |
#wrap_collision_callback(a, b, type, handler) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/chipmunk-ffi/space.rb', line 172 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] callback end |