Class: CP::Vec2

Inherits:
Object
  • Object
show all
Defined in:
lib/chipmunk-ffi/vec2.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Vec2

Returns a new instance of Vec2.



61
62
63
64
65
66
67
68
69
70
# File 'lib/chipmunk-ffi/vec2.rb', line 61

def initialize(*args)
  case args.size
  when 1
    @struct = args.first
  when 2
    @struct = CP.cpv(*args)
  else
    raise "wrong number of arguments (#{args.size} for 2)"
  end
end

Instance Attribute Details

#structObject

Returns the value of attribute struct.



60
61
62
# File 'lib/chipmunk-ffi/vec2.rb', line 60

def struct
  @struct
end

Class Method Details

.for_angle(angle) ⇒ Object



87
88
89
# File 'lib/chipmunk-ffi/vec2.rb', line 87

def self.for_angle(angle)
  Vec2.new CP.cpvforangle(angle)
end

Instance Method Details

#*(s) ⇒ Object



115
116
117
# File 'lib/chipmunk-ffi/vec2.rb', line 115

def *(s)
  Vec2.new CP.cpvmult(@struct, s)
end

#+(other_vec) ⇒ Object



107
108
109
# File 'lib/chipmunk-ffi/vec2.rb', line 107

def +(other_vec)
  Vec2.new CP.cpvadd(@struct, other_vec.struct)
end

#-(other_vec) ⇒ Object



111
112
113
# File 'lib/chipmunk-ffi/vec2.rb', line 111

def -(other_vec)
  Vec2.new CP.cpvsub(@struct, other_vec.struct)
end

#-@Object



103
104
105
# File 'lib/chipmunk-ffi/vec2.rb', line 103

def -@
  Vec2.new CP.cpvneg(@struct)  
end

#/(s) ⇒ Object



119
120
121
122
# File 'lib/chipmunk-ffi/vec2.rb', line 119

def /(s)
  factor = 1.0/s
  Vec2.new CP.cpvmult(@struct, factor)
end

#==(other) ⇒ Object



198
199
200
# File 'lib/chipmunk-ffi/vec2.rb', line 198

def ==(other)
  self.x == other.x && self.y == other.y
end

#clamp(other_vec) ⇒ Object



173
174
175
# File 'lib/chipmunk-ffi/vec2.rb', line 173

def clamp(other_vec)
  Vec2.new CP.cpvclamp(@struct)
end

#cross(other_vec) ⇒ Object



128
129
130
# File 'lib/chipmunk-ffi/vec2.rb', line 128

def cross(other_vec)
  CP.cpvcross(@struct, other_vec.struct)
end

#dist(other_vec) ⇒ Object



181
182
183
# File 'lib/chipmunk-ffi/vec2.rb', line 181

def dist(other_vec)
  CP.cpvdist(@struct, other_vec.struct)
end

#distsq(other_vec) ⇒ Object



185
186
187
# File 'lib/chipmunk-ffi/vec2.rb', line 185

def distsq(other_vec)
  CP.cpvdistsq(@struct, other_vec.struct)
end

#dot(other_vec) ⇒ Object



124
125
126
# File 'lib/chipmunk-ffi/vec2.rb', line 124

def dot(other_vec)
  CP.cpvdot(@struct, other_vec.struct)
end

#lengthObject



194
195
196
# File 'lib/chipmunk-ffi/vec2.rb', line 194

def length
  CP::cpvlength @struct
end

#lengthsqObject



152
153
154
# File 'lib/chipmunk-ffi/vec2.rb', line 152

def lengthsq
  CP.cpvlengthsq(@struct)
end

#lerp(other_vec, t) ⇒ Object



156
157
158
# File 'lib/chipmunk-ffi/vec2.rb', line 156

def lerp(other_vec, t)
  Vec2.new CP.cpvlerp(@struct, other_vec.struct, t)
end

#lerpconst(other_vec, d) ⇒ Object



177
178
179
# File 'lib/chipmunk-ffi/vec2.rb', line 177

def lerpconst(other_vec, d)
  Vec2.new CP.cpvlerpconst(@struct, other_vec.struct, d)
end

#near?(other_vec, dist) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
# File 'lib/chipmunk-ffi/vec2.rb', line 189

def near?(other_vec, dist)
  delta_v = CP.cpvsub(@struct, other_vec.struct)
  CP.cpvdot(delta_v, delta_v) < dist*dist
end

#normalizeObject



160
161
162
# File 'lib/chipmunk-ffi/vec2.rb', line 160

def normalize
  Vec2.new CP.cpvnormalize(@struct)
end

#normalize!Object



164
165
166
167
# File 'lib/chipmunk-ffi/vec2.rb', line 164

def normalize!
  @struct = CP.cpvnormalize(@struct)
  self
end

#normalize_safeObject



169
170
171
# File 'lib/chipmunk-ffi/vec2.rb', line 169

def normalize_safe
  Vec2.new CP.cpvnormalize_safe(@struct)
end

#perpObject



132
133
134
# File 'lib/chipmunk-ffi/vec2.rb', line 132

def perp
  Vec2.new CP.cpvperp(@struct)
end

#project(other_vec) ⇒ Object



140
141
142
# File 'lib/chipmunk-ffi/vec2.rb', line 140

def project(other_vec)
  Vec2.new CP.cpvproject(@struct, other_vec.struct)
end

#rotate(other_vec) ⇒ Object



144
145
146
# File 'lib/chipmunk-ffi/vec2.rb', line 144

def rotate(other_vec)
  Vec2.new CP.cpvrotate(@struct, other_vec.struct)
end

#rperpObject



136
137
138
# File 'lib/chipmunk-ffi/vec2.rb', line 136

def rperp
  Vec2.new CP.cpvrperp(@struct)
end

#to_aObject



99
100
101
# File 'lib/chipmunk-ffi/vec2.rb', line 99

def to_a
  [@struct[:x],@struct[:y]]
end

#to_angleObject



95
96
97
# File 'lib/chipmunk-ffi/vec2.rb', line 95

def to_angle
  CP.cpvtoangle @struct
end

#to_sObject



91
92
93
# File 'lib/chipmunk-ffi/vec2.rb', line 91

def to_s
  CP.cpvstr @struct
end

#unrotate(other_vec) ⇒ Object



148
149
150
# File 'lib/chipmunk-ffi/vec2.rb', line 148

def unrotate(other_vec)
  Vec2.new CP.cpvunrotate(@struct, other_vec.struct)
end

#xObject



72
73
74
# File 'lib/chipmunk-ffi/vec2.rb', line 72

def x
  @struct[:x]
end

#x=(new_x) ⇒ Object

Raises:

  • (TypeError)


75
76
77
78
# File 'lib/chipmunk-ffi/vec2.rb', line 75

def x=(new_x)
  raise TypeError, "can't modify frozen vec2" if frozen?
  @struct[:x] = new_x
end

#yObject



79
80
81
# File 'lib/chipmunk-ffi/vec2.rb', line 79

def y
  @struct[:y]
end

#y=(new_y) ⇒ Object

Raises:

  • (TypeError)


82
83
84
85
# File 'lib/chipmunk-ffi/vec2.rb', line 82

def y=(new_y)
  raise TypeError, "can't modify frozen vec2" if frozen?
  @struct[:y] = new_y
end