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.
#1 GDNet+ - Reputation: 522
Posted 09 January 2013 - 10:53 PM
#2 Members - Reputation: 2113
Posted 10 January 2013 - 12:03 AM
#3 Crossbones+ - Reputation: 1460
Posted 10 January 2013 - 01:28 AM
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, 10 January 2013 - 02:14 PM.
#4 Members - Reputation: 123
Posted 10 January 2013 - 03:51 AM
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, 10 January 2013 - 03:52 AM.
#5 Members - Reputation: 1626
Posted 10 January 2013 - 12:43 PM
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).
Digivance Game Studios Founder:
Dan Mayor - Dan@Digivance.com
www.Digivance.com
#6 Members - Reputation: 1863
Posted 10 January 2013 - 01:04 PM
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.
#7 Members - Reputation: 1566
Posted 10 January 2013 - 01:21 PM
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)
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#9 Members - Reputation: 1863
Posted 10 January 2013 - 02:39 PM
#11 Members - Reputation: 1566
Posted 10 January 2013 - 03:45 PM
I graduated from college with a b.s. in c.s. in 2005.
Which college?
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#12 Members - Reputation: 1566
Posted 10 January 2013 - 03:49 PM
Phil, what did you not understand about this code I posted?
</p><div>struct TBrick {</div>
<div> int XPosition;</div>
<div> int YPosition;</div>
<div> int XSize;</div>
<div> int YSize;</div>
<div> uint32_t Color;</div>
<div> uint32_t ActiveState; // 0 is deactive; If it takes multiple hits to kill a brick, it can start greater than 1</div>
<div>};</div>
<div> </div>
<div>struct TBall {</div>
<div> int XPosition;</div>
<div> int YPosition;</div>
<div> int XSize;</div>
<div> int YSize;</div>
<div> int XSpeed;</div>
<div> int YSpeed;</div>
<div>};</div>
<div> </div>
<div>#define BRICK_COLUMNS 20 // how many bricks in a column</div>
<div>#define BRICK_ROWS 10 // how many bricks ina row</div>
<div>#define BRICK_START_X 60 // Where the bricks start on the x-axis</div>
<div>#define BRICK_START_Y 40 // Where the bricks start on the y-axis</div>
<div>#define BRICK_WIDTH 30 // brick width in pixels</div>
<div>#define BRICK_HEIGHT 15 // brickheight in pixels</div>
<div> </div>
<div>// make this a global array</div>
<div>TBrick BrickArray[BRICK_COLUMNS][BRICK_ROWS];</div>
<div> </div>
<div>// Initialize the Brick Array somewhere at the start of your code</div>
<div>for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div> for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div> BrickArray[x][y].XSize = BRICK_WIDTH;</div>
<div> BrickArray[x][y].YSize = BRICK_HEIGHT;</div>
<div> BrickArray[x][y].XLocation = BRICK_START_X + x*BRICK_WIDTH;</div>
<div> BrickArray[x][y].YLocation = BRICK_START_Y + y*BRICK_HEIGHT;</div>
<div> BrickArray[x][y].Color = 0xFFFFFFFF; // white</div>
<div> BrickArray[x][y].ActiveState = 1; // 1 hit to turn off brick</div>
<div> }</div>
<div>}</div>
<div> </div>
<div>// in main loop do this:</div>
<div>// move Ball in X direction, then check collision</div>
<div>Ball.XLocation += Ball.XSpeed;</div>
<div>if (CheckCollision(Ball)) {</div>
<div> // The ball hit something, move to original location and negate X speed</div>
<div> Ball.XLocation -= Ball.XSpeed;</div>
<div> Ball.XSpeed = -Ball.XSpeed;</div>
<div>}</div>
<div> </div>
<div>// Do same for Y movement</div>
<div>Ball.YLocation += Ball.YSpeed;</div>
<div>if (CheckCollision(Ball)) {</div>
<div> // The ball hit something, move to original location and negate Y speed</div>
<div> Ball.YLocation -= Ball.YSpeed;</div>
<div> Ball.YSpeed = -Ball.YSpeed;</div>
<div>}</div>
<div> </div>
<div>RenderBricks();</div>
<div> </div>
<div>... // the rest of the main loop</div>
<div> </div>
<div> </div>
<div>// Here are those functions</div>
<div>bool CheckCollisions(TBall ball)</div>
<div>{</div>
<div> // loop through every brick and see if we've hit it</div>
<div> for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div> for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div> // Only check against bricks that are active</div>
<div> if (BrickArray[x][y].ActiveState > 0) {</div>
<div> if (ball.XLocation + ball.XSize < BrickArray[x][y].XLocation ||</div>
<div> ball.YLocation + ball.YSize < BrickArray[x][y].YLocation ||</div>
<div> ball.XLocation > BrickArray[x][y].XLocation + BrickArray[x][y].XSize ||</div>
<div> ball.YLocation < BrickArray[x][y].YLocation + BrickArray[x][y].YSize) {</div>
<div> // It's collided wit brick, decrment Active state of brick and return true</div>
<div> BrickArray[x][y].ActiveState--;</div>
<div> return true;</div>
<div> }</div>
<div> }</div>
<div> }</div>
<div> </div>
<div> // Check if we've hit the paddle and </div>
<div> // Check if we've hit the wall (I'll leave this up to you)</div>
<div>}</div>
<div> </div>
<div>// This Draws the bricks</div>
<div>void RenderBricks()</div>
<div>{</div>
<div> // loop through every brick and draw if active</div>
<div> for (int x = 0; x < BRICK_COLUMNS; x++) {</div>
<div> for (int y = 0; y < BRICK_ROWS; y++) {</div>
<div> if (BrickArray[x][y].ActiveState > 0) {</div>
<div> // Call you GL draw function, whatever it is. it would be simple to do in SFML however</div>
<div> GlDrawRect(BrickArray[x][y].XLocation, BrickArray[x][y].YLocation,</div>
<div> BrickArray[x][y].XSize, BrickArray[x][y].YSize, BrickArray[x][y].Color);</div>
<div> }</div>
<div> }</div>
<div> }</div>
<div>}</div>
<div>
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, 10 January 2013 - 04:32 PM.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#15 Members - Reputation: 1566
Posted 10 January 2013 - 04:29 PM
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;
};
#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
}
}
I graduated from California state san bernardino
Edited by BeerNutts, 10 January 2013 - 04:31 PM.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#18 Members - Reputation: 1566
Posted 11 January 2013 - 01:54 PM
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.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#20 Members - Reputation: 1566
Posted 11 January 2013 - 04:13 PM
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.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)






