Class: Pongo::RimParticle

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, mt) ⇒ RimParticle

The RimParticle is really just a second component of the wheel model. The rim particle is simulated in a coordsystem relative to the wheel’s center, not in worldspace.

Origins of this code are from Raigan Burns, Metanet Software



12
13
14
15
16
17
18
19
# File 'lib/pongo/rim_particle.rb', line 12

def initialize(r, mt)
  @curr = Vector.new(r, 0)
  @prev = Vector.new(0, 0)
  @speed = 0
  @angular_velocity = 0
  @max_torque = mt
  @wr = r
end

Instance Attribute Details

#angular_velocityObject

Returns the value of attribute angular_velocity.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def angular_velocity
  @angular_velocity
end

#currObject

Returns the value of attribute curr.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def curr
  @curr
end

#max_torqueObject

Returns the value of attribute max_torque.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def max_torque
  @max_torque
end

#prevObject

Returns the value of attribute prev.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def prev
  @prev
end

#speedObject

Returns the value of attribute speed.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def speed
  @speed
end

#wrObject

Returns the value of attribute wr.



5
6
7
# File 'lib/pongo/rim_particle.rb', line 5

def wr
  @wr
end

Instance Method Details

#update(dt) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pongo/rim_particle.rb', line 21

def update(dt)
  @speed = MathUtil.max(-@max_torque, MathUtil.min(@max_torque, @speed + @angular_velocity))

  # apply torque
  # this is the tangent vector at the rim particle
  dx = -@curr.y
  dy = @curr.x

  # normalize so we can scale by the rotational speed
  len = Math.sqrt(dx * dx + dy * dy)
  dx /= len
  dy /= len

  @curr.x += @speed * dx
  @curr.y += @speed * dy

  ox = @prev.x
  oy = @prev.y
  px = @prev.x = @curr.x
  py = @prev.y = @curr.y

  @curr.x += APEngine.damping * (px - ox)
  @curr.y += APEngine.damping * (py - oy)

  # hold the rim particle in place
  clen = @curr.magnitude
  diff = (clen - wr) / clen

  @curr.x -= @curr.x * diff
  @curr.y -= @curr.y * diff
end