collision question

Started by
5 comments, last by phil67rpg 11 years, 11 months ago
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.
Advertisement
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 ;)
here is some 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();
}
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:


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


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.
thanks codetoad let me chew on this for a while. actually I dont want the brick to flash just to turn off permanently.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

thanks endurion ,I am doing research on vectors and lists.I am probably going to use vectors.

This topic is closed to new replies.

Advertisement