Jump to content
  • Advertisement
Sign in to follow this  
mrukki

Collision in SDL?

This topic is 3850 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!