I'm attempting to make Pong in SFML and trying to make a bot to play against. But when I try to reference for the balls Y position inside of the bot it's only giving me the initialized position and not updating for the current position. The position does update inside of the ball script itself but when taking it to the bot it just stays put.I'll include the two CPP files just in case it can help.
Bot
#include "Albert.h"
#include <iostream>
void Albert::initVariables()
{
this->moveSpeed = 5.f;
}
void Albert::initShape()
{
this->shape.setFillColor(sf::Color::White);
this->shape.setSize(sf::Vector2f(10.f, 40.f));
this->shape.setOrigin(20.f, 10.f);
this->shape.setPosition(775.f, 300.f);
}
Albert::Albert()
{
this->initVariables();
this->initShape();
}
Albert::~Albert()
{
}
void Albert::Update(const sf::RenderTarget* target)
{
this->Brain();
this->ball.ballPos = this->ball.getBall().getPosition().y;
std::cout << this->ball.ballPos << "\n";
}
void Albert::Brain()
{
}
void Albert::Render(sf::RenderTarget* target)
{
target->draw(this->shape);
}
Ball
#include "Ball.h"
#include <iostream>
void Ball::initVariables()
{
this->start = true;
this->velocity = sf::Vector2f(-5.f, 0.f);
this->idfk = 0.f;
this->ballPos = this->ball.getPosition().y;
}
void Ball::initBall()
{
this->ball.setFillColor(sf::Color::White);
this->ball.setSize(sf::Vector2f(10.f, 10.f));
this->ball.setOrigin(10.f, 10.f);
this->ball.setPosition(400.f, 150.f);
}
Ball::Ball()
{
this->initVariables();
this->initBall();
}
Ball::~Ball()
{
}
const sf::RectangleShape Ball::getBall() const
{
return this->ball;
}
void Ball::Update(const sf::RenderTarget* target)
{
if (start)
{
GameStart();
start = false;
}
this->ballPos = this->ball.getPosition().y;
//std::cout << ballPos << "\n";
this->UpdateCollisions(target);
this->ball.move(velocity);
}
void Ball::UpdateCollisions(const sf::RenderTarget* target)
{
if (this->ball.getGlobalBounds().left <= 0.f)
{
this->ball.setPosition(400.f, 300.f);
start = true;
}
//Right
if (this->ball.getGlobalBounds().left + this->ball.getGlobalBounds().width >= target->getSize().x)
{
this->velocity.x *= -1;
//std::cout << velocity.x << ", " << velocity.y;
}
//Top
if (this->ball.getGlobalBounds().top <= 0.f)
{
this->velocity.y *= -1;
//std::cout << velocity.x << ", " << velocity.y;
}
//Bottom
if (this->ball.getGlobalBounds().top + this->ball.getGlobalBounds().height >= target->getSize().y)
{
this->velocity.y *= -1;
//std::cout << velocity.x << ", " << velocity.y;
}
}
void Ball::Bounce(const sf::RenderTarget* target)
{
std::cout << this->ball.getPosition().y << ", " << this->player.getPlayer().getPosition().y << ", " << this->player.getPlayer().getGlobalBounds().height << "\n";
idfk = (this->ball.getPosition().y - this->player.getPlayer().getPosition().y) / this->player.getPlayer().getSize().y;
std::cout << "NUMBER: " << idfk << "\n";
float y = idfk;
sf::Vector2f dir = sf::Vector2f(5.f, y);
this->velocity = dir;
std::cout << dir.x << ", " << dir.y << "\n";
}
void Ball::GameStart()
{
int random = rand() % 2;
if (random == 0)
this->velocity = sf::Vector2f(-5.f, 2.5f);
else
this->velocity = sf::Vector2f(5.f, 2.5f);
}
void Ball::Render(sf::RenderTarget* target)
{
target->draw(this->ball);
}
New to SFML so my code probably looks weird.