-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
271 lines (189 loc) · 6.49 KB
/
main.cpp
File metadata and controls
271 lines (189 loc) · 6.49 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <SFML/Graphics.hpp>
#include <Server.hpp>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cmath>
sf::Clock deltaClock;
float vectorLength(sf::Vector2f a) {
return sqrt((a.x*a.x) + (a.y*a.y));
}
float dot(sf::Vector2f a, sf::Vector2f b) {
return ((a.x * b.x) + (a.y * b.y));
}
float angle(sf::Vector2f a, sf::Vector2f b) {
float dotProd = dot(a,b);
// printf("dot: %f\n", dotProd);
float lenA = vectorLength(a);
float lenB = vectorLength(b);
float theta = dotProd/(lenA*lenB);
return acos(theta);
}
// Projection of a onto b
sf::Vector2f vectorProjection(sf::Vector2f a, sf::Vector2f b) {
// Calculate b unit vector
float bLength = hypot(b.x,b.y);
sf::Vector2f unitB(b.x/bLength, b.y/bLength);
return sf::Vector2f(a.x * b.x, a.y * b.y);
}
void translateObject(sf::CircleShape* object, sf::Vector2f aVec){
object->setPosition(object->getPosition() + aVec);
}
void bounceOffBounds(sf::CircleShape* object, sf::Vector2f* curVel) {
float x = curVel->x;
float y = curVel->y;
curVel->y = -y;
}
void bounceOffWall(sf::CircleShape* object, sf::Vector2f* curVel) {
float x = curVel->x;
float y = curVel->y;
curVel->x = -x;
curVel->y = y;
}
void bounceOffPlayer(sf::CircleShape* object, sf::Vector2f* curVel, float dif) {
float x = curVel->x;
float y = curVel->y;
// Ball hits lower racket
if(dif<0) {
curVel->y = sf::Vector2f(x, 20+(-dif)).y;
}
// Ball hits upper racket
if(dif>0) {
curVel->y = sf::Vector2f(x, 20+(-dif)).y;
}
// Ball hits middle of racket
if(dif=0) {
curVel->y = -y;
}
curVel->x = -x;
}
void centerOrigin(sf::RectangleShape* shape){
sf::Vector2f dim = shape->getSize();
shape->setOrigin(dim.x/2, dim.y/2);
}
void centerOrigin(sf::CircleShape* shape){
float rad = shape->getRadius();
shape->setOrigin(rad, rad);
}
int main()
{
sf::VideoMode vMode(500,500);
sf::RenderWindow window(vMode, "PONG");
// Wall
sf::RectangleShape wall(sf::Vector2f(10, window.getSize().y));
wall.setPosition(window.getSize().x-10,0);
wall.setFillColor(sf::Color::White);
// Player
sf::RectangleShape player(sf::Vector2f(40.0,160.0));
player.setFillColor(sf::Color::Black);
player.setOutlineColor(sf::Color::White);
player.setOutlineThickness(1.f);
centerOrigin(&player);
player.setPosition(45.0f,window.getSize().y/2);
bool inPlay = false;
// Bounds
sf::Vector2f boundDimension(window.getSize().x,3);
sf::RectangleShape upperBound(boundDimension);
sf::RectangleShape lowerBound(boundDimension);
upperBound.setPosition(0,0);
lowerBound.setPosition(0, window.getSize().y-3);
upperBound.setFillColor(sf::Color::Green);
lowerBound.setFillColor(sf::Color::Green);
// Ball
sf::CircleShape ball(25.f, 20);
ball.setFillColor(sf::Color::White);
centerOrigin(&ball);
float speed = 7000.f;
// Used to handle events
sf::Event event;
// User mouse
sf::Mouse mouse;
sf::FloatRect wBounds = wall.getGlobalBounds();
sf::Vector2f fireVelo(200,0);
sf::Rect<float> intersection;
sf::Vector2f normalLine(10,0);
bool toggle_switch = false;
while (window.isOpen())
{
window.clear();
sf::FloatRect bBounds = ball.getGlobalBounds();
sf::FloatRect pBounds = player.getGlobalBounds();
sf::FloatRect uBounds = upperBound.getGlobalBounds();
sf::FloatRect lBounds = lowerBound.getGlobalBounds();
float dt = deltaClock.restart().asSeconds();
if(!inPlay) {
ball.setPosition(player.getPosition().x+20+ball.getRadius(),player.getPosition().y);
}
while (window.pollEvent(event))
{
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
if (event.type == sf::Event::Closed)
window.close();
// Key mapping [(w,22),(a,0),(s,18),(d,3)]
// Space = 57
if (event.type == sf::Event::KeyPressed)
{
std::cout << event.key.code << std::endl;
// std::cout << 1.f/dt << std::endl; <- fps
if(event.key.code == 22 && !pBounds.intersects(uBounds)) {
player.move(0, player.getPosition().y - (player.getPosition().y + (speed * dt)));
}
if(event.key.code == 18 && !pBounds.intersects(lBounds)) {
player.move(0, player.getPosition().y - (player.getPosition().y - (speed * dt)));
}
if(event.key.code == 7) {
toggle_switch = !toggle_switch;
if(toggle_switch) {
printf("Angle calulation toggled on\n");
} else {
printf("Angle calulation toggled of\n");
}
}
// FIRE!!!
if(event.key.code == 57 & !inPlay) {
inPlay = true;
}
}
if (event.type == sf::Event::MouseButtonPressed)
{
sf::Vector2f mousePosition(mouse.getPosition(window));
std::cout << mousePosition.x << std::endl;
std::cout << mousePosition.y << std::endl;
sf::Vector2f ballPosition(ball.getPosition());
ball.setPosition(mousePosition);
}
}
if(bBounds.intersects(wBounds)) {
bounceOffWall(&ball, &fireVelo);
}
if(bBounds.intersects(uBounds)) {
bounceOffBounds(&ball, &fireVelo);
}
if(bBounds.intersects(lBounds)) {
bounceOffBounds(&ball, &fireVelo);
}
if(bBounds.intersects(pBounds, intersection) && inPlay) {
if(toggle_switch) {
float attack_angle = angle(fireVelo, normalLine);
printf("Ball hit the player at : %f%n", attack_angle);
toggle_switch = !toggle_switch;
}
bounceOffPlayer(&ball, &fireVelo, player.getPosition().y-ball.getPosition().y);
}
if(inPlay){
translateObject(&ball, fireVelo * dt);
}
window.draw(wall);
window.draw(player);
window.draw(ball);
window.draw(upperBound);
window.draw(lowerBound);
window.display();
}
return 0;
}