Module: Box2D::ValueConversion
- Defined in:
- lib/box2d/value_conversion.rb
Class Method Summary collapse
- .copy_struct(struct_class, value) ⇒ Object
- .finite_float(value, label:) ⇒ Object
- .id_key(id) ⇒ Object
- .native_rot(angle) ⇒ Object
- .native_vec2(value, label: "vector") ⇒ Object
- .non_negative_float(value, label:) ⇒ Object
- .positive_float(value, label:) ⇒ Object
- .vec2(value) ⇒ Object
Class Method Details
.copy_struct(struct_class, value) ⇒ Object
54 55 56 57 58 |
# File 'lib/box2d/value_conversion.rb', line 54 def copy_struct(struct_class, value) struct_class.new.tap do |copy| copy.pointer.put_bytes(0, value.pointer.read_bytes(struct_class.size)) end end |
.finite_float(value, label:) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/box2d/value_conversion.rb', line 31 def finite_float(value, label:) number = Float(value) return number if number.finite? raise ArgumentError, "#{label} must be finite" rescue TypeError, ArgumentError raise ArgumentError, "#{label} must be a finite number" end |
.id_key(id) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/box2d/value_conversion.rb', line 60 def id_key(id) members = id.class.members key = Integer(id[:index1]) | (Integer(id[:generation]) << 32) return key unless members.include?(:world0) key | (Integer(id[:world0]) << 48) end |
.native_rot(angle) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/box2d/value_conversion.rb', line 15 def native_rot(angle) radians = finite_float(angle, label: "angle") Native::Rot.new.tap do |rotation| rotation[:c] = Math.cos(radians) rotation[:s] = Math.sin(radians) end end |
.native_vec2(value, label: "vector") ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/box2d/value_conversion.rb', line 7 def native_vec2(value, label: "vector") x, y = pair(value, label:) Native::Vec2.new.tap do |vector| vector[:x] = finite_float(x, label: "#{label}.x") vector[:y] = finite_float(y, label: "#{label}.y") end end |
.non_negative_float(value, label:) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/box2d/value_conversion.rb', line 47 def non_negative_float(value, label:) number = finite_float(value, label:) return number if number >= 0.0 raise ArgumentError, "#{label} must be non-negative" end |
.positive_float(value, label:) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/box2d/value_conversion.rb', line 40 def positive_float(value, label:) number = finite_float(value, label:) return number if number.positive? raise ArgumentError, "#{label} must be greater than zero" end |
.vec2(value) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/box2d/value_conversion.rb', line 23 def vec2(value) x = value[:x] y = value[:y] return Larb::Vec2.new(x, y) if defined?(Larb::Vec2) [x, y] end |