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!, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #ui_renderer?, #update

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#radiusObject

Returns the value of attribute radius.



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

def radius
  @radius
end

Instance Method Details

#awakeObject



9
10
11
# File 'lib/engine/physics/components/sphere_collider.rb', line 9

def awake
  @radius ||= 1
end

#collision_for(other_collider) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/engine/physics/components/sphere_collider.rb', line 21

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

#destroyObject



17
18
19
# File 'lib/engine/physics/components/sphere_collider.rb', line 17

def destroy
  Engine::Physics::PhysicsResolver.unregister_collider(self)
end

#inverse_massObject



47
48
49
# File 'lib/engine/physics/components/sphere_collider.rb', line 47

def inverse_mass
  1.0 / rigidbody.mass
end

#rigidbodyObject



55
56
57
# File 'lib/engine/physics/components/sphere_collider.rb', line 55

def rigidbody
  game_object.component(Rigidbody)
end

#startObject



13
14
15
# File 'lib/engine/physics/components/sphere_collider.rb', line 13

def start
  Engine::Physics::PhysicsResolver.register_collider(self)
end

#static?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/engine/physics/components/sphere_collider.rb', line 43

def static?
  rigidbody.nil?
end

#velocity(pos) ⇒ Object



51
52
53
# File 'lib/engine/physics/components/sphere_collider.rb', line 51

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