Sign in to follow this  

Collision in SDL?

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody, I'm tring to make everything solid in my 2d platform game but I got some problems. I've already made collision for the "border" round the level put I can't figure out how to make everything else solid :( Here is my code for the collision:
int charx = 40;
int chary = 270;
int chaar = 0;

bool right_key, left_key, up_key, down_key;
bool end_game = false;
SDL_Event event;
bool hold_down[323] = {false};

while (!end_game)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
hold_down[event.key.keysym.sym] = true;
}
if (event.type == SDL_KEYUP)
{
hold_down[event.key.keysym.sym] = false;
}
}

if (hold_down[SDLK_ESCAPE])
{       
end_game = true;
}

if (left_key = true && hold_down[SDLK_LEFT])
{
if (charx == -20)
{
right_key = true;
left_key = false;
}
else
{
charx -= 2;
chaar = 1;
}
}
if (right_key = true && hold_down[SDLK_RIGHT])
{
if (charx == 570)

{
right_key = false;
left_key = true;
}

else
{
charx += 2;
chaar = 0;
}
}


if (hold_down[SDLK_UP])
{
chary -= 2;
}

if (down_key = true && hold_down[SDLK_DOWN])
{
if (chary == 270)
down_key = false;
else
chary += 2;
}


The variable charx holds the value for the X-axis and chary for the y. Chaar holds a value for two pics, if chaar equals to 0 it apply the right pic of the character and if 1 then the left. This works but not for some other collision. I've heard about SDL_CollideBoundingBox() but I've never understud that :) So if anybody have any good Idéa how I can make simple collisions for my level please post everything you can think of! :) Mrukki [Edited by - mrukki on January 8, 2008 12:19:32 AM]

Share this post


Link to post
Share on other sites
I'd take a look at this tutorial as it deals specificly with what you want fairly well.

These Lazyfoo tutorials might be of help also:
Lesson 17: Collision Detection
Lesson 18: Per Pixel Collision
Lesson 19: Circular Collision Detection

I've never heard of SDL_CollideBoundingBox(), and I don't see it in the SDL documentation. Basicly, what you want to do is check if the player's position is inside a object's box, kinda the opposite of how you check to make sure the player isn't outside of the level.

Share this post


Link to post
Share on other sites

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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