diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-10-30 09:00:45 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-10-30 09:00:45 -0300 |
commit | ab4126f51061277e87b41c48b40e7b54942d4eca (patch) | |
tree | c58168b60323c4d43b58743b099e562a89e60a56 /core/math | |
parent | 8b15b26eedad4fdd33d50f5f9aa0fcc1875d503f (diff) | |
parent | 914015f3b63dd956e72ea937d46ea4b2db005ada (diff) |
Merge branch 'master' of https://github.com/godotengine/godot
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/SCsub | 2 | ||||
-rw-r--r-- | core/math/math_2d.cpp | 24 | ||||
-rw-r--r-- | core/math/math_2d.h | 15 | ||||
-rw-r--r-- | core/math/math_funcs.cpp | 37 | ||||
-rw-r--r-- | core/math/vector3.cpp | 12 | ||||
-rw-r--r-- | core/math/vector3.h | 197 |
6 files changed, 131 insertions, 156 deletions
diff --git a/core/math/SCsub b/core/math/SCsub index 7b4a6acbc0..c2731d60e6 100644 --- a/core/math/SCsub +++ b/core/math/SCsub @@ -1,3 +1,5 @@ +#!/usr/bin/env python + Import('env') env.add_source_files(env.core_sources,"*.cpp") diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index 0e2060008c..e616f05914 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -424,7 +424,7 @@ Matrix32 Matrix32::inverse() const { void Matrix32::affine_invert() { - float det = elements[0][0]*elements[1][1] - elements[1][0]*elements[0][1]; + float det = basis_determinant(); ERR_FAIL_COND(det==0); float idet = 1.0 / det; @@ -475,18 +475,18 @@ Matrix32::Matrix32(real_t p_rot, const Vector2& p_pos) { elements[2]=p_pos; } -Vector2 Matrix32::get_scale() const { +Size2 Matrix32::get_scale() const { - return Vector2( elements[0].length(), elements[1].length() ); + return Size2( elements[0].length(), elements[1].length() ); } -void Matrix32::scale(const Vector2& p_scale) { +void Matrix32::scale(const Size2& p_scale) { elements[0]*=p_scale; elements[1]*=p_scale; elements[2]*=p_scale; } -void Matrix32::scale_basis(const Vector2& p_scale) { +void Matrix32::scale_basis(const Size2& p_scale) { elements[0]*=p_scale; elements[1]*=p_scale; @@ -501,7 +501,6 @@ void Matrix32::translate( const Vector2& p_translation ) { elements[2]+=basis_xform(p_translation); } - void Matrix32::orthonormalize() { // Gram-Schmidt Process @@ -550,11 +549,6 @@ void Matrix32::operator*=(const Matrix32& p_transform) { elements[2] = xform(p_transform.elements[2]); float x0,x1,y0,y1; -/* - x0 = p_transform.tdotx(elements[0]); - x1 = p_transform.tdoty(elements[0]); - y0 = p_transform.tdotx(elements[1]); - y1 = p_transform.tdoty(elements[1]);*/ x0 = tdotx(p_transform.elements[0]); x1 = tdoty(p_transform.elements[0]); @@ -576,7 +570,7 @@ Matrix32 Matrix32::operator*(const Matrix32& p_transform) const { } -Matrix32 Matrix32::scaled(const Vector2& p_scale) const { +Matrix32 Matrix32::scaled(const Size2& p_scale) const { Matrix32 copy=*this; copy.scale(p_scale); @@ -584,7 +578,7 @@ Matrix32 Matrix32::scaled(const Vector2& p_scale) const { } -Matrix32 Matrix32::basis_scaled(const Vector2& p_scale) const { +Matrix32 Matrix32::basis_scaled(const Size2& p_scale) const { Matrix32 copy=*this; copy.scale_basis(p_scale); @@ -629,8 +623,8 @@ Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) cons real_t r1 = get_rotation(); real_t r2 = p_transform.get_rotation(); - Vector2 s1 = get_scale(); - Vector2 s2 = p_transform.get_scale(); + Size2 s1 = get_scale(); + Size2 s2 = p_transform.get_scale(); //slerp rotation Vector2 v1(Math::cos(r1), Math::sin(r1)); diff --git a/core/math/math_2d.h b/core/math/math_2d.h index 90aae9fe50..38c1ac9656 100644 --- a/core/math/math_2d.h +++ b/core/math/math_2d.h @@ -553,10 +553,8 @@ struct Rect2i { struct Matrix32 { - Vector2 elements[3]; - _FORCE_INLINE_ float tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; } _FORCE_INLINE_ float tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; } @@ -577,26 +575,25 @@ struct Matrix32 { _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi,const Size2& p_scale); void rotate(real_t p_phi); - void scale(const Vector2& p_scale); - void scale_basis(const Vector2& p_scale); + void scale(const Size2& p_scale); + void scale_basis(const Size2& p_scale); void translate( real_t p_tx, real_t p_ty); void translate( const Vector2& p_translation ); float basis_determinant() const; - Vector2 get_scale() const; + Size2 get_scale() const; _FORCE_INLINE_ const Vector2& get_origin() const { return elements[2]; } _FORCE_INLINE_ void set_origin(const Vector2& p_origin) { elements[2]=p_origin; } - Matrix32 scaled(const Vector2& p_scale) const; - Matrix32 basis_scaled(const Vector2& p_scale) const; + Matrix32 scaled(const Size2& p_scale) const; + Matrix32 basis_scaled(const Size2& p_scale) const; Matrix32 translated(const Vector2& p_offset) const; Matrix32 rotated(float p_phi) const; Matrix32 untranslated() const; - void orthonormalize(); Matrix32 orthonormalized() const; @@ -615,7 +612,6 @@ struct Matrix32 { _FORCE_INLINE_ Rect2 xform(const Rect2& p_vec) const; _FORCE_INLINE_ Rect2 xform_inv(const Rect2& p_vec) const; - operator String() const; Matrix32(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) { @@ -630,7 +626,6 @@ struct Matrix32 { Matrix32(real_t p_rot, const Vector2& p_pos); Matrix32() { elements[0][0]=1.0; elements[1][1]=1.0; } - }; bool Rect2::intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const { diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 64615fe6b4..46c0218707 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -41,37 +41,16 @@ static uint32_t Q[4096]; #endif uint32_t Math::rand_from_seed(uint32_t *seed) { - -#if 1 - uint32_t k; - uint32_t s = (*seed); - if (s == 0) - s = 0x12345987; - k = s / 127773; - s = 16807 * (s - k * 127773) - 2836 * k; -// if (s < 0) -// s += 2147483647; - (*seed) = s; - return (s & Math::RANDOM_MAX); -#else - *seed = *seed * 1103515245 + 12345; - return (*seed % ((unsigned int)RANDOM_MAX + 1)); -#endif + // Xorshift31 PRNG + if ( *seed == 0 ) *seed = Math::RANDOM_MAX; + (*seed) ^= (*seed) << 13; + (*seed) ^= (*seed) >> 17; + (*seed) ^= (*seed) << 5; + return (*seed) & Math::RANDOM_MAX; } void Math::seed(uint32_t x) { -#if 0 - int i; - - Q[0] = x; - Q[1] = x + PHI; - Q[2] = x + PHI + PHI; - - for (i = 3; i < 4096; i++) - Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; -#else default_seed=x; -#endif } void Math::randomize() { @@ -82,12 +61,12 @@ void Math::randomize() { uint32_t Math::rand() { - return rand_from_seed(&default_seed)&0x7FFFFFFF; + return rand_from_seed(&default_seed); } double Math::randf() { - return (double)rand() / (double)RANDOM_MAX; + return (double)rand() / (double)Math::RANDOM_MAX; } double Math::sin(double p_x) { diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index b4a13d8f6d..fae3831dd6 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -49,13 +49,13 @@ void Vector3::set_axis(int p_axis,real_t p_value) { } real_t Vector3::get_axis(int p_axis) const { - ERR_FAIL_INDEX_V(p_axis,3,0); - return operator[](p_axis); + ERR_FAIL_INDEX_V(p_axis,3,0); + return operator[](p_axis); } int Vector3::min_axis() const { - return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); + return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2); } int Vector3::max_axis() const { @@ -71,9 +71,9 @@ void Vector3::snap(float p_val) { } Vector3 Vector3::snapped(float p_val) const { - Vector3 v=*this; - v.snap(p_val); - return v; + Vector3 v=*this; + v.snap(p_val); + return v; } diff --git a/core/math/vector3.h b/core/math/vector3.h index 910446023a..06840be5e7 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -63,77 +63,78 @@ struct Vector3 { return coord[p_axis]; } - void set_axis(int p_axis,real_t p_value); - real_t get_axis(int p_axis) const; + void set_axis(int p_axis,real_t p_value); + real_t get_axis(int p_axis) const; - int min_axis() const; - int max_axis() const; + int min_axis() const; + int max_axis() const; - _FORCE_INLINE_ real_t length() const; - _FORCE_INLINE_ real_t length_squared() const; + _FORCE_INLINE_ real_t length() const; + _FORCE_INLINE_ real_t length_squared() const; - _FORCE_INLINE_ void normalize(); - _FORCE_INLINE_ Vector3 normalized() const; - _FORCE_INLINE_ Vector3 inverse() const; + _FORCE_INLINE_ void normalize(); + _FORCE_INLINE_ Vector3 normalized() const; + _FORCE_INLINE_ Vector3 inverse() const; _FORCE_INLINE_ void zero(); - void snap(float p_val); - Vector3 snapped(float p_val) const; + void snap(float p_val); + Vector3 snapped(float p_val) const; void rotate(const Vector3& p_axis,float p_phi); Vector3 rotated(const Vector3& p_axis,float p_phi) const; - /* Static Methods between 2 vector3s */ + /* Static Methods between 2 vector3s */ - _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const; + _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const; Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const; Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const; - _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const; - _FORCE_INLINE_ real_t dot(const Vector3& p_b) const; + _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const; + _FORCE_INLINE_ real_t dot(const Vector3& p_b) const; - _FORCE_INLINE_ Vector3 abs() const; - _FORCE_INLINE_ Vector3 floor() const; - _FORCE_INLINE_ Vector3 ceil() const; + _FORCE_INLINE_ Vector3 abs() const; + _FORCE_INLINE_ Vector3 floor() const; + _FORCE_INLINE_ Vector3 ceil() const; - _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const; - _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const; + _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const; + _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const; + _FORCE_INLINE_ real_t angle_to(const Vector3& p_b) const; _FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const; _FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const; - /* Operators */ + /* Operators */ - _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v); - _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const; - _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v); - _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const; - _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v); - _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const; - _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v); - _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const; + _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v); + _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const; + _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v); + _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const; + _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v); + _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const; + _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v); + _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const; - _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar); - _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const; - _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar); - _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const; + _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar); + _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const; + _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar); + _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const; - _FORCE_INLINE_ Vector3 operator-() const; + _FORCE_INLINE_ Vector3 operator-() const; - _FORCE_INLINE_ bool operator==(const Vector3& p_v) const; - _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const; - _FORCE_INLINE_ bool operator<(const Vector3& p_v) const; + _FORCE_INLINE_ bool operator==(const Vector3& p_v) const; + _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const; + _FORCE_INLINE_ bool operator<(const Vector3& p_v) const; _FORCE_INLINE_ bool operator<=(const Vector3& p_v) const; operator String() const; - _FORCE_INLINE_ Vector3() { x=y=z=0; } - _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; } + _FORCE_INLINE_ Vector3() { x=y=z=0; } + _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; } }; @@ -151,11 +152,12 @@ Vector3 Vector3::cross(const Vector3& p_b) const { (x * p_b.y) - (y * p_b.x) ); - return ret; + return ret; } + real_t Vector3::dot(const Vector3& p_b) const { - return x*p_b.x + y*p_b.y + z*p_b.z; + return x*p_b.x + y*p_b.y + z*p_b.z; } Vector3 Vector3::abs() const { @@ -180,115 +182,119 @@ Vector3 Vector3::linear_interpolate(const Vector3& p_b,float p_t) const { y+(p_t * (p_b.y-y)), z+(p_t * (p_b.z-z)) ); - } - - real_t Vector3::distance_to(const Vector3& p_b) const { + return (p_b-*this).length(); } + real_t Vector3::distance_squared_to(const Vector3& p_b) const { - return (p_b-*this).length_squared(); + return (p_b-*this).length_squared(); +} + +real_t Vector3::angle_to(const Vector3& p_b) const { + + return Math::acos(this->dot(p_b) / Math::sqrt(this->length_squared() * p_b.length_squared())); } /* Operators */ Vector3& Vector3::operator+=(const Vector3& p_v) { - x+=p_v.x; - y+=p_v.y; - z+=p_v.z; - return *this; + x+=p_v.x; + y+=p_v.y; + z+=p_v.z; + return *this; } + Vector3 Vector3::operator+(const Vector3& p_v) const { - return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z); + return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z); } Vector3& Vector3::operator-=(const Vector3& p_v) { - x-=p_v.x; - y-=p_v.y; - z-=p_v.z; - return *this; + x-=p_v.x; + y-=p_v.y; + z-=p_v.z; + return *this; } Vector3 Vector3::operator-(const Vector3& p_v) const { - return Vector3(x-p_v.x, y-p_v.y, z- p_v.z); + return Vector3(x-p_v.x, y-p_v.y, z- p_v.z); } - - Vector3& Vector3::operator*=(const Vector3& p_v) { - x*=p_v.x; - y*=p_v.y; - z*=p_v.z; - return *this; + x*=p_v.x; + y*=p_v.y; + z*=p_v.z; + return *this; } Vector3 Vector3::operator*(const Vector3& p_v) const { - return Vector3(x*p_v.x, y*p_v.y, z* p_v.z); + return Vector3(x*p_v.x, y*p_v.y, z* p_v.z); } Vector3& Vector3::operator/=(const Vector3& p_v) { - x/=p_v.x; - y/=p_v.y; - z/=p_v.z; - return *this; + x/=p_v.x; + y/=p_v.y; + z/=p_v.z; + return *this; } + Vector3 Vector3::operator/(const Vector3& p_v) const { - return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z); + return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z); } Vector3& Vector3::operator*=(real_t p_scalar) { - x*=p_scalar; - y*=p_scalar; - z*=p_scalar; - return *this; + x*=p_scalar; + y*=p_scalar; + z*=p_scalar; + return *this; } _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) { + return p_vec * p_scalar; } Vector3 Vector3::operator*(real_t p_scalar) const { - return Vector3( x*p_scalar, y*p_scalar, z*p_scalar); + return Vector3( x*p_scalar, y*p_scalar, z*p_scalar); } Vector3& Vector3::operator/=(real_t p_scalar) { - x/=p_scalar; - y/=p_scalar; - z/=p_scalar; - return *this; + x/=p_scalar; + y/=p_scalar; + z/=p_scalar; + return *this; } Vector3 Vector3::operator/(real_t p_scalar) const { - return Vector3( x/p_scalar, y/p_scalar, z/p_scalar); + return Vector3( x/p_scalar, y/p_scalar, z/p_scalar); } Vector3 Vector3::operator-() const { - return Vector3( -x, -y, -z ); + return Vector3( -x, -y, -z ); } - bool Vector3::operator==(const Vector3& p_v) const { - return (x==p_v.x && y==p_v.y && z==p_v.z); + return (x==p_v.x && y==p_v.y && z==p_v.z); } bool Vector3::operator!=(const Vector3& p_v) const { - return (x!=p_v.x || y!=p_v.y || z!=p_v.z); + return (x!=p_v.x || y!=p_v.y || z!=p_v.z); } bool Vector3::operator<(const Vector3& p_v) const { @@ -298,9 +304,9 @@ bool Vector3::operator<(const Vector3& p_v) const { return z<p_v.z; else return y<p_v.y; - } else + } else { return x<p_v.x; - + } } bool Vector3::operator<=(const Vector3& p_v) const { @@ -310,9 +316,9 @@ bool Vector3::operator<=(const Vector3& p_v) const { return z<=p_v.z; else return y<p_v.y; - } else + } else { return x<p_v.x; - + } } _FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) { @@ -333,6 +339,7 @@ real_t Vector3::length() const { return Math::sqrt(x2+y2+z2); } + real_t Vector3::length_squared() const { real_t x2=x*x; @@ -340,27 +347,25 @@ real_t Vector3::length_squared() const { real_t z2=z*z; return x2+y2+z2; - } void Vector3::normalize() { - real_t l=length(); - if (l==0) { - + real_t l=length(); + if (l==0) { x=y=z=0; - } else { - + } else { x/=l; y/=l; z/=l; - } + } } + Vector3 Vector3::normalized() const { - Vector3 v=*this; - v.normalize(); - return v; + Vector3 v=*this; + v.normalize(); + return v; } Vector3 Vector3::inverse() const { @@ -377,10 +382,10 @@ Vector3 Vector3::slide(const Vector3& p_vec) const { return p_vec - *this * this->dot(p_vec); } + Vector3 Vector3::reflect(const Vector3& p_vec) const { return p_vec - *this * this->dot(p_vec) * 2.0; - } #endif |