-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.cpp
More file actions
117 lines (97 loc) · 2.69 KB
/
App.cpp
File metadata and controls
117 lines (97 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "App.h"
#include "Circle.h"
#include "Line.h"
#include "Object.h"
#include "Shape.h"
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/Graphics/Vertex.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
const bool App::running() const
{
return window->isOpen();
}
void App::initWindow()
{
videoMode = sf::VideoMode(800, 600);
window = new sf::RenderWindow(videoMode, "Simple physics", sf::Style::Default);
window->setFramerateLimit(60);
core = new Core();
}
App::App()
{
initWindow();
}
App::~App()
{
delete window;
}
void App::pollEvents()
{
while (window->pollEvent(sfmlEvent))
{
// CORE POLL EVENTS
core->pollEvents(*window,sfmlEvent);
// WINDOW EVENTS
switch (sfmlEvent.type)
{
case sf::Event::Closed:
window->close();
break;
case sf::Event::KeyPressed:
if (sfmlEvent.key.code == sf::Keyboard::Escape)
{
window->close();
}
break;
}
}
}
void App::collisionCircleLine(Object* circle, Line* line)
{
sf::Vector2f p = sf::Vector2f(circle->getPosition()); //center of circle
sf::Vector2f s = line->getPoints()[0].position; //point at start of line
sf::Vector2f e = line->getPoints()[1].position; //point at ent of line
sf::Vector2f ps = p - s;
sf::Vector2f se = e - s;
float lengthLine = (e.x - s.x) * (e.x - s.x) + (e.y - s.y) * (e.y - s.y);
float t = ((ps.x * se.x) + (ps.y * se.y)) / lengthLine; //point of normal on line
sf::Vector2f st;
st.x = s.x + t*se.x;
st.y = s.y + t*se.y;
sf::Vector2f distance = p - st;
float distanceBetween = sqrtf((distance.x*distance.x) + (distance.y*distance.y));
sf::Vector2f normal = distance / distanceBetween;
float dotProductNormal = circle->getVelocity().x*normal.x +
circle->getVelocity().y*normal.y;
sf::Vector2f tangential = sf::Vector2f(-normal.y, normal.x);
float dotProductTangential = circle->getVelocity().x*tangential.x +
circle->getVelocity().y * tangential.y;
if (distanceBetween <= circle->getRadius())
{
if (t > -0.f && t < 1.f)
{
circle->setVelocity(sf::Vector2f(-normal.x*dotProductNormal + tangential.x*dotProductTangential,
-normal.y*dotProductNormal + tangential.y*dotProductTangential));
float overlap = distanceBetween - circle->getRadius();
circle->setPosition(p.x - distance.x * overlap / distanceBetween,
p.y -distance.y * overlap / distanceBetween);
}
}
}
void App::update()
{
float deltaTime = 0.f;
deltaTime = clock.restart().asSeconds();
pollEvents();
core->update(*window, deltaTime);
}
void App::render()
{
window->clear();
window->draw(*core);
window->display();
}