Class: Engine::Physics::Components::SphereCollider

Inherits:
Component
  • Object
show all
Defined in:
lib/engine/physics/components/sphere_collider.rb

Instance Attribute Summary collapse

Attributes inherited from Component

#game_object

Instance Method Summary collapse

Methods inherited from Component

#_erase!, #destroy, #destroy!, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #start, #ui_renderer?, #update

Constructor Details

#initialize(radius) ⇒ SphereCollider

Returns a new instance of SphereCollider.



7
8
9
# File 'lib/engine/physics/components/sphere_collider.rb', line 7

def initialize(radius)
  @radius = radius
end

Instance Attribute Details

#radiusObject

Returns the value of attribute radius.



5
6
7
# File 'lib/engine/physics/components/sphere_collider.rb', line 5

def radius
  @radius
end

Instance Method Details

#collision_for(other_collider) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/engine/physics/components/sphere_collider.rb', line 11

def collision_for(other_collider)
  distance = (game_object.pos - other_collider.game_object.pos).magnitude
  min_distance = radius + other_collider.radius

  return nil if distance >= min_distance

  direction = (other_collider.game_object.pos - game_object.pos).normalize
  collision_point = collision_point(direction, min_distance - distance)
  magnitude = normal_impulse_magnitude(direction, other_collider, collision_point)

  return nil if magnitude <= 0

  normal_impulse = -direction * magnitude
  tangential_impulse = tangential_impulse(other_collider, normal_impulse, collision_point)

  puts "normal_impulse: #{normal_impulse}"
  puts "tangential_impulse: #{tangential_impulse}"
  puts "total impulse: #{normal_impulse + tangential_impulse}"

  Engine::Physics::Collision.new(normal_impulse + tangential_impulse, collision_point)
end

#inverse_massObject



37
38
39
# File 'lib/engine/physics/components/sphere_collider.rb', line 37

def inverse_mass
  1.0 / rigidbody.mass
end

#rigidbodyObject



45
46
47
# File 'lib/engine/physics/components/sphere_collider.rb', line 45

def rigidbody
  game_object.components.find { |c| c.is_a?(Rigidbody) }
end

#static?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/engine/physics/components/sphere_collider.rb', line 33

def static?
  rigidbody.nil?
end

#velocity(pos) ⇒ Object



41
42
43
# File 'lib/engine/physics/components/sphere_collider.rb', line 41

def velocity(pos)
  rigidbody&.velocity_at_point(pos) || Vector[0, 0, 0]
end