Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SDL collision detection!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 stoffe1100   Members   -  Reputation: 105

Like
0Likes
Like

Posted 01 June 2012 - 02:07 PM

Hello, i am trying to make some basic game that are using tiles. Until now it has gone pretty well and forward. But now im just totally stucked. I am trying to write a move funtion to move the player, and after that it checks if there is a collsion and if there is it moves the player back. It works at some parts. But generally it is acting very strange. Could you please take a look at it?

Thanks in advice. I will send all the code i think is necessary and just reply if you need more.

If there is anything else that looks strange just tell me, i want to learn as much as possible.

Here is game.h

#include "base.h"
#include "player.h"
#ifndef GAME_H
#define GAME_H
class game : public base
{
public:
game(void);
~game(void);
private:
bool InitSDL();
void GameLoop();
void HandleEvents();
void UpdateGame();
void Render();
void LoadMap(string filename);
void BlitMap();
bool running;
bool fullscreen;
SDL_Event event;
bool keys[400];
player * Player;
SDL_Surface * screen,*Background,*Blocks;
SDL_Rect BackgroundBox;
vector< vector<int> > map;
};
#endif

Here is game.cpp

#include "game.h"
SDL_Rect base::coord;
game::game(void)
{
if(!this->InitSDL()){exit(1);}
BackgroundBox.x = BackgroundBox.y = coord.x = coord.y = 0;
coord.w = BackgroundBox.w = 800;
coord.h = BackgroundBox.h = 600;
fullscreen = false;
Background = Load_Image("Background.png");
Blocks = Load_Image("Blocks.png");
this->LoadMap("test.TILEMAP");
Player = new player;
Player->CurrentMap(map);
this->GameLoop();
}
void game::GameLoop()
{
Uint32 start, end;
running = true;
while(running)
{
  start = SDL_GetTicks();
  this->HandleEvents();
  this->UpdateGame();
  this->Render();
  end = SDL_GetTicks();
  if(1000/FPS > end - start)
  {
   SDL_Delay(1000/FPS - (end - start));
  }
}
}
void game::HandleEvents()
{
while(SDL_PollEvent(&event))
{
  switch(event.type)
  {
  case SDL_QUIT:
   running = false;
   break;
  case SDL_KEYDOWN:
   keys[event.key.keysym.sym] = true;
   break;
  case SDL_KEYUP:
   keys[event.key.keysym.sym] = false;
   break;
  }
}
}
void game::UpdateGame()
{
Player->SetXvel(0);
Player->SetYvel(0);
if(keys[SDLK_a]){
  coord.x-=vel;
  BackgroundBox.x-=vel;
  Player->SetXvel(-3);
  if(BackgroundBox.x <= 0)
  {
   BackgroundBox.x = Background->w - SCREEN_WIDTH;
  }
}
else if(keys[SDLK_d]){
  coord.x+=vel;
  BackgroundBox.x+=vel;
  Player->SetXvel(3);
  if(BackgroundBox.x >= Background->w - SCREEN_WIDTH)
  {
   BackgroundBox.x = 0;
  }
}
if(keys[SDLK_w]){
  Player->SetYvel(-3);
}else if(keys[SDLK_s]){
  Player->SetYvel(3);
}
else if(keys[SDLK_ESCAPE]){
  running = false;
}else if(keys[SDLK_F5]){
  fullscreen = (!fullscreen);
  if(fullscreen)
  {
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
  }else{
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
  }
}
Player->MovePlayer();

}
void game::Render()
{
SDL_BlitSurface(Background,&BackgroundBox,screen,NULL);
this->BlitMap();
Player->Blit(screen);
SDL_Flip(screen);
}
void game::LoadMap(string filename)
{
ifstream in(filename.c_str());
int Width,Height,current;
in >> Width >> Height;
if(!in.is_open())
{
  cout << "Error: could not load "<< filename << endl;
  return;
}
for(int j = 0;j != Height;j++)
{
  vector<int> TempVec;
  for(int k = 0; k != Width;k++)
  {
   if(in.eof())
   {
	cout << "Error: File end reached to early" << endl;
	return;
   }
   in >> current;
   TempVec.push_back(current);
  }
  map.push_back(TempVec);
}
in.close();
}
void game::BlitMap()
{
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   SDL_Rect BlockRect = {(map[j][k]-1)*TILE_SIZE,0,TILE_SIZE,TILE_SIZE};
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   SDL_BlitSurface(Blocks,&BlockRect,screen,&DestRect);
  }
}
}
game::~game(void)
{
delete Player;
SDL_FreeSurface(Blocks);
SDL_FreeSurface(Background);
SDL_Quit();
}
bool game::InitSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
  cout << "Error: could not initialize SDL" << endl;
  return false;
}else{
  cout << "SDL initialized!" << endl;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
  cout << "Error: could not initialize the screen" << endl;
  return false;
}else{
  cout << "screen initialized!" << endl;
}
if(TTF_Init() == -1)
{
  cout << "Error: could not initialize TTF" << endl;
  return false;
}else{
  cout << "TTF initialized" << endl;
}
for(int i = 0; i != 400; i++)
{
  keys[i] = false;
}
return true;
}

Here is player.h

#include "base.h"
#ifndef PLAYER_H
#define PLAYER_H
class player : public base
{
public:
player(void);
void SetXvel(int vel);
void SetYvel(int vel);
void CurrentMap(vector<vector<int> > Map);

void MovePlayer();
void Blit(SDL_Surface * screen);
~player(void);
private:
vector< vector<int> > map;
SDL_Surface * image;
SDL_Rect box;
int Xvel,Yvel;
bool Ground;

};
#endif

here is player.cpp

#include "player.h"

player::player(void):
Yvel(3),
Xvel(5)
{
image = Load_Image("Player.png");
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,200,0,200));
box.w = image->w;
box.h = image->h;
box.x = SCREEN_WIDTH/2 - box.w/2;
box.y = SCREEN_HEIGHT/2;
Ground = false;
}
void player::SetXvel(int vel)
{
Xvel = vel;
}
void player::SetYvel(int vel)
{
Yvel = vel;
}
void player::MovePlayer()
{
box.x += Xvel;
box.y += Yvel;
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   if(map[j][k] == 0)
   {
	continue;
   }
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   if(CheckCollision(box,DestRect))
   {
    if(box.x+box.w >= DestRect.x)
	 {
	 box.x-=Xvel;
	 }
	 else if(box.x <= DestRect.x + DestRect.w)
	 {
	 box.x-=Xvel;
	 }
	 if(box.y+box.h >= DestRect.y)
	 {
	 box.y-=Yvel;
	 } 
	 else if(box.y <= DestRect.y + DestRect.h)
	 {
	 box.y-=Yvel;
	 }
   }
  }
}
}
void player::CurrentMap(vector<vector<int> > Map)
{
map = Map;
}
void player::Blit(SDL_Surface * screen)
{
SDL_BlitSurface(image,NULL,screen,&box);
}
player::~player(void)
{
SDL_FreeSurface(image);
}

Here is a class which contains the most basic stuffs that all classes need. its called base

Here is base.h

#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
using namespace std;
class base
{
public:
static const int SCREEN_WIDTH = 800;
static const int SCREEN_HEIGHT = 600;
static const int SCREEN_BPP = 32;
static const int FPS = 60;

static const int Gravity = 2;
static const int vel = 2;
static const int TILE_SIZE = 50;
static SDL_Rect coord;
base(void);
SDL_Surface * Load_Image(string filename);
bool CheckCollision(SDL_Rect r1,SDL_Rect r2);
~base(void);
};
#endif

Here is base.cpp

#include "base.h"

base::base(void)
{
}
SDL_Surface * base::Load_Image(string filename)
{
SDL_Surface * IMG = IMG_Load(filename.c_str());
if(IMG != NULL)
{
  SDL_Surface * Opt = SDL_DisplayFormat(IMG);
  SDL_FreeSurface(IMG);
  cout << "Loading image " << filename << " Succeeded!" << endl;
  return Opt;
}else{
  cout << "Error: image " << filename << " could not be loaded" << endl;
  return NULL;
}
}
bool base::CheckCollision(SDL_Rect r1,SDL_Rect r2)
{
if(r1.x > r2.x+r2.w){
  return false;
}
else if(r1.y > r2.y+r2.h){
  return false;
}
else if(r1.x+r1.w < r2.x){
  return false;
}
else if(r1.y+r1.h < r2.y){
  return false;
}else{
  return true;
}
}
base::~base(void)
{
}

Edited by stoffe1100, 02 June 2012 - 08:37 AM.


Sponsor:

#2 CableGuy   Members   -  Reputation: 925

Like
0Likes
Like

Posted 02 June 2012 - 07:35 AM

What do you mean by strange? What are the exact problems you are facing?

Tile collision is pretty easy. Usually you have a 2d array representing the map, checking for collison simply invovlves getting the player position in the tile map
and indexing the array to check if this tile is passable.

Your code looks a bit overly complicated. Why do you have a for loop when checking for collisions?

Edited by CableGuy, 02 June 2012 - 07:39 AM.


#3 stoffe1100   Members   -  Reputation: 105

Like
0Likes
Like

Posted 02 June 2012 - 08:21 AM

What do you mean by strange? What are the exact problems you are facing?

Tile collision is pretty easy. Usually you have a 2d array representing the map, checking for collison simply invovlves getting the player position in the tile map
and indexing the array to check if this tile is passable.

Your code looks a bit overly complicated. Why do you have a for loop when checking for collisions?


Actually my map is stored in a 2 dimensional vector called map.

For the moment it works pretty well when colliding Up, Down and left. But i only get slowed when i collide from the right side. And if i push towards a wall i get pushed to the top of the wall without pressing up key. And sometimes i just fall under the whole map when colliding.

What i am really looking for is like a whole new structure for how my collsion should look like. I dont understand why it is over complicated? please tell me and i will try to explain.

Also when im colliding my ball is bouncing very quickly. Which it shouldnt because everything should be calculated and set back within one frame!

Ive been playing around with some stuffs and i updated the player::move() function. When it is like this the collision in the Y axis is working great. but the X axis collisions are super weird.

Please help me, i have no clue on how to continue. I cant stop thinking of those problems. its really anoying

Edited by stoffe1100, 02 June 2012 - 08:39 AM.


#4 CableGuy   Members   -  Reputation: 925

Like
0Likes
Like

Posted 02 June 2012 - 12:20 PM

I didn't quite get exactly what type of game you are creating and what exactly you are trying to achieve...
If you can answer those question I might be able to help you rethink your problem

#5 stoffe1100   Members   -  Reputation: 105

Like
0Likes
Like

Posted 02 June 2012 - 03:54 PM

I didn't quite get exactly what type of game you are creating and what exactly you are trying to achieve...
If you can answer those question I might be able to help you rethink your problem


Hmm, I am actually just doing this to train myself on sdl. But if i say mario? something like that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS