Class: RubyLabs::SphereLab::Body

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

Overview

Body

A Body object represents the state of a celestial body. A body has mass (a scalar), position (a vector), and velocity (a vector). A third vector, named force, is used when calculating forces acting on a body. The size and color attributes are used by the visualization methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Body

Create a new Body object. If the argument list is empty, the attributes (mass, position, etc) are all set to 0. Otherwise the arguments should be mass (a Float), position (a vector), and velocity (another vector). A fourth argument is an optional name for the body.

Example:

>> b = Body.new
=> : 0 kg (0,0,0) (0,0,0)
>> b = Body.new(1e6, Vector.new(0,0,0), Vector.new(0,0,0), "test")
=> test: 1e+06 kg (0,0,0) (0,0,0)


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/spherelab.rb', line 206

def initialize(*args)
  if args.length > 0
    @mass, @position, @velocity, @name = args
  else
    @mass = 0.0
    @position = Vector.new(0.0, 0.0, 0.0)
    @velocity = Vector.new(0.0, 0.0, 0.0)
    @name = nil
  end
  @force = Vector.new(0.0, 0.0, 0.0)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



193
194
195
# File 'lib/spherelab.rb', line 193

def color
  @color
end

#forceObject

Returns the value of attribute force.



192
193
194
# File 'lib/spherelab.rb', line 192

def force
  @force
end

#graphicObject

Returns the value of attribute graphic.



193
194
195
# File 'lib/spherelab.rb', line 193

def graphic
  @graphic
end

#massObject

Returns the value of attribute mass.



192
193
194
# File 'lib/spherelab.rb', line 192

def mass
  @mass
end

#nameObject

Returns the value of attribute name.



193
194
195
# File 'lib/spherelab.rb', line 193

def name
  @name
end

#positionObject

Returns the value of attribute position.



192
193
194
# File 'lib/spherelab.rb', line 192

def position
  @position
end

#prevxObject

Returns the value of attribute prevx.



193
194
195
# File 'lib/spherelab.rb', line 193

def prevx
  @prevx
end

#prevyObject

Returns the value of attribute prevy.



193
194
195
# File 'lib/spherelab.rb', line 193

def prevy
  @prevy
end

#sizeObject

Returns the value of attribute size.



193
194
195
# File 'lib/spherelab.rb', line 193

def size
  @size
end

#velocityObject

Returns the value of attribute velocity.



192
193
194
# File 'lib/spherelab.rb', line 192

def velocity
  @velocity
end

Class Method Details

.interaction(b1, b2) ⇒ Object

This class method will compute the interaction between bodies b1 and b2 and update their force vectors. Since the forces on the bodies are the same, but acting in the opposite direction, the force can be calculated just once and then used to update both bodies.



269
270
271
272
273
274
# File 'lib/spherelab.rb', line 269

def Body.interaction(b1, b2)
  r = b1.position - b2.position
  a = r.norm ** 3
  b1.force.add(r * (b2.mass / a))
  b2.force.add(r * (-b1.mass / a))
end

Instance Method Details

#add_force(b) ⇒ Object

Compute the force exerted on this body by body b and update this body’s force vector.



249
250
251
252
253
254
# File 'lib/spherelab.rb', line 249

def add_force(b)
  r = @position - b.position
  nr = r.norm ** 3
  mr = b.mass / nr
  @force.add(r * mr)
end

#clear_forceObject

Reset the force vector to (0,0,0). Called by SphereLab#step_system at the start of each new round of interaction calculations.



241
242
243
244
245
# File 'lib/spherelab.rb', line 241

def clear_force
  @force.x = 0.0
  @force.y = 0.0
  @force.z = 0.0
end

#cloneObject

Make a copy of this body.



220
221
222
223
224
225
226
227
228
229
# File 'lib/spherelab.rb', line 220

def clone
  copy = super
  if graphic
    copy.position = position.clone
    copy.velocity = velocity.clone
    copy.force = force.clone
    copy.graphic = Canvas::Circle.new(prevx, prevy, size, :fill => color)
  end
  return copy
end

#inspectObject

Create a string that summarizes the state of this body.



233
234
235
236
# File 'lib/spherelab.rb', line 233

def inspect
  name = @name ? @name : ""
  return sprintf "%s: %.3g kg %s %s", name, @mass, @position.inspect, @velocity.inspect
end

#move(dt) ⇒ Object

Update this body’s position by applying the current force vector for dt seconds.



258
259
260
261
262
# File 'lib/spherelab.rb', line 258

def move(dt)
  acc = @force * G * -1.0
  @velocity.add( acc * dt ) 
  @position.add( @velocity * dt )
end