• Create Account

# mypel16000

Member Since 31 Oct 2012
Offline Last Active Dec 20 2013 03:41 PM

### In Topic: Attempt at A star algorithm not working

19 December 2013 - 05:44 AM

I understand, but that's not the problem. It is the pathfinding that isn't working. It is not going the right way. Whilst I appreciate these tips for improving my code, the real problem is that the algorithm doesn't work. I'm thinking there might be a problem calculating the F G and H scores, or a problem in the code which looks at adjacent nodes

### In Topic: Attempt at A star algorithm not working

19 December 2013 - 04:06 AM

The map is composed of 40x30 tiles each one of a width and height of 64 pixels. This makes a map of 2560x1920 pixels.

They are both, two specific nodes formed by a Node class as follows:

```#pragma once
#include "Position.h"
class Node
{
public:
Node(void);
Node(int x, int y, bool w);
Position* getPosition();
Node* getParentNode();
void setParentNode(Node *p);
int getX();
int getY();
int getGScore(Node *p);
int getHScore(Node *p);
int getGScore();
int getHScore();
int getFScore();

void computeScores(Node *end);

bool hasParent();
bool closed, opened;
bool walkable;

private:

Node* parent;
int x, y, f, g, h;

};
```

And implementation:

```#include "Node.h"

Node::Node(void)
{
parent = NULL;
closed = false;
opened = false;

x = y = f = g = f = 0;
}

Node::Node( int x, int y, bool w)
{
Node();

this->walkable = w;
this->x = x;
this->y = y;
}

Position* Node::getPosition()
{
return new Position(sf::Vector2f((float)(x * 64), (float)(y * 64)));
}

Node* Node::getParentNode()
{
return parent;
}

void Node::setParentNode(Node *p)
{
parent = p;
}

int Node::getX()
{
return x;
}

int Node::getY()
{
return y;
}

int Node::getGScore(Node *p)
{
return p->g + ((x == p->x || y == p->y) ? 10:14);
//changed last 10 from 14 to 10
}

int Node::getHScore(Node *p)
{
return ((abs(p->x - x) + abs(p->y - y)) *10 );
}

int Node::getGScore()
{
return g;
}

int Node::getHScore()
{
return h;
}

int Node::getFScore()
{
return f;
}

void Node::computeScores(Node *end)
{
g = getGScore(parent);
h = getHScore(end);
f = g + h;
}

bool Node::hasParent()
{
return parent != NULL;
}
```

Just  to make it clear, the node's x and y are its coordinates in the node map, starting from 0,0 and going to 40,30. If a point were at (70,70) it would be in the node with coordinates (1,1)  (the second down and to the right) as there is a 0,0 tile a 0,1 and a 1,0.

The functions getPoint() and getPointFromCoord() return the node with a given set of coordinates. getPoint uses the node's coordinate in the tile map of 40x30 where as the latter gets it from a set of coordinates in the actual pixel map (just by dividing by 64). Note that both list is a list containing all the nodes in the map. Here they are:

```Node* Pathfinder::getNodeFromCoord(int x, int y)
{
for ( i = list.begin(); i != list.end(); i++ )
{
if((*i)->getX() == (int)(x / 64)
&& (*i)->getY() == (int)(y / 64))
{
return (*i);
}
}
}

Node* Pathfinder::getPoint(int x, int y)
{
for ( i = list.begin(); i != list.end(); i++ )
{
if((*i)->getX() == (int)(x)
&& (*i)->getY() == (int)y)
{
return (*i);
}
}
}
```

### In Topic: Attempt at A star algorithm not working

18 December 2013 - 04:53 PM

I am aware of that, and I mentioned I was, but that isn't my problem. The problem is it should be going towards node (0, 0) when instead, it's following a weird pattern and then suddendly jumping to the other side for no immediatly apparent reason. Thanks for your help though

### In Topic: Gravity Practice 2D

17 July 2013 - 02:25 PM

Thanks guys, useful stuff! +1s.

However, can you answer how I would deal with the force upwards when clicking the ball? Does it just change the acceleration, and then I let the formula continue decreasing this upwards velocity till it becomes negative, hence dropping the object again?