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

Class Method Summary collapse

Class Method Details

.collided?(object1, object2) ⇒ Boolean

Given two GameObjects, determine if the boundary of one crosses the boundary of the other.

Returns:

  • (Boolean)


558
559
560
561
562
563
# File 'lib/zyps.rb', line 558

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).



538
539
540
541
542
543
544
545
546
547
# File 'lib/zyps.rb', line 538

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.



503
504
505
506
507
508
509
510
511
512
513
# File 'lib/zyps.rb', line 503

def Utility.find_angle(origin, target)
	#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.
	to_degrees(radians)
end

.find_distance(origin, target) ⇒ Object

Get the distance from one Location to another.



516
517
518
519
520
521
522
# File 'lib/zyps.rb', line 516

def Utility.find_distance(origin, target)
	#Get vector from origin to target.
	x_difference = origin.x - target.x
	y_difference = origin.y - target.y
	#Get distance.
	Math.sqrt(x_difference ** 2 + y_difference ** 2)
end

.find_reflection_angle(normal, angle) ⇒ Object

Given a normal and an angle, find the reflection angle.



550
551
552
553
554
555
# File 'lib/zyps.rb', line 550

def Utility.find_reflection_angle(normal, angle)
	incidence_angle = normal - angle
	reflection_angle = normal + incidence_angle
	reflection_angle %= 360
	reflection_angle
end

.to_degrees(radians) ⇒ Object

Convert radians to degrees.



525
526
527
# File 'lib/zyps.rb', line 525

def Utility.to_degrees(radians)
	radians / PI2 * 360
end

.to_radians(degrees) ⇒ Object

Convert degrees to radians.



530
531
532
533
534
535
# File 'lib/zyps.rb', line 530

def Utility.to_radians(degrees)
	radians = degrees / 360.0 * PI2
	radians = radians % PI2
	radians += PI2 if radians < 0
	radians
end