• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

return to breakout

27 posts in this topic

well I have decided to finish my breakout game using opengl and c++. I am still stuck on how to get the ball to bounce off the brick and get it to disappear. Then when it is removed I want the ball to pass through the space where the brick was. Please don't give me a lot of code. Also I am using magic numbers for now. Here is just a little bit of code. [code]if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f) { ystep = -ystep; g_bBlock[4].m_bActive_three=false; }[/code] well this code checks for a brick and then switches the ball's velocity and then turns off the brick. but it still bounces off where the brick was. I know I have asked this question before but I would like a fresh perspective. I am doing my best to finish this project.
0

Share this post


Link to post
Share on other sites
You already have an array with a bool inside that tells if a brick is active or not. You only do the collision check if the brick is still active.
2

Share this post


Link to post
Share on other sites

well I have decided to finish my breakout game using opengl and c++. I am still stuck on how to get the ball to bounce off the brick and get it to disappear. Then when it is removed I want the ball to pass through the space where the brick was. Please don't give me a lot of code. Also I am using magic numbers for now. Here is just a little bit of code.

if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
	{
	ystep = -ystep;
	g_bBlock[4].m_bActive_three=false;	
	}
well this code checks for a brick and then switches the ball's velocity and then turns off the brick. but it still bounces off where the brick was. I know I have asked this question before but I would like a fresh perspective. I am doing my best to finish this project.

simply check that the brick is still valid when your comparing to hit it, like so:

if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f && g_bBlock[4].m_bActive_three==true) { 
   ystep = -ystep; 
   g_bBlock[4].m_bActive_three=false; 
}

 

your naming convention is a bit odd(_three?)

 

edit: also, just to toss this out their for thought: what happens when your ball impacts the block along the side's rather than top or bottom?

Edited by slicer4ever
1

Share this post


Link to post
Share on other sites

As Endurion pointed out just use the bool in the condition for collision.

if(g_bBlock[4].m_bActive_three==true)
{
if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
{
ystep = -ystep;
g_bBlock[4].m_bActive_three=false;
}
}

But trust me, there are much more efficient ways of detecting collision and removing the blocks.  The one you are using is probably the easiest but not accurate in many cases.

Edited by Sam Sandeep
0

Share this post


Link to post
Share on other sites

I would agree with Endurion and Sam Sandeep.  Sounds like you are calculating collision even if the brick is "turned off".  I would also like to add why are you keeping the brick in memory at all?  If it is "destroyed" once the ball makes impact why don't you just completely remove it from memory?  Remove it from the active collection, free up memory don't have to waste time calculating for collision on it (even if it is turned off).

1

Share this post


Link to post
Share on other sites
Man, I really hate to be a dick, but...

http://www.gamedev.net/topic/636083-little-more-help/
http://www.gamedev.net/topic/635556-little-help/
http://www.gamedev.net/topic/635213-collision-detection/
http://www.gamedev.net/topic/634727-breakout-game/
http://www.gamedev.net/topic/633265-collision-detection/
http://www.gamedev.net/topic/625158-collision-question/
http://www.gamedev.net/topic/622320-collision-code/
http://www.gamedev.net/topic/622165-2d-collision/

The list goes on.

You have this habit of posting a question, getting some answers, maybe posting some final response like "Wow, that is a lot of code", then disappearing for a few weeks. When you come back, you are asking the same exact questions as before. I really hate discouraging anyone, but are you really certain that programming is for you? It just doesn't seem like you are actually learning anything or making any progress at all. There is nothing wrong with not being able to program, but there is something wrong with continuing to ask the same questions over and over, wasting the time and effort of everyone who has posted in your many threads trying to help you out.
2

Share this post


Link to post
Share on other sites

Sometimes I feel like Phil is trolling us, and he just chuckles when we post a lot of code to help him.

 

Then I think maybe he's just struggling very hard and just can't "get it".  It happens.  At least he's trying hard (if he's not trolling)

0

Share this post


Link to post
Share on other sites
If you're serious, then instead of posting another topic like this, go through all of your previous posts. Read the answers that other people posted, think about what they're saying, try to apply it to your problem. Your thread history has some good advice. Encapsulation. Separating logic from rendering. Collision detection. Go ahead and try to process it, then when you have specific new questions, come back here for clarification. Because the thing is, you are still pretty much doing collision "wrong". Previous answers in your other threads gave you some avenues to follow to learn how to do collision "right". It's really pointless to ignore that advice and keep chasing the wrong things and asking the same questions.
1

Share this post


Link to post
Share on other sites
ok blanc I am not the best programmer but I do enjoy it. I graduated from college with a b.s. in c.s. in 2005. I have actually learned a lot since then but there always seems to be more to learn.
0

Share this post


Link to post
Share on other sites

Phil, what did you not understand about this code I posted?

[code] 

struct TBrick {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  uint32_t Color;
  uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};
 
struct TBall {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  int XSpeed;
  int YSpeed;
};
 
#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels
 
// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];
 
// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
  for (int y = 0; y < BRICK_ROWS; y++) {
    BrickArray[x][y].XSize = BRICK_WIDTH;
    BrickArray[x][y].YSize = BRICK_HEIGHT;
    BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
    BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
    BrickArray[x][y].Color = 0xFFFFFFFF; // white
    BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick
  }
}
  
// in main loop do this:
// move Ball in X direction, then check collision
Ball.XLocation += Ball.XSpeed;
if (CheckCollision(Ball)) {
  // The ball hit something, move to original location and negate X speed
  Ball.XLocation -= Ball.XSpeed;
  Ball.XSpeed = -Ball.XSpeed;
}
 
// Do same for Y movement
Ball.YLocation += Ball.YSpeed;
if (CheckCollision(Ball)) {
  // The ball hit something, move to original location and negate Y speed
  Ball.YLocation -= Ball.YSpeed;
  Ball.YSpeed = -Ball.YSpeed;
}
 
RenderBricks();
 
... // the rest of the main loop
 
 
// Here are those functions
bool CheckCollisions(TBall ball)
{
  // loop through every brick and see if we've hit it
  for (int x = 0; x < BRICK_COLUMNS; x++) {
    for (int y = 0; y < BRICK_ROWS; y++) {
     // Only check against bricks that are active
     if (BrickArray[x][y].ActiveState > 0) {
      if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||
          ball.YLocation  + ball.YSize < BrickArray[x][y].YLocation ||
          ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||
          ball.YLocation  < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {
        // It's collided wit brick, decrment Active state of brick and return true
        BrickArray[x][y].ActiveState--;
        return true;
      }
    }
  }
 
  // Check if we've hit the paddle and 
  // Check if we've hit the wall (I'll leave this up to you)
}
 
// This Draws the bricks
void RenderBricks()
{
  // loop through every brick and draw if active
  for (int x = 0; x < BRICK_COLUMNS; x++) {
    for (int y = 0; y < BRICK_ROWS; y++) {
     if (BrickArray[x][y].ActiveState > 0) {
       // Call you GL draw function, whatever it is.  it would be simple to do in SFML however
       GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,
                           BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);
     }
   }
  }
}
[/code]

 

It gives a general idea of how to encapsulate brick's and the ball into it's own structure, and shows how to handle collisions and rendering.

Edited by BeerNutts
0

Share this post


Link to post
Share on other sites

Start with the structures:

 

 
struct TBrick {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  uint32_t Color;
  uint32_t ActiveState;  // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1
};
 
struct TBall {
  int XPosition;
  int YPosition;
  int XSize;
  int YSize;
  int XSpeed;
  int YSpeed;
};
 
Do you understand why that does, and understand why they are group'd together?  
 
Then, look at the array of bricks, and the loop that initializes the brick array:

#define BRICK_COLUMNS 20 // how many bricks in a column
#define BRICK_ROWS 10 // how many bricks ina  row
#define BRICK_START_X 60 // Where the bricks start on the x-axis
#define BRICK_START_Y 40 // Where the bricks start on the y-axis
#define BRICK_WIDTH 30 // brick width in pixels
#define BRICK_HEIGHT 15 // brickheight in pixels

 

// make this a global array
TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];
 
// Initialize the Brick Array somewhere at the start of your code
for (int x = 0; x < BRICK_COLUMNS; x++) {
  for (int y = 0; y < BRICK_ROWS; y++) {
    BrickArray[x][y].XSize = BRICK_WIDTH;
    BrickArray[x][y].YSize = BRICK_HEIGHT;
    BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;
    BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;
    BrickArray[x][y].Color = 0xFFFFFFFF; // white
    BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick
  }
}
 
Do you know what that is doing?  And slowly go form there.  Just break up " a lot of code" into small pieces of code.  read the comments in the code.  It explains every thing.
 
If you have questions about certain parts of it, ask about it.  it's better than staring at something not understanding what it's doing.
 
I graduated from California state san bernardino

 

Really?  BS in CS?  Do you have a job as a programmer now?
 
Edited by BeerNutts
0

Share this post


Link to post
Share on other sites
yes a bachelors in computer science, I am unemployed at the moment, although I have had some job interviews last year. I am still looking for work right now.
0

Share this post


Link to post
Share on other sites
yeah!! thanks for all the help, I finally solved the collision problem with the ball and bricks.

 

Show us your solution.  I've heard you say that before, but you come back and ask the question again.

0

Share this post


Link to post
Share on other sites
well here it is [code]if( g_bBlock[4].m_bActive_three==true) { if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f) { ystep = -ystep; bricks[2][4]=true; g_bBlock[4].m_bActive_three=false; } }[/code]
0

Share this post


Link to post
Share on other sites

well here it is

if(	g_bBlock[4].m_bActive_three==true)	
	{
	if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
	{
	ystep = -ystep;
	bricks[2][4]=true;
	g_bBlock[4].m_bActive_three=false;	
	}
	}

 

Sigh.

...

Just...Sigh.

I give up.  Good luck to you.

0

Share this post


Link to post
Share on other sites

well here it is

if(	g_bBlock[4].m_bActive_three==true)	
	{
	if(x>=3.0f && x<=5.0f && y>=3.5f && y<=4.0f)
	{
	ystep = -ystep;
	bricks[2][4]=true;
	g_bBlock[4].m_bActive_three=false;	
	}
	}

 

So I want to chime in on this, maybe a modification of your code into something that resembles the naming conventions of BeerNutts (will remove magic numbers/ambiguity) might help you to understand:

 

// Start a loop to loop through all the block
for(unsigned int i = 0; i < MAX_BLOCKS; i++)
{
    // We check to see if blocks are active
    if(g_bBlock[i].isActive)
    {
        // If there is a collision with one of these blocks
        if(ball.x <= g_bBlock[i].width
          || ball.width >= g_bBlock[i].x
          || ball.y <= g_bBlock[i].height
          || ball.height <= g_bBlock[i].y)
        {
            // Then switch the block to inactive
            g_bBlock[i].isActive = false;
            // I assume yStep is the speed the ball is going?
            // If so it is reversed and the ball move backwards.
            yStep *= -1;
        }
    }
}

 

I have commented each step to assist in understanding. See if you understand this and get back to us, tell us what is going on. Try not to code with so many magic numbers, it makes it nearly impossible to understand later, when attempting to convert to use variables.

 

Regards,

 

Stitchs.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0