Sign in to follow this  
  • entries
    55
  • comments
    55
  • views
    24447

First Day

Sign in to follow this  

91 views

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
Sign in to follow this  


2 Comments


Recommended Comments

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.

Share this comment


Link to comment
Thanks for the info MrCpaw. I'll be sure to read it again when I get to collision detection.

Share this comment


Link to comment

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