Class: Metro::UI::PhysicsSprite
- Defined in:
- lib/metro/models/ui/physics_sprite.rb
Overview
A physics sprite is a Metro model that is specially designed to draw and manage an image. A sprite maintains an image, location information, and rotation. It also has a physics body and shape to assist with being placed within a ‘Metro::UI::Space`.
Constant Summary
Constants included from Metro::Units
Instance Attribute Summary collapse
-
#debug ⇒ Object
When this value is true the bounding box will be placed around the sprite.
-
#mass ⇒ Object
The mass specified here is given to the body.
-
#moment_of_interia ⇒ Object
The moment of inertia determines how the sprite will react to the forces applied to it.
-
#shape_name ⇒ Object
The name of the shape.
-
#shape_size ⇒ Object
A physics sprite has by default a collision shape that is a square.
Attributes inherited from Sprite
#angle, #center_x, #center_y, #color, #dimensions, #image, #position, #scale
Attributes inherited from Model
Instance Method Summary collapse
-
#body ⇒ Object
The body of the physics sprite that is created with the mass and moment of interia specified in the other properties.
-
#draw ⇒ Object
On draw, draw the specified sprite.
- #draw_bounding_box ⇒ Object
-
#push(x_amount, y_amount) ⇒ Object
An helper method that makes it easy to apply an impulse to the body at the center of the body.
-
#shape ⇒ Object
A polygon shape for the physics sprite with the size based on the ‘shape_size` property value.
-
#show ⇒ Object
Upon the scene start the body is assigned the x and y position.
-
#update ⇒ Object
On update track the position of the sprite based on the position of the physics body.
Methods inherited from Sprite
#bottom, #bounds, #left, #right, #top
Methods inherited from Model
#_load, #_save, #after_initialize, #bounds, #create, #draw_completed?, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #to_hash, #update_completed?
Methods included from HasEvents
Methods included from KeyValueCoding
Methods included from PropertyOwner
Constructor Details
This class inherits a constructor from Metro::Model
Instance Attribute Details
#debug ⇒ Object
When this value is true the bounding box will be placed around the sprite. This is useful in determining correct sizes and collisions.
36 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 36 property :debug, type: :boolean, default: false |
#mass ⇒ Object
The mass specified here is given to the body.
14 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 14 property :mass, default: 10 |
#moment_of_interia ⇒ Object
The moment of inertia determines how the sprite will react to the forces applied to it.
19 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 19 property :moment_of_interia, default: 1000000 |
#shape_name ⇒ Object
The name of the shape. This name is important when the space defines actions based on the collision of particular objects. By default all physics sprites are named ‘object’.
31 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 31 property :shape_name, type: :text, default: "object" |
#shape_size ⇒ Object
A physics sprite has by default a collision shape that is a square. So this property defines the length of one side which is used to create the appropriately sized shape for the sprite.
25 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 25 property :shape_size, default: 48.0 |
Instance Method Details
#body ⇒ Object
Returns the body of the physics sprite that is created with the mass and moment of interia specified in the other properties.
40 41 42 43 44 45 46 47 48 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 40 def body @body ||= begin body = CP::Body.new(mass,moment_of_interia) body.p = CP::Vec2::ZERO body.v = CP::Vec2::ZERO body.a = 0 body end end |
#draw ⇒ Object
On draw, draw the specified sprite.
90 91 92 93 94 95 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 90 def draw angle_in_degrees = body.a.radians.to_degrees image.draw_rot(x,y,z_order,angle_in_degrees) draw_bounding_box if debug end |
#draw_bounding_box ⇒ Object
97 98 99 100 101 102 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 97 def draw_bounding_box @bounding_box_border ||= create "metro::ui::border" @bounding_box_border.position = Point.at(position.x - shape_size,shape.bb.t) @bounding_box_border.dimensions = Dimensions.of(shape.bb.r - shape.bb.l,shape.bb.b - shape.bb.t) @bounding_box_border.draw end |
#push(x_amount, y_amount) ⇒ Object
An helper method that makes it easy to apply an impulse to the body at the center of the body. The impulse provided is in two parameters, the x and y component.
72 73 74 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 72 def push(x_amount,y_amount) body.apply_impulse(CP::Vec2.new(x_amount,y_amount),CP::Vec2.new(0.0, 0.0)) end |
#shape ⇒ Object
Returns a polygon shape for the physics sprite with the size based on the ‘shape_size` property value.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 52 def shape @shape ||= begin poly_array = [ [ -1 * shape_size, -1 * shape_size ], [ -1 * shape_size, shape_size ], [ shape_size, shape_size ], [ shape_size, -1 * shape_size ] ].map do |x,y| CP::Vec2.new(x,y) end new_shape = CP::Shape::Poly.new(body,poly_array, CP::Vec2::ZERO) new_shape.collision_type = shape_name.to_sym new_shape.e = 0.0 new_shape end end |
#show ⇒ Object
Upon the scene start the body is assigned the x and y position. If this method is overriden the position will need to be set manually.
78 79 80 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 78 def show body.p = CP::Vec2.new(x,y) end |
#update ⇒ Object
On update track the position of the sprite based on the position of the physics body.
84 85 86 87 |
# File 'lib/metro/models/ui/physics_sprite.rb', line 84 def update self.x = body.p.x self.y = body.p.y end |