•      Sign In
• Create Account

# Sunsharior

Member Since 14 Dec 2013
Offline Last Active Oct 06 2016 06:42 PM

### In Topic: What would you be willing to trade to get your ideal job in the gaming industry?

03 July 2015 - 05:24 AM

I would sacrifice a lot more things (i've already sacrificed so much), but i wouldn't sacrifice my happiness.

### In Topic: 2D Polygon Collision Detection

02 July 2015 - 06:46 AM

The question is vague, but i'll try to answer anyway.

Not too long ago, i've been at the same place where you are at the moment. The biggest problem i had was trying to assimilate everything at once.

Start with small bites.

1. First, design a simple Polygon class. Just try to make it work, and focus on that.
2. Once it's working, try to implement your own  SAT algorithm that work with your own polygon class. You'll need to understand how SAT work.
3. Once that is working, modify your sat algorythm to include the MTV.

Now i realize this might not help you, so here is my own SAT function you can read and try to understand (or even use if you want, but a mention would be nice, just don't claim it as your own).

The code is well documented, should you have more question, i'll be happy to answer.

The Sat Function

Spoiler

The Collision structure, including the MTV.

Spoiler

The Polygon struct.

Spoiler

The Segment struct

Spoiler

The Vector struct

Spoiler

Edit: I highly recommend this book. It explain everything you'll ever want to know about collisions.

### In Topic: Creating menus using Codeblocks (C++)

02 July 2015 - 06:18 AM

Also, each Menus can be a state inside the engine you are using, making a simple interface with Update, Draw and Clear or what ever fit your need. Consider using one state per menu, unless your design do not need it.

Just plug the current state inside the main game loop and change it when you need it.

We'll be happy to answers more question.

### In Topic: Creating rectangles with contiguous filled grid cell

25 June 2015 - 09:00 PM

I finally figured out the algorithm i needed. Here is my solution, in case some one need it (in C++).

Please note that my solution will scan along the X axis all the way to the end before going to the next row. This is exactly how my game need it, but i feel the need to mention it.

The algorithm is not optimized. In my case, it did not need to be optimized.

```//////////////////////////////////////////////////////////////////////////
// Calculate the rectangles that can exist in a grid.
std::vector<SDL_Rect> MapManager::CalculateVoidZones()
{
std::vector<SDL_Point> corners;
std::vector<SDL_Point> sizes;

// Retrieve the maximum col and row. note that the col and row are already shrinked by 1.
SDL_Point max = {mInfo->GetCol(), mInfo->GetRow()};

// scan a col. Start from top left.
for(int row = 0; row <= max.y; row++)
{
// remember the current width for consecutive void state.
const short INVALID = -1;
SDL_Point lastcorner = {INVALID, INVALID}; // invalid corner.
int width = 0;

// scan a col. go right.
for(int col = 0; col <= max.x; col++)
{
bool lastcol = (col == mInfo->GetCol() && width > 0);
bool addnewcorner = false, nonvoid = true;

// if the cell is a void.
if(GetTile(row, col)->IsState(STATE_TILE_VOID))
{
nonvoid = false;

if(lastcorner.x == INVALID && lastcorner.y == INVALID)
{
lastcorner.x = col;
lastcorner.y = row;
}

bool addwidth = lastcorner.x == (col - width);

if (addwidth)
width++;

if(!addwidth || lastcol)
addnewcorner = true;
}

if(addnewcorner || (nonvoid && width))
{
bool fit = false; // do the rectangle fit into a previous one?

// loop through all existing corners
for(unsigned int i = 0; i < corners.size(); i++)
{
if(corners[i].x == lastcorner.x &&
corners[i].y == (lastcorner.y - sizes[i].y) &&
width == sizes[i].x)
{
// increase the height
sizes[i].y++;
fit = true;
}
}

if(!fit)
{
corners.push_back(lastcorner);

SDL_Point size = SDL_Point();
size.x = width;
size.y = 1;
sizes.push_back(size);
}

// reset
lastcorner.x = INVALID;
lastcorner.y = INVALID;
width = 0;
}
}
}

// finally, create the rects
std::vector<SDL_Rect> voidzones;
if(corners.size() && corners.size() == sizes.size())
{
for(int i = corners.size(); i--;)
{
SDL_Point corner = corners[i];
SDL_Point size = sizes[i];

// mTw and mTh is the width and height of a tile, respectively.
SDL_Rect rect = {corner.x * mTw,
corner.y * mTh,
size.x * mTw,
size.y * mTh};
rects.push_back(rect);
}
}

return voidzones;
}
```

Have a good day.

### In Topic: Creating rectangles with contiguous filled grid cell

25 June 2015 - 12:47 PM

It's called shape vectorization or bitmap vectorization. You'll also have to handle the case where two rectangles touch.

I've googled your answer, but what i found didn't seems related to my problem. I'll google more for the moment.

PARTNERS