Class: DrivingPhysics::Car

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

Overview

treat instances of this class as immutable

Defined Under Namespace

Classes: Condition, Controls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) {|_self| ... } ⇒ Car

Returns a new instance of Car.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/driving_physics/car.rb', line 14

def initialize(environment)
  @environment = environment
  @mass             = 1000  # kg, without fuel or driver
  @min_turn_radius = 10     # meters
  @max_drive_force = 7000   # N - 1000kg car at 0.7g acceleration
  @max_brake_clamp = 100    # N
  @max_brake_force = 40_000 # N - 2000kg car at 2g braking
  @fuel_capacity   = 40     # L
  @brake_pad_depth = 10     # mm
  @driver_mass     = 75     # kg
  @fuel_consumption = 0.02  # L/s at full throttle

  @frontal_area = DrivingPhysics::FRONTAL_AREA # m^2
  @cd = DrivingPhysics::DRAG_COF

  @tires = Tire.new
  @controls = Controls.new
  @condition = Condition.new(brake_temp: @environment.air_temp,
                             brake_pad_depth: @brake_pad_depth)

  # consider downforce
  # probably area * angle
  # goes up with square of velocity

  yield self if block_given?
end

Instance Attribute Details

#brake_pad_depthObject

Returns the value of attribute brake_pad_depth.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def brake_pad_depth
  @brake_pad_depth
end

#cdObject

Returns the value of attribute cd.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def cd
  @cd
end

#conditionObject

Returns the value of attribute condition.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def condition
  @condition
end

#controlsObject

Returns the value of attribute controls.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def controls
  @controls
end

#driver_massObject

Returns the value of attribute driver_mass.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def driver_mass
  @driver_mass
end

#frontal_areaObject

Returns the value of attribute frontal_area.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def frontal_area
  @frontal_area
end

#fuel_capacityObject

Returns the value of attribute fuel_capacity.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def fuel_capacity
  @fuel_capacity
end

#fuel_consumptionObject

Returns the value of attribute fuel_consumption.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def fuel_consumption
  @fuel_consumption
end

#massObject

Returns the value of attribute mass.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def mass
  @mass
end

#max_brake_clampObject

Returns the value of attribute max_brake_clamp.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def max_brake_clamp
  @max_brake_clamp
end

#max_brake_forceObject

Returns the value of attribute max_brake_force.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def max_brake_force
  @max_brake_force
end

#max_drive_forceObject

Returns the value of attribute max_drive_force.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def max_drive_force
  @max_drive_force
end

#min_turn_radiusObject

Returns the value of attribute min_turn_radius.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def min_turn_radius
  @min_turn_radius
end

#tiresObject

Returns the value of attribute tires.



8
9
10
# File 'lib/driving_physics/car.rb', line 8

def tires
  @tires
end

Instance Method Details

#add_fuel(liters) ⇒ Object



100
101
102
103
104
105
# File 'lib/driving_physics/car.rb', line 100

def add_fuel(liters)
  sum = @condition.fuel + liters
  overflow = sum > @fuel_capacity ? sum - @fuel_capacity : 0
  @condition.add_fuel(liters - overflow)
  overflow
end

#air_resistanceObject



107
108
109
110
111
112
# File 'lib/driving_physics/car.rb', line 107

def air_resistance
  # use default air density for now
  VectorForce.air_resistance(@condition.vel,
                             frontal_area: @frontal_area,
                             drag_cof: @cd)
end

#applied_forceObject



126
127
128
# File 'lib/driving_physics/car.rb', line 126

def applied_force
  drive_force_vector + brake_force_vector
end

#brake_forceObject



80
81
82
# File 'lib/driving_physics/car.rb', line 80

def brake_force
  @max_brake_force * @controls.brake_pedal
end

#brake_force_vectorObject



84
85
86
# File 'lib/driving_physics/car.rb', line 84

def brake_force_vector
  -1 * @condition.movement_dir * brake_force
end

#drive_forceObject



72
73
74
# File 'lib/driving_physics/car.rb', line 72

def drive_force
  @condition.fuel > 0.0 ? (@max_drive_force * @controls.drive_pedal) : 0.0
end

#drive_force_vectorObject



76
77
78
# File 'lib/driving_physics/car.rb', line 76

def drive_force_vector
  @condition.dir * drive_force
end

#fuel_massObject



88
89
90
# File 'lib/driving_physics/car.rb', line 88

def fuel_mass
  @condition.fuel * @environment.petrol_density
end

#natural_forceObject



130
131
132
# File 'lib/driving_physics/car.rb', line 130

def natural_force
  air_resistance + rotational_resistance + rolling_resistance
end

#rolling_resistanceObject



119
120
121
122
123
124
# File 'lib/driving_physics/car.rb', line 119

def rolling_resistance
  # TODO: downforce
  VectorForce.rolling_resistance(weight,
                                 dir: @condition.movement_dir,
                                 roll_cof: @tires.roll_cof)
end

#rotational_resistanceObject



114
115
116
117
# File 'lib/driving_physics/car.rb', line 114

def rotational_resistance
  # uses default ROT_COF
  VectorForce.rotational_resistance(@condition.vel)
end

#sum_forcesObject



134
135
136
# File 'lib/driving_physics/car.rb', line 134

def sum_forces
  applied_force + natural_force
end

#tick!Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/driving_physics/car.rb', line 61

def tick!
  @condition.tick!(force: sum_forces,
                   mass: total_mass,
                   tire: @tires,
                   env: @environment)

  @condition.consume_fuel(@fuel_consumption *
                          @controls.drive_pedal *
                          @environment.tick)
end

#to_sObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/driving_physics/car.rb', line 41

def to_s
  [[format("Mass: %.1f kg", total_mass),
    format("Power: %.1f kN", @max_drive_force.to_f / 1000),
    format("Brakes: %.1f kN", @max_brake_force.to_f / 1000),
    format("Fr.A: %.2f m^2", @frontal_area),
    format("cD: %.2f", @cd),
   ].join(' | '),
   [format("Op: %d N", drive_force - brake_force),
    format("Drive: %d N", drive_force),
    format("Brake: %d N", brake_force),
   ].join(' | '),
   [format("Net: %.1f N", sum_forces.magnitude),
    format("Air: %.1f N", air_resistance.magnitude),
    format("Rot: %.1f N", rotational_resistance.magnitude),
    format("Roll: %.1f N", rolling_resistance.magnitude),
   ].join(' | '),
    @controls, @condition, @tires,
  ].join("\n")
end

#total_massObject



92
93
94
# File 'lib/driving_physics/car.rb', line 92

def total_mass
  @mass + fuel_mass + @driver_mass
end

#weightObject



96
97
98
# File 'lib/driving_physics/car.rb', line 96

def weight
  total_mass * @environment.g
end