Class: RGame::Engine::Body
- Inherits:
-
Object
- Object
- RGame::Engine::Body
- Defined in:
- lib/rgame/engine/body.rb
Overview
Reusable kinematics for free-moving, rotating entities (ship, rocks, bullets):
position, linear velocity, facing angle and angular velocity. integrate
advances them by a fixed dt; wrap! implements toroidal screen-wrap and
offscreen? the despawn test. Pure logic; no graphics.
Angle is in radians, 0 = pointing right (+x), increasing clockwise on screen (since y grows downward). A heading's unit vector is therefore (cos, sin).
Instance Attribute Summary collapse
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#spin ⇒ Object
Returns the value of attribute spin.
-
#vx ⇒ Object
Returns the value of attribute vx.
-
#vy ⇒ Object
Returns the value of attribute vy.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(x: 0.0, y: 0.0, vx: 0.0, vy: 0.0, angle: 0.0, spin: 0.0) ⇒ Body
constructor
A new instance of Body.
- #integrate(dt) ⇒ Object
-
#offscreen?(width, height, margin) ⇒ Boolean
Fully past an edge by
margin(e.g. a bullet that left the screen). -
#wrap!(width, height, margin) ⇒ Object
Toroidal wrap: once the centre passes
marginbeyond an edge it reappears on the opposite side.
Constructor Details
#initialize(x: 0.0, y: 0.0, vx: 0.0, vy: 0.0, angle: 0.0, spin: 0.0) ⇒ Body
Returns a new instance of Body.
15 16 17 18 19 20 21 22 |
# File 'lib/rgame/engine/body.rb', line 15 def initialize(x: 0.0, y: 0.0, vx: 0.0, vy: 0.0, angle: 0.0, spin: 0.0) @x = x @y = y @vx = vx @vy = vy @angle = angle @spin = spin end |
Instance Attribute Details
#angle ⇒ Object
Returns the value of attribute angle.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def angle @angle end |
#spin ⇒ Object
Returns the value of attribute spin.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def spin @spin end |
#vx ⇒ Object
Returns the value of attribute vx.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def vx @vx end |
#vy ⇒ Object
Returns the value of attribute vy.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def vy @vy end |
#x ⇒ Object
Returns the value of attribute x.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
13 14 15 |
# File 'lib/rgame/engine/body.rb', line 13 def y @y end |
Instance Method Details
#integrate(dt) ⇒ Object
24 25 26 27 28 |
# File 'lib/rgame/engine/body.rb', line 24 def integrate(dt) @x += @vx * dt @y += @vy * dt @angle += @spin * dt end |
#offscreen?(width, height, margin) ⇒ Boolean
Fully past an edge by margin (e.g. a bullet that left the screen).
44 45 46 |
# File 'lib/rgame/engine/body.rb', line 44 def offscreen?(width, height, margin) @x < -margin || @x > width + margin || @y < -margin || @y > height + margin end |
#wrap!(width, height, margin) ⇒ Object
Toroidal wrap: once the centre passes margin beyond an edge it reappears on
the opposite side. Using margin = the entity's radius means an object spawned
flush against an edge (centre at -radius) is exactly at the threshold, not
past it, so it never wraps on the frame it spawns.
34 35 36 37 38 39 40 41 |
# File 'lib/rgame/engine/body.rb', line 34 def wrap!(width, height, margin) span_x = width + (2 * margin) span_y = height + (2 * margin) @x += span_x while @x < -margin @x -= span_x while @x > width + margin @y += span_y while @y < -margin @y -= span_y while @y > height + margin end |