Ok. So I'm trying to make images automatically push each other away upon colliding.
If you have any knowledge about collision, please take a look at my code here.
and give me some advice on what I could do to fix it/make it better; I'd greatly appreciate it!
Code: Select all
#include "SDL/SDL.h"
#include "imageManager.h"
#include "image.h"
ImageManager::ImageManager()
{
i = 1;
}
void ImageManager::WriteImage(SDL_Surface *dstSurf)
{
for( int i = 0; i < images.size(); i++)
SDL_BlitSurface( images[i]->imageSurf , &images[i]->imageRect, dstSurf,
&images[i]->imageDst );
}
void ImageManager::NewImage( const char *file, Sint16 x, Sint16 y, Uint16 w, Uint16 h )
{
images.push_back( new Image(file, x, y, w, h) );
}
void ImageManager::GetInput( SDL_Event *event)
{
if( event->type == SDL_KEYDOWN )
{
key = SDL_GetKeyState( NULL );
CheckUp();
}
}
bool ImageManager::IsCollision(SDL_Rect *r1, SDL_Rect *r2)
{
if (r1->x < r2->x + r2->w &&
r1->x + r1->w > r2->x &&
r1->y < r2->y + r2->h &&
r1->y + r1->h > r2->y)
{
return true;
}
return false;
}
void ImageManager::CheckUp()
{
if( key[SDLK_DOWN] )
{
if (images[0]->imageDst.y < 480 - images[0]->imageDst.h)
if ( images[i]->imageDst.y < 480 - images[i]->imageDst.h)
{
images[0]->imageDst.y++;
for( int j = 0; j < images.size(); j++)
{
for( int k = 0; k < images.size(); k++)
{
if (k == j)
continue;
else if ( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
{
while( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
images[k]->imageDst.y++;
}
}
}
}
else if ( images[0]->imageDst.y + images[0]->imageDst.h < images[i]->imageDst.y
|| images[0]->imageDst.x > images[i]->imageDst.x + images[i]->imageDst.w
|| images[0]->imageDst.y > images[i]->imageDst.y + images[i]->imageDst.h
|| images[0]->imageDst.x + images[0]->imageDst.w < images[i]->imageDst.x)
images[0]->imageDst.y++;
}
else if( key[SDLK_UP] )
{
if (images[0]->imageDst.y > 0)
if ( images[i]->imageDst.y > 0)
{
images[0]->imageDst.y--;
for( int j = 0; j < images.size(); j++)
{
for( int k = 0; k < images.size(); k++)
{
if (k == j)
continue;
else if ( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
{
while( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
images[k]->imageDst.y--;
}
}
}
}
else if ( images[0]->imageDst.y + images[0]->imageDst.h < images[i]->imageDst.y
|| images[0]->imageDst.x > images[i]->imageDst.x + images[i]->imageDst.w
|| images[0]->imageDst.y > images[i]->imageDst.y + images[i]->imageDst.h
|| images[0]->imageDst.x + images[0]->imageDst.w < images[i]->imageDst.x)
images[0]->imageDst.y--;
}
else if( key[SDLK_LEFT] )
{
if (images[0]->imageDst.x > 0)
if ( images[i]->imageDst.x > 0)
{
images[0]->imageDst.x--;
for( int j = 0; j < images.size(); j++)
{
for( int k = 0; k < images.size(); k++)
{
if (k == j)
continue;
else if ( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
{
while( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
images[k]->imageDst.x--;
}
}
}
}
else if ( images[0]->imageDst.y + images[0]->imageDst.h < images[i]->imageDst.y
|| images[0]->imageDst.x > images[i]->imageDst.x + images[i]->imageDst.w
|| images[0]->imageDst.y > images[i]->imageDst.y + images[i]->imageDst.h
|| images[0]->imageDst.x + images[0]->imageDst.w < images[i]->imageDst.x)
images[0]->imageDst.x--;
}
else if( key[SDLK_RIGHT] )
{
if (images[0]->imageDst.x < 640 - images[0]->imageDst.w )
if ( images[i]->imageDst.x < 640 - images[i]->imageDst.w)
{
images[0]->imageDst.x++;
for( int j = 0; j < images.size(); j++)
{
for( int k = 0; k < images.size(); k++)
{
if (k == j)
continue;
else if ( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
{
while( IsCollision(&images[j]->imageDst, &images[k]->imageDst) )
images[k]->imageDst.x++;
}
}
}
}
else if ( images[0]->imageDst.y + images[0]->imageDst.h < images[i]->imageDst.y
|| images[0]->imageDst.x > images[i]->imageDst.x + images[i]->imageDst.w
|| images[0]->imageDst.y > images[i]->imageDst.y + images[i]->imageDst.h
|| images[0]->imageDst.x + images[0]->imageDst.w < images[i]->imageDst.x)
images[0]->imageDst.x++;
}
}