Class: RGame::Engine::Body

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#angleObject

Returns the value of attribute angle.



13
14
15
# File 'lib/rgame/engine/body.rb', line 13

def angle
  @angle
end

#spinObject

Returns the value of attribute spin.



13
14
15
# File 'lib/rgame/engine/body.rb', line 13

def spin
  @spin
end

#vxObject

Returns the value of attribute vx.



13
14
15
# File 'lib/rgame/engine/body.rb', line 13

def vx
  @vx
end

#vyObject

Returns the value of attribute vy.



13
14
15
# File 'lib/rgame/engine/body.rb', line 13

def vy
  @vy
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/rgame/engine/body.rb', line 13

def x
  @x
end

#yObject

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

Returns:

  • (Boolean)


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