steering_behaviors 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -0,0 +1,7 @@
1
+ # Changelog for steering-behaviors
2
+
3
+ ## 1.0.1, 31 July 2013
4
+ * Some mild optimizations in Vector (caching, Pi calcs)
5
+
6
+ ## 1.0.0, 25 July 2013
7
+ * Initial Gem release
@@ -7,6 +7,9 @@
7
7
  # the terms found in the "LICENSE" file included with the framework.
8
8
 
9
9
  class SteeringBehaviors::Vector
10
+ TWOPI = 6.283185307179586
11
+ THREEPI = 9.42477796076938
12
+
10
13
  attr_reader :x, :y
11
14
 
12
15
  def initialize(x=0,y=0)
@@ -14,6 +17,11 @@ class SteeringBehaviors::Vector
14
17
  @y = y.to_f
15
18
  end
16
19
 
20
+ def clear_cache
21
+ @length = nil
22
+ @radians = nil
23
+ end
24
+
17
25
  def ==(other)
18
26
  self.class == other.class && @x==other.x && @y==other.y
19
27
  end
@@ -21,14 +29,17 @@ class SteeringBehaviors::Vector
21
29
 
22
30
  def x=(x)
23
31
  @x = x.to_f
32
+ clear_cache
24
33
  end
25
34
 
26
35
  def y=(y)
27
36
  @y = y.to_f
37
+ clear_cache
28
38
  end
29
39
 
30
40
  def length
31
- Math.sqrt(@x**2 + @y**2)
41
+ # @length || @length = Math...
42
+ @length ||= Math.sqrt(@x**2 + @y**2)
32
43
  end
33
44
 
34
45
  def +(v)
@@ -48,7 +59,7 @@ class SteeringBehaviors::Vector
48
59
  end
49
60
 
50
61
  def delta(other)
51
- (( ( other.radians - self.radians + Math::PI + 2*Math::PI ) % (2*Math::PI) ) - Math::PI).abs
62
+ (( ( other.radians - self.radians + THREEPI ) % (TWOPI) ) - Math::PI).abs
52
63
  end
53
64
 
54
65
  def normalize!
@@ -57,6 +68,7 @@ class SteeringBehaviors::Vector
57
68
 
58
69
  @x /= orig_length
59
70
  @y /= orig_length
71
+ clear_cache
60
72
 
61
73
  self
62
74
  end
@@ -112,21 +124,25 @@ class SteeringBehaviors::Vector
112
124
  end
113
125
 
114
126
  def radians
115
- theta = Math.acos(@y/length)
116
- if @x < 0
117
- theta *= -1
118
- end
127
+ @radians ||= lambda do |x, y|
128
+ theta = Math.acos(y/length)
129
+ if x < 0
130
+ theta *= -1
131
+ end
119
132
 
120
- theta % (2 * Math::PI)
133
+ return theta % (TWOPI)
134
+ end.call(@x, @y)
121
135
  end
122
136
 
123
137
  def from_compass_bearing!(brg)
124
138
  rad = SteeringBehaviors::Vector.deg2rad(brg)
125
139
  self.x = Math.sin(rad)
126
140
  self.y = Math.cos(rad)
141
+ clear_cache
127
142
  end
128
143
 
129
144
  def rotate!(radians)
145
+ clear_cache
130
146
  circle_cos = Math.cos(-radians)
131
147
  circle_sin = Math.sin(-radians)
132
148
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: steering_behaviors
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Powell
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-25 00:00:00.000000000 Z
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: |
15
15
  If you're building a game, you need your game agents and characters to move on their own. A standard way of doing this is with 'steering behaviors'. The seminal paper by Craig Reynolds established a core set of steering behaviors that could be utilized for a variety of common movement tasks and natural behaviors. This Ruby library can accomplish many/most of those tasks for your Ruby / JRuby game. The basic behaviors can be layered for more complicated and advanced behaviors, such as flocking and crowd movement.