Commit 244e11f3 authored by Jonas Johan Solsvik's avatar Jonas Johan Solsvik 🎮
Browse files

Add gravity

parent c1abf052
......@@ -3,9 +3,14 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
using Thing = sf::RectangleShape;
using ThingVector = std::vector<sf::RectangleShape>;
using ThingIDVector = std::vector<unsigned>;
struct Thing
{
sf::Vector2f velocity{0, 0};
sf::RectangleShape shape{};
};
using ThingVector = std::vector<Thing>;
using ThingIDVector = std::vector<uint>;
auto eventsUpdate(
sf::RenderWindow &window,
......@@ -16,11 +21,19 @@ auto physicsUpdate(
const ThingIDVector &dynamicThings,
const ThingIDVector &staticThings) -> void;
auto physicsUpdate(
ThingVector &things,
const sf::Time deltaTime,
const ThingIDVector &dynamicThings,
const ThingIDVector &staticThings) -> void;
auto physicsTestCollision(
Thing &thing,
Thing &otherThing) -> void;
auto animationUpdate(ThingVector &things) -> void;
auto animationUpdate(
ThingVector &things,
const sf::Time deltaTime) -> void;
auto renderUpdate(
sf::RenderWindow &window,
......@@ -39,41 +52,45 @@ auto main() -> int
camera.setSize(WIDTH, HEIGHT);
window.setView(camera);
// # Create ground
Thing ground{sf::Vector2f{4000, 20}};
ground.setPosition(-2000, 0);
ground.setFillColor(sf::Color::White);
// # Create players
const auto PLAYER_SIZE = sf::Vector2f{50.0f, 50.0f};
const auto PLAYER_ORIGIN = sf::Vector2f{PLAYER_SIZE.x / 2.0f, PLAYER_SIZE.y / 2.0f};
Thing player1;
player1.setFillColor(sf::Color::Red);
player1.setSize(PLAYER_SIZE);
player1.setOrigin(PLAYER_ORIGIN);
player1.setPosition(sf::Vector2f{-100, -25});
Thing player2;
player2.setFillColor(sf::Color::Blue);
player2.setSize(PLAYER_SIZE);
player2.setOrigin(PLAYER_ORIGIN);
player2.setPosition(sf::Vector2f{100, -25});
ThingVector things = {
player1,
player2,
ground,
};
ThingVector things;
const ThingIDVector dynamicThings = {0, 1};
const ThingIDVector staticThings = {2};
{
// # Create players
const auto PLAYER_SIZE = sf::Vector2f{50.0f, 50.0f};
const auto PLAYER_ORIGIN = sf::Vector2f{PLAYER_SIZE.x / 2.0f, PLAYER_SIZE.y / 2.0f};
Thing player1{};
player1.shape.setFillColor(sf::Color::Red);
player1.shape.setSize(PLAYER_SIZE);
player1.shape.setOrigin(PLAYER_ORIGIN);
player1.shape.setPosition(sf::Vector2f{-100, -25});
things.emplace_back(player1);
Thing player2{};
player2.shape.setFillColor(sf::Color::Blue);
player2.shape.setSize(PLAYER_SIZE);
player2.shape.setOrigin(PLAYER_ORIGIN);
player2.shape.setPosition(sf::Vector2f{100, -25});
things.emplace_back(player2);
// # Create ground
Thing ground{};
ground.shape.setSize(sf::Vector2f{4000, 20});
ground.shape.setPosition(-2000, 0);
ground.shape.setFillColor(sf::Color::White);
things.emplace_back(ground);
}
sf::Clock clock; // starts the clock
while (window.isOpen())
{
const auto deltaTime = clock.getElapsedTime();
clock.restart();
eventsUpdate(window, camera);
physicsUpdate(things, dynamicThings, staticThings);
animationUpdate(things);
physicsUpdate(things, deltaTime, dynamicThings, staticThings);
animationUpdate(things, deltaTime);
renderUpdate(window, things);
}
......@@ -106,12 +123,21 @@ auto eventsUpdate(
auto physicsUpdate(
ThingVector &things,
const sf::Time deltaTime,
const ThingIDVector &dynamicThings,
const ThingIDVector &staticThings) -> void
{
const auto GRAVITY = sf::Vector2f{0, 10.0f * 30};
const auto dt = deltaTime.asSeconds();
for (const auto dynamicThingID : dynamicThings)
{
auto &dynamicThing = things[dynamicThingID];
// Add gravity to velocity
{
dynamicThing.velocity += GRAVITY * dt;
}
for (const auto otherDynamicThingID : dynamicThings)
{
if (dynamicThingID == otherDynamicThingID)
......@@ -133,8 +159,20 @@ auto physicsTestCollision(
{
}
auto animationUpdate(ThingVector &things) -> void
auto animationUpdate(
ThingVector &things,
const sf::Time deltaTime) -> void
{
for (auto &thing : things)
{
const auto position = thing.shape.getPosition();
const auto velocity = thing.velocity;
const auto dt = deltaTime.asSeconds();
thing.shape.setPosition(
position.x + velocity.x * dt,
position.y + velocity.y * dt);
}
}
auto renderUpdate(
......@@ -144,7 +182,7 @@ auto renderUpdate(
window.clear();
for (auto &thing : things)
{
window.draw(thing);
window.draw(thing.shape);
}
window.display();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment