Module: Zyps::Utility
- Defined in:
- lib/zyps.rb
Overview
Various methods for working with Vectors, etc.
Constant Summary collapse
- PI2 =
:nodoc:
Math::PI * 2.0
- @@caching_enabled =
Turn caching of return values on or off.
false
Class Method Summary collapse
- .caching_enabled=(value) ⇒ Object
-
.clear_caches ⇒ Object
Empty cached return values.
-
.collided?(object1, object2) ⇒ Boolean
Given two GameObjects, determine if the boundary of one crosses the boundary of the other.
-
.constrain_value(value, absolute_maximum) ⇒ Object
Reduce a number to within an allowed maximum (or minimum, if the number is negative).
-
.find_angle(origin, target) ⇒ Object
Get the angle (in degrees) from one Location to another.
-
.find_distance(origin, target) ⇒ Object
Get the distance from one Location to another.
-
.find_reflection_angle(normal, angle) ⇒ Object
Given a normal and an angle, find the reflection angle.
-
.to_degrees(radians) ⇒ Object
Convert radians to degrees.
-
.to_radians(degrees) ⇒ Object
Convert degrees to radians.
Class Method Details
.caching_enabled=(value) ⇒ Object
552 553 554 555 |
# File 'lib/zyps.rb', line 552 def Utility.caching_enabled= (value) @@caching_enabled = value Utility.clear_caches if ! @@caching_enabled end |
.clear_caches ⇒ Object
Empty cached return values.
542 543 544 545 |
# File 'lib/zyps.rb', line 542 def Utility.clear_caches @@angles = Hash.new {|h, k| h[k] = {}} @@distances = Hash.new {|h, k| h[k] = {}} end |
.collided?(object1, object2) ⇒ Boolean
Given two GameObjects, determine if the boundary of one crosses the boundary of the other.
639 640 641 642 643 644 |
# File 'lib/zyps.rb', line 639 def Utility.collided?(object1, object2) object1_radius = Math.sqrt(object1.size / Math::PI) object2_radius = Math.sqrt(object2.size / Math::PI) return true if find_distance(object1.location, object2.location) < object1_radius + object2_radius false end |
.constrain_value(value, absolute_maximum) ⇒ Object
Reduce a number to within an allowed maximum (or minimum, if the number is negative).
619 620 621 622 623 624 625 626 627 628 |
# File 'lib/zyps.rb', line 619 def Utility.constrain_value(value, absolute_maximum) if (value.abs > absolute_maximum) then if value >= 0 then value = absolute_maximum else value = absolute_maximum * -1 end end value end |
.find_angle(origin, target) ⇒ Object
Get the angle (in degrees) from one Location to another.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/zyps.rb', line 558 def Utility.find_angle(origin, target) if @@caching_enabled #Return cached angle if there is one. return @@angles[origin][target] if @@angles[origin][target] return @@angles[target][origin] if @@angles[target][origin] end #Get vector from origin to target. x_difference = target.x - origin.x y_difference = target.y - origin.y #Get vector's angle. radians = Math.atan2(y_difference, x_difference) #Result will range from negative Pi to Pi, so correct it. radians += PI2 if radians < 0 #Convert to degrees. angle = to_degrees(radians) #Cache angle if caching enabled. if @@caching_enabled @@angles[origin][target] = angle #angle + 180 = angle from target to origin. @@angles[target][origin] = (angle + 180 % 360) end #Return result. angle end |
.find_distance(origin, target) ⇒ Object
Get the distance from one Location to another.
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/zyps.rb', line 584 def Utility.find_distance(origin, target) if @@caching_enabled #Return cached distance if there is one. return @@distances[origin][target] if @@distances[origin][target] end #Get vector from origin to target. x_difference = origin.x - target.x y_difference = origin.y - target.y #Get distance. distance = Math.sqrt(x_difference ** 2 + y_difference ** 2) #Cache distance if caching enabled. if @@caching_enabled #Origin to target distance = target to origin distance. #Cache such that either will be found. @@distances[origin][target] = distance @@distances[target][origin] = distance end #Return result. distance end |
.find_reflection_angle(normal, angle) ⇒ Object
Given a normal and an angle, find the reflection angle.
631 632 633 634 635 636 |
# File 'lib/zyps.rb', line 631 def Utility.find_reflection_angle(normal, angle) incidence_angle = normal - angle reflection_angle = normal + incidence_angle reflection_angle %= 360 reflection_angle end |