So here, I am trying to implement a Bounce logic whenever the object called Poo (followed a tutorial) hits the end of the screen. I copied my implementation for making any object not go off the screen since I tried reusing it and couldn’t find a way to implement the bounce, so I made another function, specifically for bouncing if it tries to go off the screen, but it still didn’t work. It only stops before going off the screen but it won’t bounce, the speed logic was outside the function before and now I tried putting it inside and still didn’t make a difference, please help. Also, the tutorial I’m following uses DirectX I think.
void Game::ComposeFrame()
{
if (!GameStarted) {
DrawTitle(GameTitle1X, GameTitle1Y);
}
else if (GameStarted) {
Character1(GuyPos1X, GuyPos1Y);
PooCollisionCheckImplementation();
Poo1X += Poo1vX;
Poo1Y += Poo1vY;
Poo2X += Poo2vX;
Poo2Y += Poo2vY;
Poo3X += Poo3vX;
Poo3Y += Poo3vY;
PooBorder(Poo1X, Poo1Y, Poo2vX, Poo2vY, PooWidth, PooHeight);
PooBorder(Poo2X, Poo2Y, Poo2vX, Poo2vY, PooWidth, PooHeight);
PooBorder(Poo3X, Poo3Y, Poo3vX, Poo3vY, PooWidth, PooHeight);
PooisEaten();
}
}
void Game::PooBorder(int& x, int& y, int& vx, int& vy, int Width, int Height)
{
int X = x + Width;
int Y = y + Height;
x += vx;
y += vy;
if (x <= 0) {
x = 0;
vx = -vx;
}
if (X >= gfx.ScreenWidth - 1)
{
x = gfx.ScreenWidth - Width;
vx = -vx;
}
if (y <= 0) {
y = 0;
vy = -vy;
}
if (Y >= gfx.ScreenHeight - 1)
{
y = gfx.ScreenHeight - Height;
vy = -vy;
}
}
Note that x += vx and y += vy was once outside the bounce function but it didn’t work either way.