Class: Physicist::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/physicist/body.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position:, velocity:, t0:, dimensions:) ⇒ Body

Returns a new instance of Body.



6
7
8
9
10
11
# File 'lib/physicist/body.rb', line 6

def initialize(position:, velocity:, t0:, dimensions:)
  @position = position
  @velocity = velocity
  @dimensions = dimensions
  @t0 = t0
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



3
4
5
# File 'lib/physicist/body.rb', line 3

def dimensions
  @dimensions
end

#overflow_timeObject

Returns the value of attribute overflow_time.



4
5
6
# File 'lib/physicist/body.rb', line 4

def overflow_time
  @overflow_time
end

#positionObject (readonly)

Returns the value of attribute position.



3
4
5
# File 'lib/physicist/body.rb', line 3

def position
  @position
end

#t0Object (readonly)

Returns the value of attribute t0.



3
4
5
# File 'lib/physicist/body.rb', line 3

def t0
  @t0
end

#velocityObject (readonly)

Returns the value of attribute velocity.



3
4
5
# File 'lib/physicist/body.rb', line 3

def velocity
  @velocity
end

Instance Method Details

#at(t, obstacles: [], fixed_timestep: false, planck_time: 0.00125) ⇒ Object

def planck_time 0.00125 end



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/physicist/body.rb', line 38

def at(t, obstacles:[], fixed_timestep: false, planck_time: 0.00125)
  if fixed_timestep
    # proceed in fixed timesteps...
    dt = t - t0
    acc = 0.0

    body = self
    while acc < dt + (overflow_time||0.0)
      acc += planck_time
      body = body.predict(t + acc, obstacles: obstacles)
    end

    # body.overflow_time = acc - (dt + (overflow_time||0.0))
    body
  else
    predict(t, obstacles: obstacles)
  end
end

#epsilonObject

some tiny number



30
31
32
# File 'lib/physicist/body.rb', line 30

def epsilon
  0.00000000001
end

#frictionObject



25
26
27
# File 'lib/physicist/body.rb', line 25

def friction
  10.0
end

#gravityObject



21
22
23
# File 'lib/physicist/body.rb', line 21

def gravity
  30.0
end

#heightObject



17
18
19
# File 'lib/physicist/body.rb', line 17

def height
  dimensions[1]
end

#predict(t, obstacles: []) ⇒ Object

predict without proceeding in fixed time-steps from last-updated



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/physicist/body.rb', line 58

def predict(t, obstacles: [])
  x0, _   = *position
  vx0,vy0 = *velocity

  x_speed  = vx0.abs
  sign_x = vx0 > 0.0 ? 1.0 : (vx0 < -0.0 ? -1.0 : 0.0)

  dt = t - t0

  vy = vy0 + (gravity * dt)

  fric = friction * dt
  x_halted = false

  vx = if (3*fric) < x_speed
         vx0 + (fric * -sign_x)
       else
         x_halted = true
         x_stopping_distance = (vx0 ** 2) / (3 * friction)
         0
       end

  xt,yt,vxt,vyt = deduce_y_coordinate(vy,t,obstacles:obstacles) do |y,_vyt|
    if x_halted
      [x0 + (x_stopping_distance * sign_x), y, vx, _vyt]
    else
      deduce_x_coordinate(y,vx,t,obstacles:obstacles) do |x,_vxt|
        [x, y, _vxt, _vyt]
      end
    end
  end

  vxt = 0 if -epsilon < vx && vx < epsilon
  vyt = 0 if -epsilon < vy && vy < epsilon

  Body.new(
    position: [xt,yt],
    velocity: [vxt,vyt],
    dimensions: dimensions,
    t0: t
  )
end

#widthObject



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

def width
  dimensions[0]
end