MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
anonymous_namespace{pong.cpp}::Ball Class Reference

Public Member Functions

 Ball (const glm::vec3 &pos, const glm::vec3 &vel, float r)
glm::mat4 modelMatrix () const
void resetBall ()
void update (float deltaTime, Paddle &paddle1, Paddle &paddle2, int &score1, int &score2)

Public Attributes

bool hitPaddle1 = false
bool hitPaddle2 = false
bool hitWall = false
glm::vec3 lastImpactPos {0.0f}
glm::vec3 position
float radius = 0.05f
glm::quat rollingOrientation {1.0f, 0.0f, 0.0f, 0.0f}
float speed = 1.0f
glm::vec3 velocity

Detailed Description

Definition at line 118 of file pong.cpp.

Constructor & Destructor Documentation

◆ Ball()

anonymous_namespace{pong.cpp}::Ball::Ball ( const glm::vec3 & pos,
const glm::vec3 & vel,
float r )
inlineexplicit

Definition at line 130 of file pong.cpp.

Member Function Documentation

◆ modelMatrix()

glm::mat4 anonymous_namespace{pong.cpp}::Ball::modelMatrix ( ) const
inlinenodiscard

Definition at line 150 of file pong.cpp.

150 {
151 glm::mat4 model(1.0f);
152 model = glm::translate(model, position);
153 model *= glm::mat4_cast(rollingOrientation);
154 model = glm::scale(model, glm::vec3(radius));
155 return model;
156 }

◆ resetBall()

void anonymous_namespace{pong.cpp}::Ball::resetBall ( )
inline

Definition at line 133 of file pong.cpp.

133 {
134 position = glm::vec3(0.0f, 0.0f, 0.0f);
135 rollingOrientation = glm::quat{1.0f, 0.0f, 0.0f, 0.0f};
136 const float angle = glm::radians(static_cast<float>(std::rand() % 120 - 60));
137 speed = 1.0f;
138
139 float vx = std::cos(angle);
140 float vy = std::sin(angle);
141
142 if (std::abs(vx) < 0.5f) {
143 vx = (vx < 0.0f) ? -0.5f : 0.5f;
144 }
145
146 vx *= (std::rand() % 2 == 0) ? 1.0f : -1.0f;
147 velocity = glm::normalize(glm::vec3(vx, vy, 0.0f)) * speed;
148 }

◆ update()

void anonymous_namespace{pong.cpp}::Ball::update ( float deltaTime,
Paddle & paddle1,
Paddle & paddle2,
int & score1,
int & score2 )
inline

Definition at line 158 of file pong.cpp.

158 {
159 hitPaddle1 = false;
160 hitPaddle2 = false;
161 hitWall = false;
162
163 const glm::vec3 movement = velocity * deltaTime;
164 updateRollingOrientation(movement);
165 position += movement;
166
167 if (position.y + radius > 1.0f) {
168 position.y = 1.0f - radius;
169 velocity.y = -velocity.y;
170 hitWall = true;
171 lastImpactPos = glm::vec3(position.x, 1.0f, 0.0f);
172 } else if (position.y - radius < -1.0f) {
173 position.y = -1.0f + radius;
174 velocity.y = -velocity.y;
175 hitWall = true;
176 lastImpactPos = glm::vec3(position.x, -1.0f, 0.0f);
177 }
178
179 handlePaddleCollision(paddle1);
180 handlePaddleCollision(paddle2);
181
182 if (position.x - radius < -1.8f) {
183 ++score2;
184 resetBall();
185 return;
186 }
187 if (position.x + radius > 1.8f) {
188 ++score1;
189 resetBall();
190 }
191 }

Member Data Documentation

◆ hitPaddle1

bool anonymous_namespace{pong.cpp}::Ball::hitPaddle1 = false

Definition at line 124 of file pong.cpp.

◆ hitPaddle2

bool anonymous_namespace{pong.cpp}::Ball::hitPaddle2 = false

Definition at line 125 of file pong.cpp.

◆ hitWall

bool anonymous_namespace{pong.cpp}::Ball::hitWall = false

Definition at line 126 of file pong.cpp.

◆ lastImpactPos

glm::vec3 anonymous_namespace{pong.cpp}::Ball::lastImpactPos {0.0f}

Definition at line 127 of file pong.cpp.

127{0.0f};

◆ position

glm::vec3 anonymous_namespace{pong.cpp}::Ball::position

Definition at line 120 of file pong.cpp.

◆ radius

float anonymous_namespace{pong.cpp}::Ball::radius = 0.05f

Definition at line 122 of file pong.cpp.

◆ rollingOrientation

glm::quat anonymous_namespace{pong.cpp}::Ball::rollingOrientation {1.0f, 0.0f, 0.0f, 0.0f}

Definition at line 128 of file pong.cpp.

128{1.0f, 0.0f, 0.0f, 0.0f};

◆ speed

float anonymous_namespace{pong.cpp}::Ball::speed = 1.0f

Definition at line 123 of file pong.cpp.

◆ velocity

glm::vec3 anonymous_namespace{pong.cpp}::Ball::velocity

Definition at line 121 of file pong.cpp.


The documentation for this class was generated from the following file: