diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae2c6ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.exe +*.ttf +SFML diff --git a/Force/.vscode/settings.json b/Force/.vscode/settings.json new file mode 100644 index 0000000..5fab886 --- /dev/null +++ b/Force/.vscode/settings.json @@ -0,0 +1,28 @@ +{ + "files.associations": { + "vector": "cpp", + "ios": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "deque": "cpp", + "exception": "cpp", + "fstream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "set": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "utility": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xstring": "cpp", + "xtree": "cpp", + "xutility": "cpp", + "iostream": "cpp", + "cmath": "cpp", + "iosfwd": "cpp" + } +} \ No newline at end of file diff --git a/Force/Entity.cpp b/Force/Entity.cpp new file mode 100644 index 0000000..710157b --- /dev/null +++ b/Force/Entity.cpp @@ -0,0 +1,150 @@ +#include "Entity.h" + +Entity::Entity() +{ + velocity.x = velocity.y = 0.0f; + position.x = position.y = 0.0f; + vertex.color = sf::Color::White; +} + +void Entity::ApplyForce(sf::Vector2f force, float deltaTime) +{ + velocity += force * deltaTime; +} + +void Entity::ApplyGravity(float deltaTime) +{ + velocity.y -= 10 * deltaTime; +} + +void Entity::ApplyVelocity(float deltaTime) +{ + position += velocity * deltaTime; +} + +void Entity::ApplyDrag(float drag, float deltaTime) +{ + velocity -= velocity * drag * deltaTime; +} + +sf::Vector2f Entity::GetPosition(bool inverse) +{ + sf::Vector2f result = position; + if(inverse) result.y *= -1; + return result; +} + +sf::Vertex &Entity::GetVertex() +{ + vertex.position = GetPosition(); + return vertex; +} + +void Entity::SetPosition(float x, float y, bool inverse) +{ + position.x = x; + position.y = !inverse ? -y : y; +} + +void Entity::SetVelocity(float x, float y, bool inverse) +{ + velocity.x = x; + velocity.y = !inverse ? -y : y; +} + +EntityPool::EntityPool() +{ + drag = -1; + array.clear(); +} + +EntityPool::~EntityPool() +{ + array.clear(); +} + +void EntityPool::AddNewEntity(float x, float y, bool inverse) +{ + Entity newEntity; + newEntity.SetPosition(x, inverse ? -y : y); + array.push_back(newEntity); +} + +void EntityPool::ForceToPoint(sf::Vector2f point, float force, float deltaTime, bool fixedForce, bool inverse) +{ + int i; + float magnitude; + sf::Vector2f direction; + + for (i = 0; i < array.size(); i++) + { + direction = point - array[i].GetPosition(); + magnitude = sqrt(direction.x * direction.x + direction.y * direction.y); + direction /= magnitude; + direction.y = inverse ? -direction.y : direction.y; + if(!fixedForce) direction *= (float)(1.0 / sqrt(magnitude) + 0.1); + array[i].ApplyForce(direction * force, deltaTime); + } +} + +void EntityPool::ForceFromPoint(sf::Vector2f point, float force, float deltaTime, bool fixedForce, bool inverse) +{ + int i; + float magnitude; + sf::Vector2f direction; + + for (i = 0; i < array.size(); i++) + { + direction = array[i].GetPosition() - point; + magnitude = sqrt(direction.x * direction.x + direction.y * direction.y); + direction /= magnitude; + direction.y = inverse ? -direction.y : direction.y; + if(!fixedForce) direction *= (float)(1.0 / sqrt(magnitude) + 0.1); + array[i].ApplyForce(direction * force, deltaTime); + } +} + +void EntityPool::RemoveEntities(int count) +{ + int i; + for (i = 0; i < count; i++) + array.pop_back(); +} + +void EntityPool::Update(float deltaTime) +{ + int i; + for (i = 0; i < array.size(); i++) + { + if(isGravityOn) + array[i].ApplyGravity(deltaTime); + if(drag > 0.0) + array[i].ApplyDrag(drag, deltaTime); + array[i].ApplyVelocity(deltaTime); + } +} + +void EntityPool::SetGravity(bool isActive) +{ + isGravityOn = isActive; +} + +void EntityPool::SetDrag(float drag) +{ + this -> drag = drag; +} + +float EntityPool::GetDrag() +{ + return drag; +} + +Entity &EntityPool::operator[](int index) +{ + return array[index]; +} + +int EntityPool::Size() +{ + return array.size(); +} diff --git a/Force/Entity.h b/Force/Entity.h new file mode 100644 index 0000000..7a2961a --- /dev/null +++ b/Force/Entity.h @@ -0,0 +1,44 @@ +#include +#include +#include + +class Entity; + +class EntityPool +{ + private: + std::vector array; + bool isGravityOn; + float drag; + public: + EntityPool(); + ~EntityPool(); + void AddNewEntity(float, float, bool = true); + void ForceToPoint(sf::Vector2f, float, float, bool = true, bool = true); + void ForceFromPoint(sf::Vector2f, float, float, bool = true, bool = true); + void RemoveEntities(int); + void Update(float); + void SetGravity(bool); + void SetDrag(float); + float GetDrag(); + Entity &operator[](int); + int Size(); +}; + +class Entity +{ + private: + sf::Vertex vertex; + sf::Vector2f position; + sf::Vector2f velocity; + public: + Entity(); + void ApplyForce(sf::Vector2f, float); + void ApplyGravity(float); + void ApplyVelocity(float); + void ApplyDrag(float, float); + void SetPosition(float, float, bool = true); + void SetVelocity(float, float, bool = true); + sf::Vertex &GetVertex(); + sf::Vector2f GetPosition(bool = true); +}; diff --git a/Force/compile.bat b/Force/compile.bat new file mode 100644 index 0000000..c71ef5f --- /dev/null +++ b/Force/compile.bat @@ -0,0 +1,2 @@ +mingw32-make +main.exe diff --git a/Force/main.cpp b/Force/main.cpp new file mode 100644 index 0000000..f3b4657 --- /dev/null +++ b/Force/main.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include "Entity.h" + +#define WindowSize sf::VideoMode(960, 540) +#define WindowStyle sf::Style::Titlebar | sf::Style::Close +#define WindowTitle "Test Window Syntriax" + +sf::Vertex CreateVertex(double, double); + +int main() +{ + std::ostringstream textToDisplay; + sf::RenderWindow window(WindowSize, WindowTitle, WindowStyle); + sf::Event event; + sf::Clock clock; + sf::Text text; + sf::Font font; + sf::Vector2f vector; + float radiantValue; + float timePassed; + float deltaTime; + float force = 1000; + EntityPool pool; + bool isFocused = true; + bool isFixedForce = true; + pool.SetGravity(false); + pool.SetDrag(0.0); + + if (!font.loadFromFile("../OpenSans-Bold.ttf")) + return -1; + + text.setFont(font); + window.setVerticalSyncEnabled(true); + // window.setFramerateLimit(140); + + while (window.isOpen()) + { + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + else if (event.type == sf::Event::LostFocus) + isFocused = false; + else if (event.type == sf::Event::GainedFocus) + isFocused = true; + else if (event.type == sf::Event::KeyPressed) + { + if(sf::Keyboard::isKeyPressed(sf::Keyboard::R)) + pool.RemoveEntities(pool.Size()); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) + pool.SetDrag(2); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) + pool.SetDrag(1); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) + pool.SetDrag(0.5); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::F)) + pool.SetDrag(0.25); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::G)) + pool.SetDrag(0.1); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::H)) + pool.SetDrag(0.0); + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) + force = 100; + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::X)) + force = 500; + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::C)) + force = 1000; + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::V)) + force = 2500; + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::B)) + force = 10000; + else if(sf::Keyboard::isKeyPressed(sf::Keyboard::L)) + isFixedForce = !isFixedForce; + } + } + if(!isFocused) + { + timePassed = clock.getElapsedTime().asSeconds(); + continue; + } + + deltaTime = clock.getElapsedTime().asSeconds() - timePassed; + timePassed = clock.getElapsedTime().asSeconds(); + + pool.Update(deltaTime); + + textToDisplay.str(""); + textToDisplay.clear(); + textToDisplay << "Entity Count: " << pool.Size() << + "\nDrag: " << pool.GetDrag() << + "\nForce: " << force << + "\nForce Mode: " << (isFixedForce ? "Fixed" : "Distance") << + "\nFPS: " << (1.0 / deltaTime); + text.setString(textToDisplay.str()); + + + if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) + { + vector = (sf::Vector2f) sf::Mouse::getPosition(window); + for (int i = 0; i < 18; i++) + { + radiantValue = ((float)i / 18.0 * 7.28) + timePassed; + pool.AddNewEntity(vector.x + sin(radiantValue) * 100.0, vector.y + cos(radiantValue) * 100.0); + } + } + if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) + pool.ForceToPoint((sf::Vector2f) sf::Mouse::getPosition(window), force, deltaTime, isFixedForce); + if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Middle)) + pool.ForceFromPoint((sf::Vector2f) sf::Mouse::getPosition(window), force, deltaTime, isFixedForce); + + window.clear(); + for (int i = 0; i < pool.Size(); i++) + window.draw(&pool[i].GetVertex(), 1, sf::PrimitiveType::Points); + window.draw(text); + // window.draw(&player.GetVertex(), 1, sf::PrimitiveType::Points); + window.display(); + } + + return 0; +} + +sf::Vertex CreateVertex(double x, double y) +{ + return sf::Vertex(sf::Vector2f(x, y)); +} \ No newline at end of file diff --git a/Force/makefile b/Force/makefile new file mode 100644 index 0000000..57b0d83 --- /dev/null +++ b/Force/makefile @@ -0,0 +1,2 @@ +main.exe : *.cpp + g++ -o main.exe *.cpp ../SFML/*.a \ No newline at end of file