-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAsteroid.cpp
72 lines (59 loc) · 2.34 KB
/
CAsteroid.cpp
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
// CGameObject.cpp
#define _USE_MATH_DEFINES
#include <algorithm>
#include <chrono>
#include <thread>
#include <cmath>
#include "stdafx.h"
#include "Constants.h"
#include "CGameObject.h"
#include "CAsteroid.h"
CAsteroid::CAsteroid(cv::Point2f position, std::string shape, std::string color)
: CGameObject(position, shape, color) {
m_velocity = cv::Point(static_cast<double>(rand() % (30 - 10) + 10),
static_cast<double>(rand() % (30 - 10) + 10));
m_angle = 0;
m_angleVelocity = 0;
m_acceleration = 0;
m_radius = 20;
m_objectType = "asteroid";
m_state = "active";
m_behavior = "hostile";
m_health = 1;
m_maxHealth = 1;
m_lives = 1;
m_scalarColor = rand() % (150 - 50) + 50;
}
CAsteroid::~CAsteroid() {
// destructor
}
void CAsteroid::update(cv::Rect& canvasSize) {
if (m_state == "active") {
// Get the current time
auto currentTime = std::chrono::high_resolution_clock::now();
// Calculate the time difference since the last update
std::chrono::duration<double> timeDiff = currentTime - lastUpdateTime;
double deltaTime = timeDiff.count(); // Delta time in seconds
// change direction
m_angle += m_angleVelocity;
// Check for border collision before updating the position
if (!is_border_collision(canvasSize)) {
// separate x and y components of velocity
m_position.x += m_velocity.x * deltaTime + 0.5 * cos(m_angle * PI / 180) * m_acceleration * pow(deltaTime, 2);
m_position.y += m_velocity.y * deltaTime + 0.5 * sin(m_angle * PI / 180) * m_acceleration * pow(deltaTime, 2);
// Update velocity based on acceleration
m_velocity.x += cos(m_angle * PI / 180) * m_acceleration * deltaTime;
m_velocity.y += sin(m_angle * PI / 180) * m_acceleration * deltaTime;
}
else
m_state = "inactive";
//border_bounce(canvasSize);
// Update last update time for the next iteration
lastUpdateTime = currentTime;
}
}
void CAsteroid::draw(cv::Mat& canvas) {
// Draw the blue circle representing the ship
cv::circle(canvas, m_position, m_radius, cv::Scalar(255, 255, 255), cv::FILLED);
cv::circle(canvas, m_position, m_radius - 2, cv::Scalar(m_scalarColor, m_scalarColor, m_scalarColor), cv::FILLED);
}