First Day

Published June 13, 2009
Advertisement
A quick update today. I managed to get SDL up and running and now I can put images on the screen. I also implemented a game state system like the one in LazyFoo's article on the subject (I had done this previously on my earlier projects). So at the moment I just have a title screen with no options on it but the ability to easily add new states.

Tomorrow, I hope to get the paddles and ball moving but probably won't be able to get any sort of collision detection done.

-AEdmonds
Previous Entry Square One
Next Entry Our Velocity
0 likes 2 comments

Comments

MrCpaw
Collision detection isn't that hard with 2D. You can use bounding box collision for your objects. However you will need some addition checking to insure movement doesn't occur if collision is detected upon your next movement.

Setting up bounding box collision is done like so.

You need the X and Y of your object.

int Bounding_Box_Top = Y;
int Bounding_Box_Bottom = (Y or Bounding_Box_Top, doesn't matter) + Image_Height;
int Bounding_Box_Left = X;
int Bounding_Box_Right = (X or Bounding_Box_Left, doesn't matter) + Image_Width;

Be sure to always check your current Bounding Box in your update code after movement and before collision check.

The above can be understood by drawing two squares on paper. The top left would be your Y value. To get the bottom you need to get your Y value plus your image height, which yields the location of your bottom area. Y = top, Y + 32 = 32 from the top, meaning bottom. Same applies with left and right.

Now to check for collision do something like this:


Bob_X_Old = Bob_X;
Bob_Y_Old = Bob_Y;

Bob_Y += 3;

Bob_Bounding_Box_Top = Bob_Y;
Bob_Bounding_Box_Bottom = Bob_Bounding_Box_Top + 29;
Bob_Bounding_Box_Left = Bob_X;
Bob_Bounding_Box_Right = Bob_Bounding_Box_Left + 29;

if (Bob_Bounding_Box_Bottom > Collision_1.Bounding_Box_Top &&
    Bob_Bounding_Box_Top < Collision_1.Bounding_Box_Bottom &&
    Bob_Bounding_Box_Left < Collision_1.Bounding_Box_Right &&
    Bob_Bounding_Box_Right > Collision_1.Bounding_Box_Left)
{
    Bob_Y = Bob_Y_Old;
}



What will happen here is that Bob_Y_Old gets your current position, then you move down with Bob by 3. It checks if you have collision or not, if you do your position becomes your old position.

If you need any help you can PM me at any time.
June 13, 2009 08:05 PM
AEdmonds
Thanks for the info MrCpaw. I'll be sure to read it again when I get to collision detection.
June 14, 2009 03:51 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement