Class: BulldogPhysics::BVHNode

Inherits:
Object
  • Object
show all
Defined in:
lib/RigidBodies/rigid_collisions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBVHNode



191
192
193
194
# File 'lib/RigidBodies/rigid_collisions.rb', line 191

def initialize()
  @children = Array.new
  @volume = BHVNode.new
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



189
190
191
# File 'lib/RigidBodies/rigid_collisions.rb', line 189

def body
  @body
end

#childrenObject

Returns the value of attribute children.



189
190
191
# File 'lib/RigidBodies/rigid_collisions.rb', line 189

def children
  @children
end

#parentObject

Returns the value of attribute parent.



189
190
191
# File 'lib/RigidBodies/rigid_collisions.rb', line 189

def parent
  @parent
end

#volumeObject

Returns the value of attribute volume.



189
190
191
# File 'lib/RigidBodies/rigid_collisions.rb', line 189

def volume
  @volume
end

Instance Method Details

#destroy_nodeObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/RigidBodies/rigid_collisions.rb', line 259

def destroy_node
  if !@parent.nil?
    if @parent.children[0].eql? self
      sibling = @parent.children[1]
    else
      sibling = @parent.children[0]
    end

    @parent.volume = sibling.volume
    @parent.body = sibling.volume
    @parent.children[0] = sibling.children[0]
    @parent.children[1] = sibling.children[1]


    sibling.parent = nil
    sibling.body = nil
    sibling.children = nil

    @parent.recalculate_bounding_volume()
  end

  if( @children[0])
    @children[0].parent = nil
  end

  if( @children[1])
    @children[1].parent = nil
  end

end

#get_potential_contacts(contacts, limit) ⇒ Object



200
201
202
203
204
# File 'lib/RigidBodies/rigid_collisions.rb', line 200

def get_potential_contacts(contacts, limit)
  return if limit == 0 or is_leaf

  @children[0].get_potential_contacts_with(@children[1], contacts, limit)
end

#get_potential_contacts_with(other, contacts, limit) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/RigidBodies/rigid_collisions.rb', line 206

def get_potential_contacts_with(other, contacts,limit)
  return if !overlaps(other) or limit == 0

  if is_leaf or other.is_leaf
    contacts.body[0] = @body
    contacts.body[1] = other.body
  end

  if other.is_leaf or (!is_leaf and @volume.get_size >= other.volume.get_size)

    count = @children[0].get_potential_contacts_with(other,contacts,limit)

    if limit > count
      return count + @children.get_potential_contacts_with(other, contacts[count], limit - count)
    else
      return count
    end

  else
    count = get_potential_contacts_with(other.children[0], contacts, limit)

    if limit > count
      return count + get_potential_contacts_with(other.children[1], contacts[count], limit - count)
    else
      return count
    end

  end
end

#insert(new_body, new_volume) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/RigidBodies/rigid_collisions.rb', line 237

def insert(new_body, new_volume)
  
  if is_leaf
    @children[0] = BHVNode.new(self, @volume, @body)
    @children[1] = BHVNode.new(self, new_volume, new_body)

    self.body = nil

    recalculate_bounding_volume()
  else
    if @children[0].volume.get_growth(new_volume) < @children[1].get_growth(new_volume)
      @children[0].insert(new_body, new_volume)
    else
      @children[1].insert(new_body, new_volume)
    end
  end
end

#is_leafObject



196
197
198
# File 'lib/RigidBodies/rigid_collisions.rb', line 196

def is_leaf
  !@body.nil?
end

#overlaps(other) ⇒ Object



255
256
257
# File 'lib/RigidBodies/rigid_collisions.rb', line 255

def overlaps(other)
  @volume.overlaps(other.volume)
end