Public Group

# level output problem....

This topic is 3319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok so i've spent all day yesterday going over this and i just cant see anything wrong so im hoping someone on here can help... ok so im making breakout (the old brick smashing game) and the level i output doesnt draw correctly for some reason. i've messed with the x,y start position of the draw function yet that doesnt change the outcome... and i've completely run out of ideas as to what exactly it can be thats causing it. here is the output im getting the red block is the ball the blue block is a brick and the orange is the paddle...
// This is the constructor, destructor, initialise & draw functions from my level cpp.

CLevel::CLevel()
:m_iBricksRemaining(0)
,m_pBall(0)
,m_iWidth(0)
,m_iHeight(0)
{

}

CLevel::~CLevel()
{
while (m_vecBricks.size() > 0)
{
CBrick* pBrick = m_vecBricks[m_vecBricks.size() - 1];

m_vecBricks.pop_back();

delete pBrick;
}

delete m_pBall;
m_pBall = 0;
}

bool
CLevel::Initialise(int _iWidth, int _iHeight)
{
m_iWidth = _iWidth;
m_iHeight = _iHeight;

const float fBallVelX = 200.0f;
const float fBallVelY = 75.0f;

m_pBall = new CBall();
VALIDATE(m_pBall->Initialise(m_iWidth / 2.0f, m_iHeight / 2.0f,
fBallVelX, fBallVelY));

//Set the paddle's position to be centered on the x.
// and a little bit up from the bottom of the window.

const int kiNumBricks = 36;
const int kiStartX = 200;
const int kiGap = 5;

int iCurrentX = kiStartX;
int iCurrentY = 800;

for(int i = 0; i < kiNumBricks; ++i)
{
CBrick* pBrick = new CBrick();
VALIDATE(pBrick->Initialise());

pBrick->SetX(static_cast<float>(iCurrentX));
pBrick->SetY(static_cast<float>(iCurrentY));

iCurrentX += static_cast<int>(pBrick->GetWidth()) + kiGap;

if (iCurrentX > _iWidth)
{
iCurrentX = kiStartX;
iCurrentY += 200;
}
m_vecBricks.push_back(pBrick);
}
SetBricksRemaining(kiNumBricks);

return(true);
}

void
CLevel::Draw()
{
for(unsigned int i = 0; i < m_vecBricks.size(); ++i)
{
m_vecBricks->Draw();
}

m_pBall->Draw();

DrawScore();
}

i also have a few collision algorithms if they could be effecting how the level gets intialised but i dont think that could be it. so i've been pouring over the level.cpp and cant find anything, so im lost to exactly how to fix this problem... and help would be much appreciated!!..

##### Share on other sites
It's a shot in the dark but it could be that you are assuming your Y axis is one direction when in fact it's another direction completely.

For instance that you are assuming larger values of Y are higher on the screen and lower values of Y are lower on the screen when in fact it's the reverse.

##### Share on other sites
i see what you mean, which luckily i hadn't even noticed that the y starting position is meant to be 20, i think i might have changed it earlier and forgot about it....

but no that doesnt bring it off the top of the screen even adjusting the x axis isnt changing anything

##### Share on other sites
then the x and y variables must be set set to this position somewhere else in the code, can you show us the other code where you're setting/adjusting the x and y position?

##### Share on other sites
// These are my collision detection declarations.// I have a few getX() & getY() function calls in here.voidCLevel::Process(float _fDeltaTick){	void ProcessBallWallCollision();	void ProcessBallPaddleCollision();	void ProcessBallBrickCollision();	void ProcessCheckForWin();	void ProcessBallBounds();	for(unsigned int i = 0; i < m_vecBricks.size(); ++i)	{		m_vecBricks->Process(_fDeltaTick);	}	m_pPaddle->Process(_fDeltaTick);	m_pBall->Process(_fDeltaTick);}CPaddle*CLevel::GetPaddle() const{	return (m_pPaddle);}void CLevel::ProcessBallWallCollision(){	float fBallX = m_pBall->GetX();	float fBallY = m_pBall->GetY();	float fBallW = m_pBall->GetWidth();	float fBallH = m_pBall->GetHeight();	float fHalfBallW = fBallW / 2;	if (fBallX < fHalfBallW)	{		m_pBall->SetVelocityX(m_pBall->GetVelocityX() * -1);	}		else if (fBallX > m_iWidth - fHalfBallW)	{		m_pBall->SetVelocityX(m_pBall->GetVelocityX() * -1);		}	if (fBallY < 0 )	{		m_pBall->SetVelocityY(m_pBall->GetVelocityY() * -1);	}#ifdef CHEAT_BOUNCE_ON_BACK_WALL	if (fBallY > m_iHeight - fBallH)	{		m_pBall->SetVelocityX(m_pBall->GetVelocityX() * -1);	}#endif //CHEAT_BOUNCE_ON_BACK_WALL}voidCLevel::ProcessBallPaddleCollision(){	float fBallR = m_pBall->GetRadius();	float fBallX = m_pBall->GetX();	float fBallY = m_pBall->GetY();	float fPaddleX = m_pPaddle->GetX();	float fPaddleY = m_pPaddle->GetY();	float fPaddleW = m_pPaddle->GetWidth();	float fPaddleH = m_pPaddle->GetHeight();	if ((fBallX + fBallR > fPaddleX - fPaddleW / 2) &&		(fBallX - fBallR < fPaddleX + fPaddleW / 2) &&		(fBallX + fBallR > fPaddleY - fPaddleH / 2) &&		(fBallX - fBallR < fPaddleY + fPaddleH / 2))	{		m_pBall->SetY((fPaddleY - fPaddleH / 2) - fBallR);		m_pBall->SetVelocityY(m_pBall->GetVelocityY() * -1);	}}voidCLevel::ProcessBallBrickCollision(){	for(unsigned int i = 0; i < m_vecBricks.size(); ++i)	{		if (!m_vecBricks->IsHit())		{			float fBallR = m_pBall->GetRadius();			float fBallX = m_pBall->GetX();			float fBallY = m_pBall->GetY();			float fBrickX = m_vecBricks->GetX();			float fBrickY = m_vecBricks->GetY();			float fBrickW = m_vecBricks->GetWidth();			float fBrickH = m_vecBricks->GetHeight();							if	((fBallX + fBallR > fBrickX - fBrickW / 2) &&				(fBallX - fBallR < fBrickX + fBrickW / 2) &&				(fBallX + fBallR > fBrickY - fBrickH / 2) &&				(fBallX - fBallR < fBrickY + fBrickH / 2))			{				//Hit the front side of the brick....				m_pBall->SetY((fBrickY + fBrickH / 2.0f) + fBallR);				m_pBall->SetVelocityY(m_pBall->GetVelocityY() * -1);				m_vecBricks->SetHit(true);				SetBricksRemaining(GetBricksRemaining() - 1);			}		}	}}void CLevel::ProcessCheckForWin(){	for (unsigned int i = 0; i < m_vecBricks.size(); ++i)	{		if (!m_vecBricks->IsHit())		{			return;		}	}		CGame::GetInstance().GameOverWon();}voidCLevel::ProcessBallBounds(){	if (m_pBall->GetX() < 0)	{		m_pBall->SetX(0);	}	else if (m_pBall->GetX() > m_iWidth)	{		m_pBall->SetX(static_cast<float>(m_iWidth));	}	if (m_pBall->GetY() < 0)	{		m_pBall->SetY(0.0f);	}	else if (m_pBall->GetY() > m_iHeight)	{		CGame::GetInstance().GameOverLost();		//m_pBall->SetY(m_iHeight);	}}//these definitions are in my entity.cpp//entity.cppvoidCEntity::SetX(float _f){	m_fX = _f;}voidCEntity::SetY(float _f){	m_fY = _f;}

which i have the CLevel::intialise calling to set the starting positions of the bricks.. but as shown above i static_cast the iCurrentX & Y to SetX and Y.

• 18
• 11
• 19
• 9
• 52
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999811
×