Class: Bvh::Motion

Inherits:
Object
  • Object
show all
Defined in:
lib/bvh/motion.rb,
lib/bvh/motion/frame.rb,
lib/bvh/motion/channel_data.rb

Defined Under Namespace

Classes: ChannelData, Frame

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMotion

Returns a new instance of Motion.



18
19
20
# File 'lib/bvh/motion.rb', line 18

def initialize
  @frames = []
end

Instance Attribute Details

#frame_timeObject

The amount of time that passes for each frame. Most BVH files have this set to 0.0333, or 30 frames per second. The following calculations might also be useful to you:

frame_time = 1 / frames_per_second   # =>  0.0333 = 1 / 30
frames_per_second = 1 / frame_time   # =>  30 = 1 / 0.0333


7
8
9
# File 'lib/bvh/motion.rb', line 7

def frame_time
  @frame_time
end

#framesObject (readonly)

The array of frames in this animation. You can modify this directly if you wish.



10
11
12
# File 'lib/bvh/motion.rb', line 10

def frames
  @frames
end

Instance Method Details

#add_frame(*frames) ⇒ Object Also known as: add_frames

Adds the specified frame or frames to the end of this animation.



28
29
30
# File 'lib/bvh/motion.rb', line 28

def add_frame(*frames)
  self.frames.concat frames.flatten
end

#add_time(seconds, target_frame = frames.last) ⇒ Object

Creates enough frames to fill the specified number of seconds, adds them to the animation, and returns self.

target_frame is the state at which the animation should be in upon reaching the final frame of the alotted time. If it contains data that is different from the last frame in this animation, then the result will be an interpolation between the two over the course of the alotted time.

You can use this method to specify key points in the animation, and produce a fluid result.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bvh/motion.rb', line 51

def add_time(seconds, target_frame = frames.last)
  raise "Can't calculate frames from seconds: frame_time is zero!" if frame_time == 0
  num_frames = (seconds / frame_time).to_i
  return self if num_frames == 0

  # buf holds the amount to change each frame
  buf = (frames.last - target_frame) / num_frames.to_f

  num_frames.times do |i|
    if i == (num_frames-1) then add_frame(target_frame.copy) # last iteration, last frame... we can cheat.
    else add_frame(create_frame + buf) # create_frame clones the last frame, and buf moves it closer to target
    end
  end

  self
end

#create_frameObject

Creates a single frame that is an exact copy of the last frame in this animation.



33
34
35
# File 'lib/bvh/motion.rb', line 33

def create_frame
  frames.last.copy
end

#create_frames(n) ⇒ Object

Creates N frames via #create_frame.



38
39
40
41
42
# File 'lib/bvh/motion.rb', line 38

def create_frames(n)
  r = []
  n.times { r << create_frame }
  r
end

#frame_countObject

Returns the number of frames in this animation.



23
24
25
# File 'lib/bvh/motion.rb', line 23

def frame_count
  frames.length
end

#frames_per_secondObject

Returns the frames per second calculated by (1 / frame_time)



13
# File 'lib/bvh/motion.rb', line 13

def frames_per_second; 1 / frame_time; end

#frames_per_second=(a) ⇒ Object

Assigns the frame_time by calculating it from (1 / frames_per_second)



16
# File 'lib/bvh/motion.rb', line 16

def frames_per_second=(a); self.frame_time = 1 / a; end

#truncate_time(seconds) ⇒ Object

Chops the specified amount of time off of the end of this animation and then returns self.



69
70
71
72
73
74
75
# File 'lib/bvh/motion.rb', line 69

def truncate_time(seconds)
  raise "Can't calculate frames from seconds: frame_time is zero!" if frame_time == 0
  num_frames = (seconds / frame_time).to_i
  return self if num_frames == 0
  frames[-1..-num_frames].each { |i| frames.delete i }
  self
end