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.


Messages - noct

Pages: [1] 2
1
General / Re: [SFML 2.0]Problem with A* algorithm
« on: August 18, 2016, 07:33:32 pm »
Ok, got it to be working properly. Well, kinda.
bool aStar::findPath(sf::Vector2i start, sf::Vector2i end, int tmap[10][10], int collisionMap[10][10])
{
    cout << "START" << endl;
    cout <<end.x<<" " << end.y <<endl;
StartNode.position = start;
EndNode.position = end;

OpenSet.push_back(StartNode);


while (OpenSet[minfCost(OpenSet)] != EndNode )
{
Node CurrentNode;
CurrentNode = OpenSet[minfCost(OpenSet)];


OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);
tmap[CurrentNode.position.y][CurrentNode.position.x] = 2;

if(CurrentNode == EndNode) {break; }


// neighbors
for (int y=-1; y<=1; y++)
{
    for (int x=-1; x<=1; x++)
    {
     int checkX=CurrentNode.position.x+x, checkY=CurrentNode.position.y+y;

        if(x==0 && y==0) { continue; }

        if(checkX < 0 || checkX > 9 || checkY < 0 || checkY > 9) {continue; }
        if(collisionMap[checkY][checkX] == 0)
        {
         Node newNode;
         newNode.position = sf::Vector2i(checkX, checkY);
         newNode.parentPosition = CurrentNode.position;

            if((newNode.position.x==CurrentNode.position.x && newNode.position.y!=CurrentNode.position.y) ||
               (newNode.position.y==CurrentNode.position.y && newNode.position.x!=CurrentNode.position.x))
                  { newNode.gCost = 10 + CurrentNode.gCost; }
            else
                  { newNode.gCost = 14 + CurrentNode.gCost; }

         newNode.hCost = heuristic(newNode.position, EndNode.position);
         newNode.fCost = newNode.gCost + newNode.hCost;
         OpenSet.push_back(newNode);
         tmap[checkY][checkX]=1;

        OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
         ClosedSet.push_back(CurrentNode);
        }

    }
}

cout << CurrentNode.parentPosition.x << " " << CurrentNode.parentPosition.y<< endl;

}
/*
int tmpx=OpenSet[minfCost(OpenSet)].position.x, tmpy=OpenSet[minfCost(OpenSet)].position.y;
tmap[tmpy][tmpx] = 5;
cout <<"BUILD PATH" << endl;
*/

cout << "EXIT FINDING" << endl;

OpenSet.clear();
ClosedSet.clear();
return true;
}

This's how it's working so far. Green rects are "open", red "closed", black are impassable. It's not slow, I've just set a 1s timer between findPath() function calls.

Now I know the algorithm ends when it should be, however I have a problem with filling PathSet. Algorithm stops when "currentNode" is equal to "EndNode" (i've got == an != operator overloads with object.position comparision)

I don't think so, but should I disable checking from ClosedSet? How am I able to get my ParentNode as Node class object? AFAIK class can't contain it's own objects.

2
General / Re: [SFML 2.0]Problem with A* algorithm
« on: August 17, 2016, 12:23:20 pm »
Do you mean this?
  if(checkX < 0 || checkX > 10 || checkY < 0 || checkY > 10) {continue; }
It's for sure not the line causing error here (in most and in tested cases), corrected it tho.

I think I need A* for later management of multiple entities - it's the easiest and optimal algorithm which can be expanded with more movement costs.

I'm wondering if storing parentPosition is enough - do I need a class (Node'ish) object instead?

Btw, even experts happen to make stupid/be tired mistakes, this doesn't indicate anything.


#Changed Manhattan to 8-way distance measuring

3
General / [SFML 2.0]Problem with A* algorithm
« on: August 17, 2016, 10:19:31 am »
Hello there. Im working on an implementation of A* algorithm for my SFML game-project. I've recently got some problems with passing 2d arrays to a function (I think).

I got segmentation fault error when receving array[][sizex] (I have an idea why) and many other errors with array[sizey][sizex].
Lets take a look on the code:

aStar.hpp:
//(...)
class Node
{
public:
Node();
sf::Vector2i position;
sf::Vector2i parentPosition;

int gCost;
int hCost;
int fCost;

bool operator == (const Node & nod);


};

class aStar
{
public:

aStar();
int manhattan(sf::Vector2i start, sf::Vector2i end);

Node StartNode;
Node EndNode;

  vector<Node> OpenSet;
  vector<Node> ClosedSet;
  vector<Node> NeighborSet;
  vector<Node> PathSet;

void findPath(sf::Vector2i start, sf::Vector2i end, int tmap[10][10], int collisionMap[10][10]);
void buildPath();
int minfCost(vector<Node> vec);
void findNeighbors(Node CurrentNode, int tmap[10][10], int collisionMap[10][10]);

};
//(...)

aStar.cpp:
//(...)
aStar::aStar()
{
StartNode.parentPosition = sf::Vector2i(0,0);

}

Node::Node()
{
gCost = 0;
hCost = 0;
}


//int Node::fCost() { return gCost+hCost; }

void aStar::findNeighbors(Node CurrentNode, int tmap[10][10], int collisionMap[10][10])
{

   for (int y=-1; y<=1; y++)
{
    for (int x=-1; x<=1; x++)
    {
        int checkX=CurrentNode.position.x+x, checkY=CurrentNode.position.y+y;

        if(x==0 && y==0) {continue; }

        if(checkX < 0 || checkX > 10 || checkY < 0 || checkY > 10) {continue; }
        if(collisionMap[checkY][checkX] == 0)
        {
            Node newNode;
            newNode.position = sf::Vector2i(checkX, checkY);
            newNode.parentPosition = CurrentNode.position;

            if((newNode.position.x==CurrentNode.position.x && newNode.position.y!=CurrentNode.position.y) ||
               (newNode.position.y==CurrentNode.position.y && newNode.position.x!=CurrentNode.position.x))
                    {
                     newNode.gCost = 10 + CurrentNode.gCost;
                    }
            else
                    {
                     newNode.gCost = 14 + CurrentNode.gCost;
                    }

           // OpenSet.push_back(newNode);

           NeighborSet.push_back(newNode);
            tmap[checkY][checkX] = 1;
        }


    }


}
}

bool Node::operator == (const Node & nod)
{ return (this->position == nod.position); }

int aStar::manhattan(sf::Vector2i start, sf::Vector2i end)
{
    int dist;
    dist = abs(start.x - end.x) + abs(start.y - end.y);
    return (dist*10);
}


int aStar::minfCost(vector<Node> vec)
{ int min = vec[0].fCost;
 unsigned int i=0;
    for(; i<vec.size(); i++)
    {
        if(vec[i].fCost < min) {min =vec[i].fCost; }
    }
    return i;
}


void aStar::findPath(sf::Vector2i start, sf::Vector2i end, int tmap[10][10], int collisionMap[10][10])
{

StartNode.position = start;
StartNode.gCost = 0;
StartNode.fCost = StartNode.gCost + manhattan(start, end);
EndNode.position = end;

Node CurrentNode;

OpenSet.push_back(StartNode);

while (OpenSet.size() > 0)
{
    CurrentNode = OpenSet[minfCost(OpenSet)];
    if(CurrentNode == EndNode) { buildPath();   return ; }

OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);

findNeighbors(CurrentNode, tmap, collisionMap);
for ( unsigned int i=0; i<NeighborSet.size(); i++)
{
    if(find(ClosedSet.begin(), ClosedSet.end(), NeighborSet[i]) != ClosedSet.end() )
    {
        continue;
    }
      int tent = CurrentNode.gCost + manhattan(CurrentNode.position, NeighborSet[i].position);

    if(! (find(OpenSet.begin(), OpenSet.end(), NeighborSet[i]) != OpenSet.end() ))
    {// not found
      OpenSet.push_back(NeighborSet[i]);
    }
    else if (tent >= NeighborSet[i].gCost)
    {
        continue;
    }

    NeighborSet[i].fCost = NeighborSet[i].gCost = manhattan(NeighborSet[i].position, EndNode.position);
}

}

}

void aStar::buildPath()
{
    PathSet.push_back(EndNode);
    Node newNode = EndNode;
    while (newNode.parentPosition != sf::Vector2i(-1,-1)) // only possible if newNode == StartNode
    {
        newNode.position = newNode.parentPosition;
        PathSet.push_back(newNode);
    }


}
//(...)
I know it's a big piece of code, sorry for that, however I am not able to specify what's going wrong here.
I know this code Isn't the highest quality, I want to optimise it after I'll have it working ;)
I use std namespace by default.

I got an error wherever I use tmap[10][10] (ctrl+f it)

tmap is a 10x10 int array filled with 0,1,2,3 ("block" numbers)
collisionMap is a 10x10 int array filled with 0 if a "block" is walkable, 1 if isn't and 2 if a "block" is start/end of desired path.

If you think the only option is to rewrite the entire thing, just tell me. I'm looking for at least some tips if fixing this is not possible/worth.



Debugger output: http://pastebin.com/uRFxyrXa

I've been using those pseudocodes/tutorials: (+stack ofc)
http://www.growingwiththeweb.com/2012/06/a-pathfinding-algorithm.html
https://en.wikipedia.org/wiki/A*_search_algorithm
http://www.policyalmanac.org/games/aStarTutorial.htm

4
If you'd not like to display your entire texture, just a piece, you'd have to use
setTextureRect(sf::IntRect() )
to cut desired part and then display sprite as usual.

Else if you thought about something like two pics combined into one, I think SFML won't help you there. Maybe creating all single "bloodied bat", "scratched bat" etc textures will do?

I've got similar inventory system btw ;)

5
General / Re: SFML Texture in diffrent class not drawing
« on: December 12, 2015, 09:12:22 pm »
I think you have to pass a reference to Texture instead of pointer, or it won't be working correct.

6
General / Re: Wont run on windows 10
« on: December 08, 2015, 02:53:18 pm »
Now it displays an additional window with some gfx/sfx options. I wish I had a c# System.Windows.Forms for that.
Thanks for help @eXpl0it3r

7
General / Re: Wont run on windows 10
« on: December 08, 2015, 12:07:59 am »
<insert some epic facepalm gifs>
That was it.
How should I prevent bugs like that? Should I make it possible to pick it by user, or auto get his screen resolution?

8
General / Re: Wont run on windows 10
« on: December 07, 2015, 12:22:54 pm »
I've attached some libs and all necessary (mostly txt and png) files.

My friend said me that some apps using openGL were working fine: minecraft, google earth

Zippy
Pass: sfmldev
vt scan of .exe file

Btw I've tried to execute this on the other platforms and devices. Works fine on desktop winME-win8 and virtualbox' win10.

9
General / Re: Wont run on windows 10
« on: December 05, 2015, 11:52:34 pm »
Theres no console output. I've sent him ready to go .exe file (with additional files, libs etc). Should I upload this to you?

10
General / Re: Wont run on windows 10
« on: December 05, 2015, 10:58:54 am »
I've got the same problem. My app is working correctly on my PC with win8 64bit. When I send it to a friend with win10 64 bit, he can open it as usual, but only the console is visible. It also displays main window, but it is empty. What should I do, or what info do you need to help me?

11
Graphics / Re: Proper management with class objects
« on: June 09, 2015, 12:30:34 pm »
I was wondering if the second option of yours wouldn't be better. Could you tell me why it is, or why isn't? This function is used once, to load all data.

12
Graphics / Re: Proper management with class objects
« on: June 08, 2015, 07:49:57 pm »
Solved.

// vector of class objects

vector<Mobs_normal> mobsNormal;

// loading data
Mobs_normal newMob;
       mobsNormal.push_back(newMob);
       file_mobs_normal >> mobsNormal[i].name;
       file_mobs_normal >> mobsNormal[i].lvl;
//and so on

// e. g. use in main function

cout << cEntity.mobsNormal[ i ].name << endl;
 

13
Graphics / Proper management with class objects
« on: June 05, 2015, 10:41:06 pm »
Hello there,
I've got a little problem with vectors containing class objects.

I want to create two std::vectors containing:
1. objects of class:
class Mobs_normal
{
public:

int id;
string name;
int lvl;
int dmg_min;
int dmg_max;
int hp;
int critChance;
};

Which would be a kind of mob templates.

2. objects of class:
class Entity
{
public:
vector<Mobs_normal> mobsNormal;

void load()
{
// data base
fstream file_mobs_normal;
file_mobs_normal.open("entity/mobs_normal.txt", ios::in );

if (!file_mobs_normal.good() )  {error("mobs_normal.txt"); } // 404

// mobs were an int array, but i want them to be vectors
for (int i=0; i < 11; i++ )
    {
 
       mobsNormal.name.push_back() // ?
       file_mobs_normal >> mobsStack[i].name;
       file_mobs_normal >> mobsStack[i].lvl;
       file_mobs_normal >> mobsStack[i].dmg_min;
       file_mobs_normal >> mobsStack[i].dmg_max;
       file_mobs_normal >> mobsStack[i].hp;
       file_mobs_normal >> mobsStack[i].critChance;
    }
}

void drawMobs(sf::RenderWindow & okno, Textures & cTex)
{
  // temp drawing
  for (int i=0; i < cTex.sprEntity.size(); i++)
  {
   cTex.sprEntity[ i ].setPosition((1+i)*50, 50);
   okno.draw(cTex.sprEntity[ i ] );
  }
}

void enableAiMovement(sf::RenderWindow & okno, Textures & cTex)
{
// moving objects on map and switches for "do something" methods
}

};

Which would be templates handling ai of objects drawn at the time on your screen, containing their movement and other actions on map.

I wonder if it's the best solution for this problem.
How should I create a vector containing all information of objects on map (to interact with Player)?
One for all those stats (id, name, lvl, hp) or one for every single stat? I would connect them with "id" iterator.
I want to achieve a quick access to those stats and sprite drawing methods by their int id;

Attachment: database with stats

14
Graphics / Re: Loading sprites to vector.
« on: May 26, 2015, 01:23:13 am »
@Hapax
It works as I wanted to.
Quote
Is drawTile() in the public section of the Textures class?
Yes, it's public. Shouldn't it?

@Nexus
I've had an idea to make them global, but it was smelling to easy to be a good solution.
Is const necessary?

Just have read this, good point, Equipment was only for set, get and swap things, plus some calls for cPrimitives, containing shapes, vertexes etc.

By the way, after changing texture loading from "manual" to vectors I've got some additional errors - e.g. some not defined integers were set to random values instead of 0. Anyway, setting them to 0 with all load-kind methods worked.

Thank you very much for your help and kindness.

15
Graphics / Re: Loading sprites to vector.
« on: May 25, 2015, 11:54:01 pm »
It is, indeed.

The problem appears when I try to access them in the same class, in
void drawTile()
method, and in
class Equipment : public Textures
class with this
void drawEq(sf::RenderWindow & okno, int eqOriginX, int eqOriginY, float cameraX, float cameraY)
{

for(int i=0; i<4; i++)
    {
        for(int j=0; j<7; j++)
        {
         drawTile(okno, 1, eqContainer[ i ][ j ], cameraX+eqOriginX+i*50, cameraY+eqOriginY+j*50); //(rendered window, tileType, tileID- from array, somewhereX, somewhereY)
        }
    }

}
 
method.

Aren't sprites global objects? If so, how should I pass them by reference to any other function?
I've got them loaded in cTex object of Texture class, so from my main function it would be like this? (passing)
some_function(cTex.sprWeapon1h, cTex.texWeapon1h)
and receiving
some_function(vector<sf::Sprite> & sprites, vector<sf::Texture> & textures)
Is the size of vectors saved anyway? I mean, should I use them as usual after this operation?

Ad ps: It's yet simplified code, but I'll take it into account.

Pages: [1] 2
anything