Class: Chipmunk::BB

Inherits:
Object
  • Object
show all
Defined in:
ext/chipmunk/rb_cpBB.c

Instance Method Summary collapse

Constructor Details

#initializeObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/chipmunk/rb_cpBB.c', line 43

static VALUE
rb_cpBBInitialize(int argc, VALUE *argv, VALUE self) {
  VALUE l, b, r, t;
  cpBB *bb = BBGET(self);

  rb_scan_args(argc, argv, "04", &l, &b, &r, &t);
  // initialize as a circle bounds box if ony 2 params
  if (NIL_P(r)) {
    if(NIL_P(l)) { 
       (*bb) = cpBBNew(0, 0, 1, 1); // unit box. 
    } else {
       cpVect * p =  VGET(l);
       (*bb) = cpBBNewForCircle(*p, NUM2DBL(b));
    }   
  } else {
   (*bb)  = cpBBNew(NUM2DBL(l), NUM2DBL(b), NUM2DBL(r), NUM2DBL(t));
  }  
  return self;
}

Instance Method Details

#bObject


147
148
149
150
# File 'ext/chipmunk/rb_cpBB.c', line 147

static VALUE
rb_cpBBGetB(VALUE self) {
  return rb_float_new(BBGET(self)->b);
}

#b=Object


168
169
170
171
172
# File 'ext/chipmunk/rb_cpBB.c', line 168

static VALUE
rb_cpBBSetB(VALUE self, VALUE val) {
  BBGET(self)->b = NUM2DBL(val);
  return val;
}

#contain_bb?Boolean

static VALUE rb_cpBBintersects(VALUE self, VALUE a, VALUE b) {

cpVect *a, *b;
if(IS_NIL(b)) { // box/box intersect
  return CP_INT_BOOL(cpBBIntersects(*BBGET(self), *BBGET(a)));
}
// If we get here it's a box/segment intersect
a = VGET(va);
b = VGET(vb);
if(a && b) {
  return CP_INT_BOOL(cpBBIntersectsSegment(*BBGET(self), *a, *b));
}
rb_raise(rb_eArgError, "intersects needs 1 Box or 2 Vect2 arguments");
return Qnil;

}

Returns:

  • (Boolean)

81
82
83
84
85
# File 'ext/chipmunk/rb_cpBB.c', line 81

static VALUE
rb_cpBBContainsBB(VALUE self, VALUE other) {
  int value = cpBBContainsBB(*BBGET(self), *BBGET(other));
  return CP_INT_BOOL(value);
}

#contain_vect?Boolean

Returns:

  • (Boolean)

87
88
89
90
91
# File 'ext/chipmunk/rb_cpBB.c', line 87

static VALUE
rb_cpBBContainsVect(VALUE self, VALUE other) {
  int value = cpBBContainsVect(*BBGET(self), *VGET(other));
  return CP_INT_BOOL(value);
}

#contains_bb?Boolean

static VALUE rb_cpBBintersects(VALUE self, VALUE a, VALUE b) {

cpVect *a, *b;
if(IS_NIL(b)) { // box/box intersect
  return CP_INT_BOOL(cpBBIntersects(*BBGET(self), *BBGET(a)));
}
// If we get here it's a box/segment intersect
a = VGET(va);
b = VGET(vb);
if(a && b) {
  return CP_INT_BOOL(cpBBIntersectsSegment(*BBGET(self), *a, *b));
}
rb_raise(rb_eArgError, "intersects needs 1 Box or 2 Vect2 arguments");
return Qnil;

}

Returns:

  • (Boolean)

81
82
83
84
85
# File 'ext/chipmunk/rb_cpBB.c', line 81

static VALUE
rb_cpBBContainsBB(VALUE self, VALUE other) {
  int value = cpBBContainsBB(*BBGET(self), *BBGET(other));
  return CP_INT_BOOL(value);
}

#contains_vect?Boolean

Returns:

  • (Boolean)

87
88
89
90
91
# File 'ext/chipmunk/rb_cpBB.c', line 87

static VALUE
rb_cpBBContainsVect(VALUE self, VALUE other) {
  int value = cpBBContainsVect(*BBGET(self), *VGET(other));
  return CP_INT_BOOL(value);
}

#intersect?Boolean

Returns:

  • (Boolean)

116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/chipmunk/rb_cpBB.c', line 116

static VALUE
rb_cpBBintersects(int argc, VALUE *argv, VALUE self) {
  VALUE other, b;

  rb_scan_args(argc, argv, "11", &other, &b);

  if (rb_class_of(other) == c_cpBB) {
    return rb_cpBBIntersectsBB(self, other);
  } else if ((rb_class_of(other) == c_cpVect) && (rb_class_of(b) == c_cpVect)) {
    return rb_cpBBIntersectsSegment(self, other, b);
  }
  rb_raise(rb_eArgError, "contains requires a BB or 2 Vect2 arguments");
  return Qnil;
}

#intersect_bb?Boolean

Returns:

  • (Boolean)

104
105
106
107
108
# File 'ext/chipmunk/rb_cpBB.c', line 104

static VALUE
rb_cpBBIntersectsBB(VALUE self, VALUE other) {
  int value = cpBBIntersects(*BBGET(self), *BBGET(other));
  return CP_INT_BOOL(value);
}

#intersect_segment?Boolean

Returns:

  • (Boolean)

110
111
112
113
114
# File 'ext/chipmunk/rb_cpBB.c', line 110

static VALUE
rb_cpBBIntersectsSegment(VALUE self, VALUE a, VALUE b) {
  int value = cpBBIntersectsSegment(*BBGET(self), *VGET(a), *VGET(b));
  return CP_INT_BOOL(value);
}

#intersects?Boolean

Returns:

  • (Boolean)

116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/chipmunk/rb_cpBB.c', line 116

static VALUE
rb_cpBBintersects(int argc, VALUE *argv, VALUE self) {
  VALUE other, b;

  rb_scan_args(argc, argv, "11", &other, &b);

  if (rb_class_of(other) == c_cpBB) {
    return rb_cpBBIntersectsBB(self, other);
  } else if ((rb_class_of(other) == c_cpVect) && (rb_class_of(b) == c_cpVect)) {
    return rb_cpBBIntersectsSegment(self, other, b);
  }
  rb_raise(rb_eArgError, "contains requires a BB or 2 Vect2 arguments");
  return Qnil;
}

#intersects_bb?Boolean

Returns:

  • (Boolean)

104
105
106
107
108
# File 'ext/chipmunk/rb_cpBB.c', line 104

static VALUE
rb_cpBBIntersectsBB(VALUE self, VALUE other) {
  int value = cpBBIntersects(*BBGET(self), *BBGET(other));
  return CP_INT_BOOL(value);
}

#intersects_segment?Boolean

Returns:

  • (Boolean)

110
111
112
113
114
# File 'ext/chipmunk/rb_cpBB.c', line 110

static VALUE
rb_cpBBIntersectsSegment(VALUE self, VALUE a, VALUE b) {
  int value = cpBBIntersectsSegment(*BBGET(self), *VGET(a), *VGET(b));
  return CP_INT_BOOL(value);
}

#lObject


142
143
144
145
# File 'ext/chipmunk/rb_cpBB.c', line 142

static VALUE
rb_cpBBGetL(VALUE self) {
  return rb_float_new(BBGET(self)->l);
}

#l=Object


162
163
164
165
166
# File 'ext/chipmunk/rb_cpBB.c', line 162

static VALUE
rb_cpBBSetL(VALUE self, VALUE val) {
  BBGET(self)->l = NUM2DBL(val);
  return val;
}

#rObject


152
153
154
155
# File 'ext/chipmunk/rb_cpBB.c', line 152

static VALUE
rb_cpBBGetR(VALUE self) {
  return rb_float_new(BBGET(self)->r);
}

#r=Object


174
175
176
177
178
# File 'ext/chipmunk/rb_cpBB.c', line 174

static VALUE
rb_cpBBSetR(VALUE self, VALUE val) {
  BBGET(self)->r = NUM2DBL(val);
  return val;
}

#segment_queryObject

/ Returns the fraction along the segment query the cpBB is hit. Returns INFINITY if it doesn't hit.


217
218
219
220
221
222
223
224
225
226
227
# File 'ext/chipmunk/rb_cpBB.c', line 217

static VALUE
rb_cpBBSegmentQuery(VALUE self, VALUE va, VALUE vb) {
  cpVect *a, *b;
  a = VGET(va);
  b = VGET(vb);
  if(a && b) {
    return DBL2NUM(cpBBSegmentQuery(*BBGET(self), *a, *b));
  }
  rb_raise(rb_eArgError, "query requires 2 Vect2 arguments");
  return Qnil; 
}

#tObject


157
158
159
160
# File 'ext/chipmunk/rb_cpBB.c', line 157

static VALUE
rb_cpBBGetT(VALUE self) {
  return rb_float_new(BBGET(self)->t);
}

#t=Object


180
181
182
183
184
# File 'ext/chipmunk/rb_cpBB.c', line 180

static VALUE
rb_cpBBSetT(VALUE self, VALUE val) {
  BBGET(self)->t = NUM2DBL(val);
  return val;
}

#to_sObject


186
187
188
189
190
191
192
193
194
# File 'ext/chipmunk/rb_cpBB.c', line 186

static VALUE
rb_cpBBToString(VALUE self) {
  char str[256];
  cpBB *bb = BBGET(self);

  sprintf(str, "#<CP::BB:(% .3f, % .3f) -> (% .3f, % .3f)>", bb->l, bb->b, bb->r, bb->t);

  return rb_str_new2(str);
}