Sign in to follow this  
phil67rpg

collision question

Recommended Posts

phil67rpg    443
I am making a game where a space ship shoots bullets at bricks at the top of the screen.I have got the bricks to flash on and off quickly when hit by a bullet.I want the brick to turn off permanently.let me know if you need some code.I have done a great deal of research on this topic of collision detection.I just need a hint as to what to do next.I will continue to work on this problem.Thanks for all your help in advance.

Share this post


Link to post
Share on other sites
McGrane    1743
Im not sure i fully understand what your asking, is it just to destroy the block? If you have the collision part working, just change where you have the flashing code, to remove the brick from whatever way you are displaying them, not knowing what language your using or how your implementing this, maybe just add a bool, say ... bool isAlive = true;
if( isAlive ) { RenderBlock() }; .... if( hitTest(Block) ) isAlive = false; Im sure someone can give a more detailed reply if you give more details ;)

Share this post


Link to post
Share on other sites
phil67rpg    443
here is some code
[code]
sprite sprite1;
sprite sprite2;
sprite1.x = 360+move;
sprite1.y = 510-ball_move;
sprite1.width = 5;
sprite1.height = 5;
sprite2.x = 0;
sprite2.y = 50;
sprite2.width = 0;
sprite2.height = 25;
if(Sprite_Collide(&sprite1,&sprite2)==1)
{
render_blank();
}
[/code]

Share this post


Link to post
Share on other sites
codeToad    142
Are you asking how to detect the collision, or how to make a block invisible?

To detect a collision, just check whether the bullet's coordinates are within the blocks coordinates. (i.e. (block.top >= bullet.y && block.bottom <= bullet.y && block.left <= bullet.x && block.right >= bullet.x)). Just do that for each bullet. (The ">=" and "<=" operators might need to be switched depending on what coordinate system you're using).

As for how to make a block invisible, I second McGrane. But you will have to have something a bit more complex since you want the blocks to flash before they disappear. So you will probably need an enum called BlockState that has 3 values - visible, flashing, and invisible. I don't know how you're currently keeping track of your blocks and making them flash, but I would imagine you need some sort of data structure like this:

[CODE]
class Block
{
Vertex vertices[4];
Index indices[2];
Texture* texture;
BlockState state;
int flashTimeLimit;
int elaspsedFlashingTime;
}
[/CODE]

So when you detect a collision, you set that block's state to Flashing. Then on each frame, you update elapsedFlashingTime and check if it is greater than flashTimeLimit. If so, set the block's state to invisible. And of course, when rendering you would check the block's state and make the block flash or disappear accordingly.

Share this post


Link to post
Share on other sites
Endurion    5407
The main problem here seems to be that you have rendering and game logic intertwined.

You should separate those two. In your game loop check for collisions, and if the ball and brick collide, set a flag in the brick object that it's gone or completely remove it from the list (you do have a list of game/brick objects?)

If you choose the flag way (a bool would suffice) check the flag, and if it's set, skip the brick drawing code for that particular brick.

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