Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - neobrain

Pages: [1]
1
Graphics / SFML collision detection
« on: November 22, 2014, 05:06:58 am »
I am trying to develop maps like the ones in this game (1)

Well, my current algorithm could be hacky but I have a problem detecting  collisions.

I have the following code but apparently, I still have collisions like (2). I am not sure what is wrong with the code but it is surely not giving the expected results.

/*
 * Set the number of leafs dictated by the level, very simple implementation
 * that does not come up with as cute a maps Set the first leaf at the middle
 * of the map, for an even 'i' generate a random  x-coordinate while for an odd
 * 'i' generate a y-coordinate, check for overlap/collision, hack the coordinates
 * further if one exists
 */

leafenvironment::leafenvironment(int level) {
  srand(time(nullptr));
  for (size_t i = 0; i < level; i++) {
    lily *current = new lily();
    leafs.push_back(current);
  }
  // first lily at centre of window and redundant stuff
  for (size_t i = 0; i < level; i++) {
    if (i == 0) {
      pos *current = new pos;
      current->x = WIDTH / 2;
      current->y = HEIGHT / 2;
      position.push_back(current);
      assert(leafs[i]);
      leafs[i]->SetPosition(position[i]->x, position[i]->y);
      leafs[i]->GetSprite().setScale(0.5f, 0.5f);
      continue;
    }
 
    float newpos = static_cast<float>(rand() % 10);
    if (i % 2) {
      pos *current = new pos;
      current->x = newpos * 30;
      current->y = position[i - 1]->y;
      position.push_back(current);
      assert(leafs[i]);
      leafs[i]->SetPosition(position[i]->x, position[i]->y);
      leafs[i]->GetSprite().setScale(0.5f, 0.5f);
      for (size_t j = i - 1; j <= 0; j--) {
        while (leafs[i]->GetSprite().getGlobalBounds().intersects(leafs[j]->GetSprite().getGlobalBounds())) {
          leafs[i]->SetPosition(position[i]->x * 100, position[i]->y);
        }
      }
    } else {
      pos *current = new pos;
      current->x = position[i - 1]->x;
      current->y = newpos * 30;
      position.push_back(current);
      assert(leafs[i]);
      leafs[i]->SetPosition(position[i]->x, position[i]->y);
      leafs[i]->GetSprite().setScale(0.5f, 0.5f);
      for (size_t j = i - 1; j <= 0; j--) {
        while (leafs[i]->GetSprite().getGlobalBounds().intersects(leafs[j]->GetSprite().getGlobalBounds())) {
          leafs[i]->SetPosition(position[i]->x, position[i]->y * 100);
        }
      }
    }
  }
}
 

I think of developing an algorithm where I  use an a 2-d array to represent the window grid some thing like


#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

char grid[15][16] = {
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
    {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
};

int main()
{
    srand (time(NULL));

    int x = rand() % 15, y = rand() % 15;

    grid[x][y] = '@';

    for (int lily = 0; lily < 10; ++lily)
    {
        std::vector<std::pair<int,int>> avail;
        for (int ix = x+1; ix < 15; ++ix)
        {
            if (grid[ix][y] == '@')
            {
                break;
            }
            avail.push_back(std::make_pair(ix,y));
        }
        for (int ix = x-1; ix >= 0; --ix)
        {
            if (grid[ix][y] == '@')
            {
                break;
            }
            avail.push_back(std::make_pair(ix,y));
        }

        for (int iy = y+1; iy < 15; ++iy)
        {
            if (grid[x][iy] == '@')
            {
                break;
            }
            avail.push_back(std::make_pair(x,iy));
        }
        for (int iy = y-1; iy >= 0; --iy)
        {
            if (grid[x][iy] == '@')
            {
                break;
            }
            avail.push_back(std::make_pair(x,iy));
        }
        std::pair<int,int> xy = avail[rand() % avail.size()];
        x = xy.first;
        y = xy.second;
        grid[x][y] = '@';
    }

    for (int i = 0; i < 15; ++i)
    {
        std::cout << grid[i] << std::endl;
    }
}

but then I am stuck at how to map the filled array onto the window grid. Could someone please point me in the right direction?

1)http://www.psdevwiki.com/ps3/Scogger_HD
2)http://imgur.com/a/pBEyh

Pages: [1]
anything