Class: Gosling::Polygon
Overview
A Polygon is an Actor with a shape defined by three or more vertices. Can be used to make triangles, hexagons, or any other unusual geometry not covered by the other Actors. For circles, you should use Circle. For squares or rectangles, see Rect.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Actor
#are_children_tangible, #are_children_visible, #children, #color, #is_mask, #is_tangible, #is_visible, #parent, #window
Attributes included from Transformable
Instance Method Summary collapse
-
#get_global_vertices(out = nil) ⇒ Object
Returns an array containing all of our local vertices transformed to global-space.
-
#get_vertices ⇒ Object
Returns a copy of this Polygon’s vertices (@vertices is read-only).
-
#initialize(window) ⇒ Polygon
constructor
Creates a new, square Polygon with a width and height of 1.
-
#is_point_in_bounds(point) ⇒ Object
Returns true if the point is inside of this Polygon, false otherwise.
-
#set_vertices(vertices) ⇒ Object
Sets this polygon’s vertices.
-
#set_vertices_rect(width, height) ⇒ Object
Sets this polygon to a rectangular shape with the given width and height, with its upper left at local [0, 0].
Methods inherited from Actor
#add_child, #alpha, #alpha=, #blue, #blue=, #draw, #get_actor_at, #get_actors_at, #get_global_position, #get_global_transform, #green, #green=, #has_child?, #inspect, #red, #red=, #remove_child
Methods included from Transformable
#center, #center=, #center_x, #center_x=, #center_y, #center_y=, #reset, #scale, #scale=, #scale_x, #scale_x=, #scale_y, #scale_y=, #to_matrix, transform_point, #transform_point, #translation, #translation=, untransform_point, #untransform_point, #x, #x=, #y, #y=
Constructor Details
#initialize(window) ⇒ Polygon
Creates a new, square Polygon with a width and height of 1.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gosling/polygon.rb', line 15 def initialize(window) type_check(window, Gosu::Window) super(window) @vertices = [ Snow::Vec3[0, 0, 0], Snow::Vec3[1, 0, 0], Snow::Vec3[1, 1, 0], Snow::Vec3[0, 1, 0] ] end |
Instance Method Details
#get_global_vertices(out = nil) ⇒ Object
Returns an array containing all of our local vertices transformed to global-space. (See Actor#get_global_transform.)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/gosling/polygon.rb', line 82 def get_global_vertices(out = nil) type_check(out, Array) unless out.nil? tf = MatrixCache.instance.get get_global_transform(tf) if out.nil? return @vertices.map { |v| Transformable.transform_point(tf, v, Snow::Vec3.new) } end @vertices.each_index do |i| v = @vertices[i] if out[i] Transformable.transform_point(tf, v, out[i]) else out[i] = Transformable.transform_point(tf, v) end end out ensure MatrixCache.instance.recycle(tf) if tf end |
#get_vertices ⇒ Object
Returns a copy of this Polygon’s vertices (@vertices is read-only).
29 30 31 |
# File 'lib/gosling/polygon.rb', line 29 def get_vertices @vertices.dup end |
#is_point_in_bounds(point) ⇒ Object
Returns true if the point is inside of this Polygon, false otherwise.
108 109 110 |
# File 'lib/gosling/polygon.rb', line 108 def is_point_in_bounds(point) Collision.is_point_in_shape?(point, self) end |
#set_vertices(vertices) ⇒ Object
Sets this polygon’s vertices. Requires three or more Snow::Vec2, Vec3, Vec4, or Arrays containing 2 or more numbers.
Usage:
-
polygon.set_vertices([Snow::Vec3[-1, 0, 0], Snow::Vec3[0, -1, 0], Snow::Vec3[1, 1, 0]])
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gosling/polygon.rb', line 40 def set_vertices(vertices) type_check(vertices, Array) raise ArgumentError.new("set_vertices() expects an array of at least three 2D vectors") unless vertices.length >= 3 vertices.each do |v| types_check(v, Snow::Vec2, Snow::Vec3, Snow::Vec4, Array) if v.is_a?(Array) raise ArgumentError.new("set_vertices() expects an array of at least three 2D vectors") unless v.length >= 2 v.each { |n| type_check(n, Numeric) } end end if @vertices.length < vertices.length @vertices.concat(Array.new(vertices.length - @vertices.length) { Snow::Vec3.new }) elsif @vertices.length > vertices.length @vertices.pop(@vertices.length - vertices.length) end vertices.each_index { |i| @vertices[i].set(vertices[i][0], vertices[i][1], 0) } end |
#set_vertices_rect(width, height) ⇒ Object
Sets this polygon to a rectangular shape with the given width and height, with its upper left at local [0, 0].
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gosling/polygon.rb', line 63 def set_vertices_rect(width, height) raise ArgumentError.new("Expected positive non-zero integer, but received #{width.inspect}!") unless width > 0 raise ArgumentError.new("Expected positive non-zero integer, but received #{height.inspect}!") unless height > 0 if @vertices.length < 4 @vertices.concat(Array.new(4 - @vertices.length) { Snow::Vec3.new }) elsif @vertices.length > 4 @vertices.pop(@vertices.length - 4) end @vertices[0].set( 0, 0, 0) @vertices[1].set(width, 0, 0) @vertices[2].set(width, height, 0) @vertices[3].set( 0, height, 0) end |