Sign in to follow this  
Hazzy

Platformer in SDL - Collisions

Recommended Posts

Alright I only recently started learning SDL and I'm trying to make a platform game since I thought it would be a good start (after Pong that is...).
Anyway I've done gravity and the player movement so far and working on collisions so that the player has something to work with.

Currently I only have 1 block that the player can interact with and I have no idea how to make the player be able to interact with every block.

Player Collision Code:
[code]
//Check collisions
if (check_collision(box, tblock1.box, 4)) //tblock1 is a single block object that the player can walk on
{
if (Left == true)
{
box.x-=pSpeed;
}
if (Right == true)
{
box.x+=pSpeed;
}
}
[/code]
I have created the class for the blocks the player can walk on but I don't know how to refer to every object of that class so that I can check every collision. All I have done in the code above is made it so it checks for the object tblock1 which is from the Block class.

Basically how do I make the players collision function check for every object created by the Block class? Thanks.

Share this post


Link to post
Share on other sites
This actually doesn't have anything to do with SDL specifically; it's more of a general programming (or maybe math/physics) problem.

As such, you might PM a mod and see if you can get the thread moved to a more appropriate forum (you might be able to get more help that way).

Share this post


Link to post
Share on other sites
You want to use a container to store an arbitrary number of blocks. A simple example for the moment might be std::vector<>, or you might want to look into a data structure that can take advantage of the spatial nature of the data. An arbitrary sized 2 dimensional array would be an example if your platforms are "tile" based.

Share this post


Link to post
Share on other sites
[quote]
You want to use a container to store an arbitrary number of blocks. A simple example for the moment might be std::vector<>, or you might want to look into a data structure that can take advantage of the spatial nature of the data. An arbitrary sized 2 dimensional array would be an example if your platforms are "tile" based.
[/quote]
Thanks, looking into it now.


[quote]
This actually doesn't have anything to do with SDL specifically; it's more of a general programming (or maybe math/physics) problem.

As such, you might PM a mod and see if you can get the thread moved to a more appropriate forum (you might be able to get more help that way).
[/quote]
Since I pretty much have a solution now I won't think this is necessary. In the future I'll take my future questions to the right board, just I was a little confused on which board I should of posted in so I went ahead with the SDL board rather than general programming (Since I'm using SDL).

Thanks for the advice anyways.

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