Class: SFML::Vector3
- Inherits:
-
Object
- Object
- SFML::Vector3
- Includes:
- Enumerable
- Defined in:
- ext/system/vec3.c,
ext/system/vec3.c
Overview
A 3D vector of floats, used for positions, sizes and directions in 3D space (e.g. Listener and SoundSource position/direction/velocity).
Includes Enumerable and behaves like a 3-element sequence: it responds to
#each and #to_a, and can be compared or combined with a plain
[x, y, z] Array anywhere a Vector3 is accepted.
Instance Attribute Summary collapse
-
#x ⇒ Float
Returns the X component of the vector.
-
#y ⇒ Float
Returns the Y component of the vector.
-
#z ⇒ Float
Returns the Z component of the vector.
Instance Method Summary collapse
-
#*(scalar) ⇒ Vector3
Multiplies all three components by
scalar. -
#+(other) ⇒ Vector3
othermay be a Vector3 or a 3-element Array. -
#-(other) ⇒ Vector3
othermay be a Vector3 or a 3-element Array. -
#==(other) ⇒ Boolean
othermay be a Vector3 or a 3-element Array. -
#each {|component| ... } ⇒ self
Yields
x,y, thenz. -
#initialize(*args) ⇒ Vector3
constructor
Creates a new 3D vector from three coordinates, a 3-element Array, or another Vector3.
-
#size ⇒ Integer
Returns the number of components, always 3.
-
#size ⇒ Integer
Returns the number of components, always 3.
-
#to_a ⇒ Array
Returns the vector's components as a three-element Array.
-
#to_a ⇒ Array
Returns the vector's components as a three-element Array.
-
#to_s ⇒ String
Returns a human-readable
"(x, y, z)"representation.
Constructor Details
#new ⇒ Vector3(0, 0, 0) #new(x, y, z) ⇒ Vector3(x] #new([x, y, z]) ⇒ Vector3(x] #new(other_vector3) ⇒ Vector3
Creates a new 3D vector from three coordinates, a 3-element Array, or another Vector3.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'ext/system/vec3.c', line 51
static VALUE Vector3_initialize(int argc, VALUE* argv, VALUE self) {
Vector3* ptr;
float x = 0;
float y = 0;
float z = 0;
if (argc == 1 && rb_obj_is_kind_of(argv[0], rb_cVector3)) {
ptr = Get_Vector3_Struct(argv[0]);
x = ptr->vec.x;
y = ptr->vec.y;
z = ptr->vec.z;
} else if (argc == 1 && RB_TYPE_P(argv[0], T_ARRAY)) {
if (RARRAY_LEN(argv[0]) < 3) {
raise_invalid_array_length(3);
}
x = NUM2DBL(rb_ary_entry(argv[0], 0));
y = NUM2DBL(rb_ary_entry(argv[0], 1));
z = NUM2DBL(rb_ary_entry(argv[0], 2));
} else if (argc == 3) {
x = NUM2DBL(argv[0]);
y = NUM2DBL(argv[1]);
z = NUM2DBL(argv[2]);
} else if (argc != 0) {
raise_invalid_arguments_excepted(3, argc);
}
ptr = Get_Vector3_Struct(self);
ptr->vec.x = x;
ptr->vec.y = y;
ptr->vec.z = z;
return self;
}
|
Instance Attribute Details
#x ⇒ Float
Returns the X component of the vector.
269 270 271 |
# File 'ext/system/vec3.c', line 269
static VALUE Vector3_get_x(VALUE self) {
return DBL2NUM(((Vector3*)Get_Vector3_Struct(self))->vec.x);
}
|
#y ⇒ Float
Returns the Y component of the vector.
269 270 271 |
# File 'ext/system/vec3.c', line 269
static VALUE Vector3_get_y(VALUE self) {
return DBL2NUM(((Vector3*)Get_Vector3_Struct(self))->vec.y);
}
|
#z ⇒ Float
Returns the Z component of the vector.
269 270 271 |
# File 'ext/system/vec3.c', line 269
static VALUE Vector3_get_z(VALUE self) {
return DBL2NUM(((Vector3*)Get_Vector3_Struct(self))->vec.z);
}
|
Instance Method Details
#*(scalar) ⇒ Vector3
Multiplies all three components by scalar.
247 248 249 250 251 252 |
# File 'ext/system/vec3.c', line 247
static VALUE Vector3_mul(VALUE self, VALUE rb_scalar) {
Vector3* a = Get_Vector3_Struct(self);
float scalar = NUM2DBL(rb_scalar);
return vec3f_to_rb((sfVector3f){a->vec.x * scalar, a->vec.y * scalar, a->vec.z * scalar});
}
|
#+(other) ⇒ Vector3
other may be a Vector3 or a 3-element Array.
219 220 221 222 223 224 |
# File 'ext/system/vec3.c', line 219
static VALUE Vector3_add(VALUE self, VALUE rb_other) {
Vector3* a = Get_Vector3_Struct(self);
sfVector3f b = vec3f_from_rb(rb_other);
return vec3f_to_rb((sfVector3f){a->vec.x + b.x, a->vec.y + b.y, a->vec.z + b.z});
}
|
#-(other) ⇒ Vector3
other may be a Vector3 or a 3-element Array.
233 234 235 236 237 238 |
# File 'ext/system/vec3.c', line 233
static VALUE Vector3_sub(VALUE self, VALUE rb_other) {
Vector3* a = Get_Vector3_Struct(self);
sfVector3f b = vec3f_from_rb(rb_other);
return vec3f_to_rb((sfVector3f){a->vec.x - b.x, a->vec.y - b.y, a->vec.z - b.z});
}
|
#==(other) ⇒ Boolean
other may be a Vector3 or a 3-element Array.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'ext/system/vec3.c', line 197
static VALUE Vector3_eql(VALUE self, VALUE rb_other) {
Vector3* a = Get_Vector3_Struct(self);
sfVector3f b;
if (rb_obj_is_kind_of(rb_other, rb_cVector3)) {
b = ((Vector3*)Get_Vector3_Struct(rb_other))->vec;
} else if (RB_TYPE_P(rb_other, T_ARRAY) && RARRAY_LEN(rb_other) >= 3) {
b = vec3f_from_rb(rb_other);
} else {
return Qfalse;
}
return BOOL2RB(a->vec.x == b.x && a->vec.y == b.y && a->vec.z == b.z);
}
|
#each {|component| ... } ⇒ self
Yields x, y, then z.
170 171 172 173 174 175 176 177 178 |
# File 'ext/system/vec3.c', line 170
static VALUE Vector3_each(VALUE self) {
Vector3* vec = Get_Vector3_Struct(self);
rb_yield(DBL2NUM(vec->vec.x));
rb_yield(DBL2NUM(vec->vec.y));
rb_yield(DBL2NUM(vec->vec.z));
return self;
}
|
#size ⇒ Integer
Returns the number of components, always 3.
186 187 188 |
# File 'ext/system/vec3.c', line 186 static VALUE Vector3_size(VALUE self) { return INT2NUM(3); } |
#size ⇒ Integer
Returns the number of components, always 3.
186 187 188 |
# File 'ext/system/vec3.c', line 186 static VALUE Vector3_size(VALUE self) { return INT2NUM(3); } |
#to_a ⇒ Array
Returns the vector's components as a three-element Array.
158 159 160 161 162 |
# File 'ext/system/vec3.c', line 158
static VALUE Vector3_to_a(VALUE self) {
Vector3* vec = Get_Vector3_Struct(self);
return rb_ary_new_from_args(3, DBL2NUM(vec->vec.x), DBL2NUM(vec->vec.y), DBL2NUM(vec->vec.z));
}
|
#to_a ⇒ Array
Returns the vector's components as a three-element Array.
158 159 160 161 162 |
# File 'ext/system/vec3.c', line 158
static VALUE Vector3_to_a(VALUE self) {
Vector3* vec = Get_Vector3_Struct(self);
return rb_ary_new_from_args(3, DBL2NUM(vec->vec.x), DBL2NUM(vec->vec.y), DBL2NUM(vec->vec.z));
}
|
#to_s ⇒ String
Returns a human-readable "(x, y, z)" representation.
260 261 262 263 264 265 266 267 |
# File 'ext/system/vec3.c', line 260
static VALUE Vector3_to_s(VALUE self) {
Vector3* a = Get_Vector3_Struct(self);
char buffer[96];
snprintf(buffer, sizeof(buffer), "(%g, %g, %g)", a->vec.x, a->vec.y, a->vec.z);
return rb_str_new2(buffer);
}
|