diff options
Diffstat (limited to 'src/libjin/Math')
-rw-r--r-- | src/libjin/Math/je_random.cpp | 41 | ||||
-rw-r--r-- | src/libjin/Math/je_random.h | 11 | ||||
-rw-r--r-- | src/libjin/Math/je_transform.cpp | 99 | ||||
-rw-r--r-- | src/libjin/Math/je_transform.h | 49 | ||||
-rw-r--r-- | src/libjin/Math/je_vector2.hpp | 11 |
5 files changed, 207 insertions, 4 deletions
diff --git a/src/libjin/Math/je_random.cpp b/src/libjin/Math/je_random.cpp index 983fa36..216fd79 100644 --- a/src/libjin/Math/je_random.cpp +++ b/src/libjin/Math/je_random.cpp @@ -1,3 +1,4 @@ +#include <math.h> #include "je_random.h" namespace JinEngine @@ -5,9 +6,47 @@ namespace JinEngine namespace Math { - RandomGenerator::RandomGenerator() + RandomGenerator::RandomGenerator(uint32 seed) + : mSeed(seed) { } + uint32 RandomGenerator::rand() + { + unsigned int next = mSeed; + uint32 result; + + next *= 1103515245; + next += 12345; + result = (unsigned int)(next / 65536) % 2048; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int)(next / 65536) % 1024; + + next *= 1103515245; + next += 12345; + result <<= 10; + result ^= (unsigned int)(next / 65536) % 1024; + + mSeed = next; + + return result; + } + + uint32 RandomGenerator::rand(uint32 min, uint32 max) + { + uint32 n = rand(); + return n % (max - min + 1) + min; + } + + float RandomGenerator::randf(float min, float max, int ac) + { + uint32 a = pow(10.f, ac); + uint32 n = rand(min*a, max*a); + return (float)n / a; + } + } }
\ No newline at end of file diff --git a/src/libjin/Math/je_random.h b/src/libjin/Math/je_random.h index 0bcdec7..0f53155 100644 --- a/src/libjin/Math/je_random.h +++ b/src/libjin/Math/je_random.h @@ -14,11 +14,16 @@ namespace JinEngine class RandomGenerator { public: - RandomGenerator(); - explicit RandomGenerator(uint32 seed); + RandomGenerator(uint32 seed); + + uint32 rand(); uint32 rand(uint32 min, uint32 max); - float rand(float min, float max); + + float randf(float min, float max, int ac); + + private: + uint32 mSeed; }; diff --git a/src/libjin/Math/je_transform.cpp b/src/libjin/Math/je_transform.cpp new file mode 100644 index 0000000..c0676cb --- /dev/null +++ b/src/libjin/Math/je_transform.cpp @@ -0,0 +1,99 @@ +#include "je_transform.h" + +namespace JinEngine +{ + namespace Math + { + + Transform::Transform() + : mScale(1, 1) + , mPosition(0, 0) + , mOrigin(0, 0) + , mRotation(0) + { + } + + void Transform::set(float x, float y, float sx, float sy, float r, float ox, float oy) + { + setPosition(x, y); + setScale(sx, sy); + setRotation(r); + setOrigin(ox, oy); + } + + void Transform::setScale(float sx, float sy) + { + mScale.set(sx, sy); + } + + Vector2<float> Transform::getScale() const + { + return mScale; + } + + void Transform::scale(float sx, float sy) + { + mScale.x *= sx; + mScale.y *= sy; + } + + void Transform::setPosition(const Vector2<float>& p) + { + setPosition(p.x, p.y); + } + + void Transform::setPosition(float x, float y) + { + mPosition.set(x, y); + } + + Vector2<float> Transform::getPosition() const + { + return mPosition; + } + + void Transform::move(float x, float y) + { + mPosition.x += x; + mPosition.y += y; + } + + void Transform::move(const Vector2<float>& v) + { + move(v.x, v.y); + } + + void Transform::setOrigin(float x, float y) + { + mOrigin.set(x, y); + } + + Vector2<float> Transform::getOrigin() const + { + return mOrigin; + } + + void Transform::setRotation(float r) + { + mRotation = r; + } + + float Transform::getRotation() const + { + return mRotation; + } + + void Transform::rotate(float r) + { + mRotation += r; + } + + Matrix Transform::getMatrix() const + { + Matrix m; + m.setTransformation(mPosition.x, mPosition.y, mRotation, mScale.x, mScale.y, mOrigin.x, mOrigin.y); + return m; + } + + } +}
\ No newline at end of file diff --git a/src/libjin/Math/je_transform.h b/src/libjin/Math/je_transform.h new file mode 100644 index 0000000..cb1f0ee --- /dev/null +++ b/src/libjin/Math/je_transform.h @@ -0,0 +1,49 @@ +#ifndef __JE_TRANSFORM_H__ +#define __JE_TRANSFORM_H__ + +#include "je_matrix.h" +#include "je_vector2.hpp" + +namespace JinEngine +{ + namespace Math + { + + class Transform + { + public: + Transform(); + + void set(float x, float y, float sx, float sy, float r, float ox, float oy); + + void setScale(float sx, float sy); + Vector2<float> getScale() const; + void scale(float sx, float sy); + + void setPosition(float x, float y); + void setPosition(const Vector2<float>& p); + Vector2<float> getPosition() const; + void move(float x, float y); + void move(const Vector2<float>& v); + + void setOrigin(float x, float y); + Vector2<float> getOrigin() const; + + void setRotation(float r); + float getRotation() const; + void rotate(float r); + + Matrix getMatrix() const; + + private: + Vector2<float> mScale; + Vector2<float> mPosition; + Vector2<float> mOrigin; + float mRotation; + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/src/libjin/Math/je_vector2.hpp b/src/libjin/Math/je_vector2.hpp index e9599e1..b4cab44 100644 --- a/src/libjin/Math/je_vector2.hpp +++ b/src/libjin/Math/je_vector2.hpp @@ -31,6 +31,17 @@ namespace JinEngine data[1] = v.data[1]; } + Vector2<T> operator * (float n) + { + return Vector2<T>(data[0]*n, data[1]*n); + } + + void operator +=(const Vector2<T> v) + { + data[0] += v.data[0]; + data[1] += v.data[1]; + } + void set(T _x, T _y) { data[0] = _x; |