Advertisement

[SDL + OPENGL] Collision Detection Problems

Started by October 11, 2011 10:04 PM
-1 comments, last by walkingbush 13 years, 2 months ago
Hello,

So I have this engine that so far uses sdl for input and opengl for graphics. It was going good until i hit the most important part of a platformer: the collisiion detection. I cannot, for the life of me, get this to work. At the moment the player falls abnormally slowly into the wall, then sinks halfway down and therefore cant move any longer.

You can download the source, with the compiled game (and a level editor lol) here: Download @ Box.net

SDL Info:

Version: v1.2

Language: c++

OS: Win7

For those of you who wish to just look:

Player update code:


void player::update(float delta)
{
xprev = x;
yprev = y;

cam.update(x, y, texture);
position[0]=x;
position[1]=y;
position = cam.updateRect(position);

Uint8* keystates = SDL_GetKeyState(NULL);

if (keystates[SDLK_1])
{
currentGun = pistol;
Arm.switchWeapons(currentGun);
}

if (keystates[SDLK_2])
{
currentGun = main;
Arm.switchWeapons(currentGun);
}

if (keystates[SDLK_w]||keystates[SDLK_UP])
{
if (jump)
{
vel[1] = -640;
jump = false;
}
}

if (keystates[SDLK_a]||keystates[SDLK_LEFT])
{
if (vel[0]>-450)
{
vel[0]-=50;
}
side = 0;
Arm.side = 0;
}

if (keystates[SDLK_d]||keystates[SDLK_RIGHT])
{
if (vel[0]<450)
{
vel[0]+=50;
}
side = 1;
Arm.side = 1;
}

if (!keystates[SDLK_a]&&!keystates[SDLK_LEFT]&&!keystates[SDLK_d]&&!keystates[SDLK_RIGHT])
{
if (vel[0]>0)
{
vel[0]-=50;
}

if (vel[0]<0)
{
vel[0]+=50;
}
}

if (vel[1] < 1600)
{
vel[1] += 64;
}

else
{
vel[1] = 1600;
}

if (!colver)
{
y += vel[1]*(delta/1000);
}

else
{
//y -= vel[1]*(delta/1000);
vel[1] = 0;
jump = true;
}

if (!colhor)
{
x += vel[0]*(delta/1000);
}

else
{
//x -= vel[0]*(delta/1000);
vel[0] = 0;
}

Arm.update(position[0], position[1]);

colhor = false;
colver = false;

}


can be found in intities.cpp.

collision detection part:


void world::updateMap()
{
ExRectangle playerRect(Player.x + Player.vel[0], Player.y + Player.vel[1], Player.texture.Width(), Player.texture.Height());

for (int o = 0; o < objectList.size(); ++o)
{
if(!objectList.at(o).walkThrough)
{
if(check_collision_hor(playerRect, objectList.at(o).rect))
{
Player.colhor = true;
cout<<"Itsa colhorra"<<endl;
}

if(check_collision_ver(playerRect, objectList.at(o).rect))
{
Player.colver = true;
cout<<"Itsa pizza!"<<endl;
}
}
}
}



in world.cpp.

these are the collision detection funtions i have:


int check_collision( ExRectangle Arect, ExRectangle Brect )
{
Vector2 tmpPlayer = Vector2(Arect.X + (Arect.W / 2), Arect.Y + (Arect.H / 2));
Vector2 tmpObject = Vector2(Brect.X + (Brect.W / 2), Brect.Y + (Brect.H / 2));

Vector2 length = tmpPlayer - tmpObject;

if (length[0] < Arect.W / 2 && length[0] > 0)
{
return 1;
}

if (length[0] > (Arect.W / 2) * -1 && length[0] < 0)
{
return 2;
}

if (length[1] < Arect.H / 2 && length[1] > 0)
{
return 3;
}

if (length[1] > (Arect.H / 2) * -1 && length[1] < 0)
{
return 4;
}

return 0;
}

bool check_collision_ver( ExRectangle Arect, ExRectangle Brect )
{
Vector2 tmpPlayer = Vector2(Arect.X + (Arect.W / 2), Arect.Y + (Arect.H / 2));
Vector2 tmpObject = Vector2(Brect.X + (Brect.W / 2), Brect.Y + (Brect.H / 2));

Vector2 length = tmpPlayer - tmpObject;

if (length[1] < Arect.H / 2 && length[1] > 0)
{
return true;
}

if (length[1] > (Arect.H / 2) * -1 && length[1] < 0)
{
return true;
}

return false;
}

bool check_collision_hor( ExRectangle Arect, ExRectangle Brect )
{
Vector2 tmpPlayer = Vector2(Arect.X + (Arect.W / 2), Arect.Y + (Arect.H / 2));
Vector2 tmpObject = Vector2(Brect.X + (Brect.W / 2), Brect.Y + (Brect.H / 2));

Vector2 length = tmpPlayer - tmpObject;

if (length[0] < Arect.W / 2 && length[0] > 0)
{
return true;
}

if (length[0] > (Arect.W / 2) * -1 && length[0] < 0)
{
return true;
}

return false;
}



I appreciate any suggestions or solutions.

This topic is closed to new replies.

Advertisement