Class: BulldogPhysics::BoundingSphere

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BoundingSphere

Returns a new instance of BoundingSphere.



5
6
7
8
9
10
11
12
13
14
# File 'lib/RigidBodies/rigid_collisions.rb', line 5

def initialize(*args)
  if args.size > 1
    if args[0].is_a? Vector3
      @center = args[0]
      @radius = args[1]
    elsif args[0].is_a?  BoundingSphere
      initialize_with_bounding_spheres(args[0],args[1])
    end
  end
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



3
4
5
# File 'lib/RigidBodies/rigid_collisions.rb', line 3

def center
  @center
end

#radiusObject

Returns the value of attribute radius.



3
4
5
# File 'lib/RigidBodies/rigid_collisions.rb', line 3

def radius
  @radius
end

Instance Method Details

#initialize_with_bounding_spheres(one, two) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/RigidBodies/rigid_collisions.rb', line 16

def initialize_with_bounding_spheres(one, two)
  center_offset = two.center - one.center
  distance = center_offset.squareMagnitude()
  radius_diff = two.radius - one.radius

  if radius_diff * radius_diff >= distance
    if one.radius > two.radius
      @center = one.center
      @radius = one.radius
    else
      @center = two.center
      @radius = two.radius
    end
  else
    distance = Math.sqrt(distance)
    @radius = (distance + one.radius + two.radius) * 0.5
    @center = one.center

    if distance > 0
      @center += center_offset * ((@radius - one.radius) / distance)
    end
  end

end

#overlaps(other) ⇒ Object



41
42
43
44
# File 'lib/RigidBodies/rigid_collisions.rb', line 41

def overlaps(other)
  distance_squared = (@center - other.center).squareMagnitude
  return distance_squared < (@radius + other.radius)*(@radius + other.radius)
end