Skip to content
Snippets Groups Projects
Commit 37f54d19 authored by CARDILE VINCENT's avatar CARDILE VINCENT
Browse files

update physique

parent 30ee4521
No related merge requests found
......@@ -3,6 +3,7 @@
#include <memory>
#include <vector>
#include <cmath>
#include "GridHandle.hpp"
#include "Player.hpp"
#include "Ball.hpp"
......@@ -19,6 +20,7 @@ private:
static void brickRectCollision(Ball* ball, Brick<int>* brick);
static void brickTriCollision(Ball* ball, Brick<int>* brick);
static void brickHexCollision(Ball* ball, Brick<int>* brick);
static const double PI;
public:
static int_fast8_t collisionBall(std::shared_ptr<Player> player, std::shared_ptr<GridHandle> grid, Ball* ball);
......
#include "../includes/Physics.hpp"
static int cont = 0;
/**
* @brief Look for collision between the ball and the player and change the direction of the ball
* @return void
......@@ -24,7 +26,7 @@ Physics::collisionBallPlayer(std::shared_ptr<Player> player, Ball* ball)
} else if (ball->getX() >= player->getX() + 5*player->getWidth()/8 && ball->getX() < player->getX() + 6*player->getWidth()/8)
{
ball->setDirectionX(0.5);
} else if (ball->getX() >= player->getX() + 3*player->getWidth()/8 && ball->getX() < player->getX() + 5*player->getWidth()/8)
} else if (ball->getX() >= player->getX() + 3.5*player->getWidth()/8 && ball->getX() < player->getX() + 4.5*player->getWidth()/8)
{
ball->setDirectionX(0);
}
......@@ -66,21 +68,52 @@ Physics::collisionBallWall(Ball* ball)
void
Physics::brickRectCollision(Ball* ball, Brick<int>* brick)
{
// for (int dy = -ball->getRadius(); dy <= ball->getRadius(); ++dy)
// {
// for (int dx = -ball->getRadius(); dx <= ball->getRadius(); ++dx)
// {
// if (dx * dx + dy * dy == ball->getRadius() * ball->getRadius())
// {
// if(ball->getX() + dx >= brick->getX()
// && ball->getX() + dx <= brick->getX() + brick->getW()
// && ball->getY() + dy >= brick->getY()
// && ball->getY() + dy <= brick->getY() + brick->getH())
// {
// if(ball->getY() + dy > brick->getY() && ball->getDirectionY() > 0
// || ball->getY() + dy < brick->getY() + brick->getH() && ball->getDirectionY() < 0)
// {
// std::cout << "CollisionY " << cont << " at " << ball->getY() + dy << " with " << brick->getY() << " or " << brick->getY() + brick->getH() << std::endl;
// ball->setDirectionY(ball->getDirectionY() * -1);
// } else
// if(ball->getX() + dx > brick->getX() && ball->getDirectionX() > 0
// || ball->getX() + dx < brick->getX() + brick->getW() && ball->getDirectionX() < 0)
// {
// std::cout << "CollisionX " << cont << " at " << ball->getX() + dx << " with " << brick->getX() << " or " << brick->getY() + brick->getW() << std::endl;
// ball->setDirectionX(ball->getDirectionX() * -1);
// }
// brick->gotHit();
// cont++;
// }
// }
// }
// }
if(ball->getX() + ball->getRadius() >= brick->getX()
&& ball->getX() - ball->getRadius() <= brick->getX() + brick->getW()
&& ball->getY() + ball->getRadius() >= brick->getY()
&& ball->getY() - ball->getRadius() <= brick->getY() + brick->getH())
{
if(ball->getX() < brick->getX() && ball->getDirectionX() > 0
|| ball->getX() > brick->getX() + brick->getW() && ball->getDirectionX() < 0)
{
ball->setDirectionX(ball->getDirectionX() * -1);
}
if(ball->getY() < brick->getY() && ball->getDirectionY() > 0
|| ball->getY() > brick->getY() + brick->getH() && ball->getDirectionY() < 0)
{
ball->setDirectionY(ball->getDirectionY() * -1);
}
else if(ball->getX() < brick->getX() && ball->getDirectionX() > 0
|| ball->getX() > brick->getX() + brick->getW() && ball->getDirectionX() < 0)
{
ball->setDirectionX(ball->getDirectionX() * -1);
}
brick->gotHit();
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment