forked from vittorioromeo/cppcon2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp03.cpp
More file actions
100 lines (77 loc) · 3.3 KB
/
Copy pathp03.cpp
File metadata and controls
100 lines (77 loc) · 3.3 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
// Copyright (c) 2014 Vittorio Romeo
// License: MIT License | http://opensource.org/licenses/MIT
// http://vittorioromeo.info | vittorio.romeo@outlook.com
// The ball moves! However, we need to find a way to stop it from
// leaving the window's bounds.
// By testing if the X coordinate of the ball exceeds the window's
// width or is less than 0 we can check if the ball left the window
// horizontally. The same principle applies for the vertical bounds.
// {Info: ball vs window collision}
#include <SFML/Graphics.hpp>
constexpr unsigned int wndWidth{800}, wndHeight{600};
class Ball
{
public:
static const sf::Color defColor;
static constexpr float defRadius{10.f};
static constexpr float defVelocity{8.f};
sf::CircleShape shape;
sf::Vector2f velocity{-defVelocity, -defVelocity};
Ball(float mX, float mY)
{
shape.setPosition(mX, mY);
shape.setRadius(defRadius);
shape.setFillColor(defColor);
shape.setOrigin(defRadius, defRadius);
}
// We will need to get very often the ball's left/right/top/bottom
// bounds. Let's define some simple getters to help us.
float x() const noexcept { return shape.getPosition().x; }
float y() const noexcept { return shape.getPosition().y; }
float left() const noexcept { return x() - shape.getRadius(); }
float right() const noexcept { return x() + shape.getRadius(); }
float top() const noexcept { return y() - shape.getRadius(); }
float bottom() const noexcept { return y() + shape.getRadius(); }
void update()
{
// We need to keep the ball "inside the window".
// The most common (and probably best) way of doing this, and
// of dealing with any kind of collision detection, is moving
// the object first, then checking if it's intersecting
// something.
// If the test is positive, we simply respond to the collision
// by altering the object's position and/or velocity.
// Therefore, we begin by moving the ball.
shape.move(velocity);
// After the ball has moved, it may be "outside the window".
// We need to check every direction and respond by changing
// the velocity.
// If it's leaving towards the left, we need to set
// horizontal velocity to a positive value (towards the right).
if(left() < 0) velocity.x = defVelocity;
// Otherwise, if it's leaving towards the right, we need to
// set horizontal velocity to a negative value (towards the
// left).
else if(right() > wndWidth) velocity.x = -defVelocity;
// The same idea can be applied for top/bottom collisions.
if(top() < 0) velocity.y = defVelocity;
else if(bottom() > wndHeight) velocity.y = -defVelocity;
}
void draw(sf::RenderWindow& mTarget) { mTarget.draw(shape); }
};
const sf::Color Ball::defColor{sf::Color::Red};
int main()
{
Ball ball{wndWidth / 2.f, wndHeight / 2.f};
sf::RenderWindow window{{wndWidth, wndHeight}, "Arkanoid - 3"};
window.setFramerateLimit(60);
while(true)
{
window.clear(sf::Color::Black);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break;
ball.update();
ball.draw(window);
window.display();
}
return 0;
}