Class: Chipmunk::Shape::Poly

Inherits:
Object
  • Object
show all
Includes:
Chipmunk::Shape
Defined in:
ext/chipmunk/rb_cpShape.c

Instance Attribute Summary

Attributes included from Chipmunk::Shape

#obj

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chipmunk::Shape

#bb, #body, #body=, #cache_bb, #collision_type, #collision_type=, #data, #e, #e=, #group, #group=, #layers, #layers=, #nearest_point_query, #object, #object=, #point_query, #raw_bb, reset_id_counter, #segment_query, #sensor=, #sensor?, #surface_v, #surface_v=, #u, #u=

Constructor Details

#initializeObject


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/chipmunk/rb_cpShape.c', line 321

static VALUE
rb_cpPolyInitialize(int argc, VALUE * argv, VALUE self) {
  VALUE body, arr, offset;
  cpPolyShape *poly = (cpPolyShape *)SHAPE(self);

  rb_scan_args(argc, argv, "21", &body, &arr, &offset);

  RBCP_ARRAY_POINTS(arr, num, verts);

  if(!cpPolyValidate(verts, num)) {
    rb_raise(rb_eArgError, "The verts array does not from a valid polygon!");
  }

  cpPolyShapeInit(poly, BODY(body), num, verts, VGET_ZERO(offset));
  poly->shape.data           = (void *)self;
  poly->shape.collision_type = Qnil;

  rb_ivar_set(self, id_body, body);

  return self;
}

Class Method Details

.boxObject


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'ext/chipmunk/rb_cpShape.c', line 297

static VALUE
rb_cpShapeBoxInitialize(int argc, VALUE * argv, VALUE self) {
  VALUE body, widthOrBB, height;
  rb_scan_args(argc, argv, "21", &body, &widthOrBB, &height);

  VALUE newPoly = rb_cpPolyAlloc(c_cpPolyShape);
  cpPolyShape *poly = (cpPolyShape *)SHAPE(newPoly);

  if(NIL_P(height)) {
    // takes bb
    cpBoxShapeInit2(poly, BODY(body), *BBGET(widthOrBB));
  } else {
    // takes w / h
    cpBoxShapeInit(poly, BODY(body), NUM2DBL(widthOrBB), NUM2DBL(height));
  }

  poly->shape.data           = (void *)self;
  poly->shape.collision_type = Qnil;

  rb_ivar_set(newPoly, id_body, body);

  return newPoly;
}

.valid?Boolean

Returns:

  • (Boolean)

283
284
285
286
287
288
# File 'ext/chipmunk/rb_cpShape.c', line 283

static VALUE
rb_cpPolyValidate(VALUE self, VALUE arr) {
  RBCP_ARRAY_POINTS(arr, num, verts);

  return CP_INT_BOOL(cpPolyValidate(verts, num));
}

Instance Method Details

#[]Object


379
380
381
382
383
384
385
386
387
# File 'ext/chipmunk/rb_cpShape.c', line 379

static VALUE
rb_cpPolyShapeGetVert(VALUE self, VALUE vindex) {
  cpShape *shape = SHAPE(self);
  int index      = NUM2INT(vindex);
  if ((index < 0) || (index >= cpPolyShapeGetNumVerts(shape))) {
    return Qnil;
  }
  return VNEW(cpPolyShapeGetVert(shape, index));
}

#lengthObject

also include an array-ish interface


374
375
376
377
# File 'ext/chipmunk/rb_cpShape.c', line 374

static VALUE
rb_cpPolyShapeGetNumVerts(VALUE self) {
  return INT2NUM(cpPolyShapeGetNumVerts(SHAPE(self)));
}

#num_vertsObject


374
375
376
377
# File 'ext/chipmunk/rb_cpShape.c', line 374

static VALUE
rb_cpPolyShapeGetNumVerts(VALUE self) {
  return INT2NUM(cpPolyShapeGetNumVerts(SHAPE(self)));
}

#set_verts!Object


421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'ext/chipmunk/rb_cpShape.c', line 421

static VALUE
rb_cpPolyShapeSetVerts(VALUE self, VALUE arr, VALUE offset) {
  cpShape *poly = SHAPE(self);

  RBCP_ARRAY_POINTS(arr, num, verts);

  if(!cpPolyValidate(verts, num)) {
    rb_raise(rb_eArgError, "The verts array does not from a valid polygon!");
  }

  cpPolyShapeSetVerts(poly, num, verts, *VGET(offset));
  return self;
}

#sizeObject


374
375
376
377
# File 'ext/chipmunk/rb_cpShape.c', line 374

static VALUE
rb_cpPolyShapeGetNumVerts(VALUE self) {
  return INT2NUM(cpPolyShapeGetNumVerts(SHAPE(self)));
}

#vertObject


379
380
381
382
383
384
385
386
387
# File 'ext/chipmunk/rb_cpShape.c', line 379

static VALUE
rb_cpPolyShapeGetVert(VALUE self, VALUE vindex) {
  cpShape *shape = SHAPE(self);
  int index      = NUM2INT(vindex);
  if ((index < 0) || (index >= cpPolyShapeGetNumVerts(shape))) {
    return Qnil;
  }
  return VNEW(cpPolyShapeGetVert(shape, index));
}