-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.hpp
More file actions
41 lines (34 loc) · 1.22 KB
/
ball.hpp
File metadata and controls
41 lines (34 loc) · 1.22 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
#pragma once
#include <SFML/Graphics.hpp>
class Ball {
public:
sf::Vector2f position;
sf::Vector2f position_old;
sf::Vector2f acceleration;
float radius;
sf::Color color;
Ball(float x, float y, float r, sf::Color c)
: position(x, y), position_old(x, y), acceleration(0, 0), radius(r), color(c) {}
void update(float dt) {
sf::Vector2f velocity = position - position_old;
position_old = position;
position += velocity + acceleration * dt * dt;
acceleration = {};
}
void accelerate(sf::Vector2f force) {
acceleration += force;
}
void setVelocity(sf::Vector2f v, float dt) {
position_old = position - (v * dt);
}
sf::Vector2f getVelocity(float dt) const {
return (position - position_old) / dt;
}
void draw(sf::RenderWindow& window) const {
sf::CircleShape circle(radius);
circle.setFillColor(color);
circle.setOrigin(radius, radius);
circle.setPosition(position);
window.draw(circle);
}
};