diff --git a/Example/MyPongScene.cpp b/Example/MyPongScene.cpp index f8a3983..6aade5b 100644 --- a/Example/MyPongScene.cpp +++ b/Example/MyPongScene.cpp @@ -16,6 +16,8 @@ #define BALL_THICKNESS 25.0f +#define HALF_BALL_THICKNESS 12.5f + #define PADDLE_SPEED 5.0f @@ -77,6 +79,32 @@ void MyPongScene::update(float delta_time, clock_t clock) { m_ball_pose.translate(glm::vec3(m_ball_direction * m_ball_speed * delta_time, 0.0f)); + vec3 ball_position = m_ball_pose.get_position(); + + // Bounce Top and Bottom + + if (ball_position.y + HALF_BALL_THICKNESS + OUTLINE_INWARDS > m_screen_size.y / 2.0f) + { + m_ball_direction.y = -glm::abs(m_ball_direction.y); + } + + if (ball_position.y - HALF_BALL_THICKNESS - OUTLINE_INWARDS < -m_screen_size.y / 2.0f) + { + m_ball_direction.y = glm::abs(m_ball_direction.y); + } + + // Bounce Left Right (temp) + + if (ball_position.x + HALF_BALL_THICKNESS + OUTLINE_INWARDS > m_screen_size.x / 2.0f) + { + m_ball_direction.x = -glm::abs(m_ball_direction.x); + } + + if (ball_position.x - HALF_BALL_THICKNESS - OUTLINE_INWARDS < -m_screen_size.x / 2.0f) + { + m_ball_direction.x = glm::abs(m_ball_direction.x); + } + m_ball_batch.reset_rendered(); m_ball_batch.add_rendered(m_ball_pose); diff --git a/OpenGLEngine/Poseable.h b/OpenGLEngine/Poseable.h index 775e058..5d572e6 100644 --- a/OpenGLEngine/Poseable.h +++ b/OpenGLEngine/Poseable.h @@ -27,8 +27,11 @@ namespace charcoal void rotate(const vec3& axis, float angle); void set_orientation_matrix(const mat4& orientation_matrix) { m_orientation_matrix = orientation_matrix; } + const mat4& get_orientation_matrix() const { return m_orientation_matrix; } + vec3 get_position() const { return vec3(m_orientation_matrix[3][0], m_orientation_matrix[3][1], m_orientation_matrix[3][2]); } + protected: mat4 m_orientation_matrix; };