-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cpp
More file actions
107 lines (93 loc) · 3.58 KB
/
Copy pathBall.cpp
File metadata and controls
107 lines (93 loc) · 3.58 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
#include "Ball.h"
#include "Game.h"
#include <iostream>
#include <cmath>
#include <SDL_image.h>
Ball::Ball(float posx, float posy, float r, int ID, SDL_Renderer* renderer, const char* texturePath)
: renderer(renderer), texture(nullptr), isPocketed(false) {
id = ID;
position.setX(posx);
position.setY(posy);
radius = r;
texPos.setX(position.getX() - radius);
texPos.setY(position.getY() - radius);
std::cout << "Ball initialized at position (" << posx << ", " << posy << ") with velocity (" << velocity.getX() << ", " << velocity.getY() << ")" << std::endl;
if (texturePath) {
SDL_Surface* surface = IMG_Load(texturePath);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
else {
std::cerr << "Unable to load texture: " << IMG_GetError() << std::endl;
}
}
}
void Ball::printBall() {
std::cout << "Ball" << std::endl;
std::cout << id << std::endl;
std::cout << position.getX() << std::endl;
std::cout << position.getY() << std::endl;
std::cout << velocity.getX() << std::endl;
std::cout << velocity.getY() << std::endl;
std::cout << mass << std::endl;
}
void Ball::printID() {
std::cout << id << std::endl;
}
void Ball::resetCue() {
position.setX(350.f);
position.setY(335.f);
velocity.setX(0.f);
velocity.setY(0.f);
}
void Ball::draw() {
if (isPocketed) return;
if (texture) {
SDL_Rect destRect;
destRect.x = static_cast<int>(texPos.getX());
destRect.y = static_cast<int>(texPos.getY());
destRect.w = static_cast<int>(radius * 2);
destRect.h = static_cast<int>(radius * 2);
SDL_RenderCopy(renderer, texture, NULL, &destRect);
}
else {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Êðàñíûé öâåò
for (int w = 0; w < radius * 2; w++) {
for (int h = 0; h < radius * 2; h++) {
int dx = radius - w; // Ãîðèçîíòàëüíîå îòêëîíåíèå
int dy = radius - h; // Âåðòèêàëüíîå îòêëîíåíèå
if ((dx * dx + dy * dy) <= (radius * radius)) {
SDL_RenderDrawPoint(renderer, static_cast<int>(position.getX()) + dx, static_cast<int>(position.getY()) + dy);
}
}
}
}
}
void Ball::updatePhysics() {
//std::cout << "Before update - Position: (" << position.getX() << ", " << position.getY() << ") Velocity: (" << velocity.getX() << ", " << velocity.getY() << ")" << std::endl;
velocity.setX(velocity.getX() * 0.997);
velocity.setY(velocity.getY() * 0.997);
if (abs(velocity.getX() * velocity.getX() + velocity.getY() * velocity.getY()) < 0.0001) {
velocity.setX(0);
velocity.setY(0);
}
position.setX(position.getX() + velocity.getX());
position.setY(position.getY() + velocity.getY());
// std::cout << "After update - Position: (" << position.getX() << ", " << position.getY() << ")" << std::endl;
}
void Ball::update() {
if (position.getX() < 0)
position.setX(Game::screenSize.getX());
if (position.getX() > Game::screenSize.getX())
position.setX(0);
if (position.getY() < 0)
position.setY(Game::screenSize.getY());
if (position.getY() > Game::screenSize.getY())
position.setY(0);
texPos.setX(position.getX() - radius);
texPos.setY(position.getY() - radius);
}
bool Ball::isMoving() const {
return velocity.getX() != 0 || velocity.getY() != 0;
}