Class: Chingu::GameObject

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

Overview

extend game object with gravity features

Instance Method Summary collapse

Instance Method Details

#adjust_gravity(source) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spacewar/spacewar.rb', line 105

def adjust_gravity(source)
  # Calculate vector to source
  vect_x = -(self.x - source.x)
  vect_y = -(self.y - source.y)

  # Get length of vector to source
  length = (vect_x**2 + vect_y**2)**0.5

  # Calculate unit vector
  vect_x = vect_x/length
  vect_y = vect_y/length

  # Diminish unit vector as 1/r**2
  vect_x *= (1/length ** 2)
  vect_y *= (1/length ** 2)

  # Scale to strength of gravity and apply
  self.velocity_x += vect_x * Spacewar::G_CONST
  self.velocity_y += vect_y * Spacewar::G_CONST
end