• Create Account

# mypel16000

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

### #5078536Gravity Practice 2D

Posted by on 17 July 2013 - 02:14 PM

Hi, I have been looking at many posts, but none of them really explain what I want to know.

I want to create a "gravity ball" game for practice with gravity so that I can later implement it into my game.

The game consists in a ball dropping with gravity, and you having to click it to push it back up again.

I am not yet concerned with sideways movement as I'd have to calculate angles and stuff, and I only want to focus on gravity itself now.

I have coded this as a gravity function:

```acceleration += 9.8;
velocity += acceleration;
y += velocity;```

I have however, a series of conceptual problems as I do not truly understand the gravity equation. The following are my questions:

Acceleration is measured as ms^-2, so I would only have to compute this equation every second. This would then make my game change velocity every second and therefore would give an non-smooth effect. How would I go about computing this equation 60 times per second? I think there is an equation for gravity involving time as time an acceleration are related, is there such a thing?

Another thing I can't get my head around is how gravity works when the object is moving upwards. If the downward speed is of, say, 50m/s, and we exert a force that makes it have an upwards speed of 100m/s, how does the formula create the parabola effect needed? Do you reset the timer and the acceleration inside the gravity equation, so that the first second its going upwards it gets a velocity of 90 m/s, and then its velocity keeps decreasing till it flips around and starts falling again? When do I reset the timer?

Please explain how it works, or give me an example of the code used for such an exercise.

Thank you

### #5031189Animations

Posted by on 11 February 2013 - 02:22 PM

Wow, thanks everyone. This will hopefully help people in need. I was just wondering, because I though you probably would be able to quickly create sprites. I did some extra research and found this:

Spriter

It is a piece of software to create animations. It is very easy to use and works with key-frames, so hopefully, it will be useful for many. You basically have a timeline in which you can add transformations. For a simple, enlarging animation, you could start with a scale factor of 1 and add a keyframe after a second which had a scale factor of 2. What this will do is gradually increase the size of the sprite until it doubles at the end of the second. It can then be exported with as many frames as you want (you don´t have to do them individually!) Great help, seriously

### #5027264Implementation - SFML

Posted by on 30 January 2013 - 01:08 PM

Hi, even with the lack of help from the members of this forum who answered my first A star question (bringing my reputation down by 200), I have managed to get an A star recipe which was very useful. The thing is that I don't know how to implement it onto my project as it comes with no instructions.

Now, I will need someone very good with C++ to show me what functions i should use.

The code in the int main() that is shown in the code:

```int main( int argc, char *argv[] )
{

cout << "STL A* Search implementation\n©2001 Justin Heyes-Jones\n";

// Our sample problem defines the world as a 2d array representing a terrain
// Each element contains an integer from 0 to 5 which indicates the cost
// of travel across the terrain. Zero means the least possible difficulty
// in travelling (think ice rink if you can skate) whilst 5 represents the
// most difficult. 9 indicates that we cannot pass.

// Create an instance of the search class...

AStarSearch<MapSearchNode> astarsearch;

unsigned int SearchCount = 0;

const unsigned int NumSearches = 1;

while(SearchCount < NumSearches)
{

// Create a start state
MapSearchNode nodeStart;
nodeStart.x = rand()%MAP_WIDTH;
nodeStart.y = rand()%MAP_HEIGHT;

// Define the goal state
MapSearchNode nodeEnd;
nodeEnd.x = rand()%MAP_WIDTH;
nodeEnd.y = rand()%MAP_HEIGHT;

// Set Start and goal states

astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd );

unsigned int SearchState;
unsigned int SearchSteps = 0;

do
{
SearchState = astarsearch.SearchStep();

SearchSteps++;

#if DEBUG_LISTS

cout << "Steps:" << SearchSteps << "\n";

int len = 0;

cout << "Open:\n";
MapSearchNode *p = astarsearch.GetOpenListStart();
while( p )
{
len++;
#if !DEBUG_LIST_LENGTHS_ONLY
((MapSearchNode *)p)->PrintNodeInfo();
#endif
p = astarsearch.GetOpenListNext();

}

cout << "Open list has " << len << " nodes\n";

len = 0;

cout << "Closed:\n";
p = astarsearch.GetClosedListStart();
while( p )
{
len++;
#if !DEBUG_LIST_LENGTHS_ONLY
p->PrintNodeInfo();
#endif
p = astarsearch.GetClosedListNext();
}

cout << "Closed list has " << len << " nodes\n";
#endif

}
while( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SEARCHING );

if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_SUCCEEDED )
{
cout << "Search found goal state\n";

MapSearchNode *node = astarsearch.GetSolutionStart();

#if DISPLAY_SOLUTION
cout << "Displaying solution\n";
#endif
int steps = 0;

node->PrintNodeInfo();
for( ;; )
{
node = astarsearch.GetSolutionNext();

if( !node )
{
break;
}

node->PrintNodeInfo();
steps ++;

};

cout << "Solution steps " << steps << endl;

// Once you're done with the solution you can free the nodes up
astarsearch.FreeSolutionNodes();

}
else if( SearchState == AStarSearch<MapSearchNode>::SEARCH_STATE_FAILED )
{
cout << "Search terminated. Did not find goal state\n";

}

// Display the number of loops the search went through
cout << "SearchSteps : " << SearchSteps << "\n";

SearchCount ++;

astarsearch.EnsureMemoryFreed();
}

return 0;
}
```

My game will consist of a vector of zombies which will try and get to the player through a map with obstacles. I know how to create the map, but I need to know how to make this powerful code make my zombies follow the player without going through the obstacles. I would like to know how to initialize the code, what to loop for each zombie in the vector and how to get the player to move node by node to the player. Also, as my player will keep moving, this needs to be updated every second or so.

I know this is a tough job, but please, if you are really good with C++ and have experience with A*, help me.

Thank you

### #5026552Collision in walls - SFML

Posted by on 28 January 2013 - 04:22 PM

Thank you beernuts! Most helpfull of all.

And Servant of the lord, I appreciate your effort, but the room isn't actually L shaped. There is a counter with an L shape inside the room

### #5026506Collision in walls - SFML

Posted by on 28 January 2013 - 02:28 PM

Hi, thanks for the info, but I find it is poorly explained. As I have many walls in my room, how would I check for collision on all of them. INCLUDING AN L SHAPE

___

|     |

|     |____

|_______|

????

### #5026008A star

Posted by on 27 January 2013 - 04:22 AM

Thank you for all the -1s.... I am just desperate as I have been trying to fix this for weeks. I have realised that the error isn't in the code... It is just in my project. I am using Code::Blocks and SFML. Even if I don't include it in the main function and even if I don't include anything froom the rest of the project in it. It returns all the same errors (see above). It is the most bizarre thing I have ever seen, and I have no idea what might be causing it. Is there some compatibility issue with codeblocks or just something? The code for the working one is this:
.length();i++)>;y++)>;i++)>;y++)>.h>.h>

```#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include <math.h>
#include <ctime>
#include <cstdlib>
#include <stdio.h>

int n=60; // horizontal size of the map
int m=60; // vertical size size of the map
int map[60][60];
int closed_nodes_map[60][60]; // map of closed (tried-out) nodes
int open_nodes_map[60][60]; // map of open (not-yet-tried) nodes
int dir_map[60][60]; // map of directions
const int dir=8; // number of possible directions to go at any position
// if dir==4
//static int dx[dir]={1, 0, -1, 0};
//static int dy[dir]={0, 1, 0, -1};
// if dir==8
static int dx[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dy[dir]={0, 1, 1, 1, 0, -1, -1, -1};

class node
{
// current position
int xPos;
int yPos;
// total distance already travelled to reach the node
int level;
// priority=level+remaining distance estimate
int priority;  // smaller: higher priority

public:
node(int xp, int yp, int d, int p)
{xPos=xp; yPos=yp; level=d; priority=p;}

int getxPos() const {return xPos;}
int getyPos() const {return yPos;}
int getLevel() const {return level;}
int getPriority() const {return priority;}

void updatePriority(const int & xDest, const int & yDest)
{
priority=level+estimate(xDest, yDest)*10; //A*
}

// give better priority to going strait instead of diagonally
void nextLevel(const int & i) // i: direction
{
level+=(dir==8?(i%2==0?10:14):10);
}

// Estimation function for the remaining distance to the goal.
const int & estimate(const int & xDest, const int & yDest) const
{
static int xd, yd, d;
xd=xDest-xPos;
yd=yDest-yPos;

// Euclidian Distance
d=static_cast<int>(sqrt(xd*xd+yd*yd));

// Manhattan distance
//d=abs(xd)+abs(yd);

// Chebyshev distance
//d=max(abs(xd), abs(yd));

return(d);
}
};

// Determine priority (in the priority queue)
bool operator<(const node & a, const node & b)
{
return a.getPriority() > b.getPriority();
}

// A-star algorithm.

// The route returned is a string of direction digits.
std::string pathFind( const int & xStart, const int & yStart,
const int & xFinish, const int & yFinish )
{
static std::priority_queue<node> pq[2]; // list of open (not-yet-tried) nodes
static int pqi; // pq index
static node* n0;
static node* m0;
static int i, j, x, y;
int xla = 60;
int ydy = 60;
static char c;
pqi=0;

// reset the node maps
for(y=0;y<m;y++)
{
for(x=0;x<n;x++)
{
closed_nodes_map[x][y]=0;
open_nodes_map[x][y]=0;
}
}

// create the start node and push into list of open nodes
n0=new node(xStart, yStart, 0, 0);
n0->updatePriority(xFinish, yFinish);
pq[pqi].push(*n0);
open_nodes_map[x][y]=n0->getPriority(); // mark it on the open nodes map

// A* search
while(!pq[pqi].empty())
{
// get the current node w/ the highest priority
// from the list of open nodes
n0=new node( pq[pqi].top().getxPos(), pq[pqi].top().getyPos(),
pq[pqi].top().getLevel(), pq[pqi].top().getPriority());

x=n0->getxPos(); y=n0->getyPos();

pq[pqi].pop(); // remove the node from the open list
open_nodes_map[x][y]=0;
// mark it on the closed nodes map
closed_nodes_map[x][y]=1;

// quit searching when the goal state is reached
//if((*n0).estimate(xFinish, yFinish) == 0)
if(x==xFinish && y==yFinish)
{
// generate the path from finish to start
// by following the directions
std::string path="";
while(!(x==xStart && y==yStart))
{
j=dir_map[x][y];
c='0'+(j+dir/2)%dir;
path=c+path;
x+=dx[j];
y+=dy[j];
}

// garbage collection
delete n0;
// empty the leftover nodes
while(!pq[pqi].empty()) pq[pqi].pop();
return path;
}

// generate moves (child nodes) in all possible directions
for(i=0;i<dir;i++)
{
xla=x+dx[i]; ydy=y+dy[i];

if(!(xla<0 || xla>n-1 || ydy<0 || ydy>m-1 || map[xla][ydy]==1
|| closed_nodes_map[xla][ydy]==1))
{
// generate a child node
m0=new node( xla, ydy, n0->getLevel(),
n0->getPriority());
m0->nextLevel(i);
m0->updatePriority(xFinish, yFinish);

// if it is not in the open list then add into that
if(open_nodes_map[xla][ydy]==0)
{
open_nodes_map[xla][ydy]=m0->getPriority();
pq[pqi].push(*m0);
// mark its parent node direction
dir_map[xla][ydy]=(i+dir/2)%dir;
}
else if(open_nodes_map[xla][ydy]>m0->getPriority())
{
// update the priority info
open_nodes_map[xla][ydy]=m0->getPriority();
// update the parent direction info
dir_map[xla][ydy]=(i+dir/2)%dir;

// replace the node
// by emptying one pq to the other one
// except the node to be replaced will be ignored
// and the new node will be pushed in instead
while(!(pq[pqi].top().getxPos()==xla &&
pq[pqi].top().getyPos()==ydy))
{
pq[1-pqi].push(pq[pqi].top());
pq[pqi].pop();
}
pq[pqi].pop(); // remove the wanted node

// empty the larger size pq to the smaller one
if(pq[pqi].size()>pq[1-pqi].size()) pqi=1-pqi;
while(!pq[pqi].empty())
{
pq[1-pqi].push(pq[pqi].top());
pq[pqi].pop();
}
pqi=1-pqi;
}
else delete m0; // garbage collection
}
}
delete n0; // garbage collection
}
return ""; // no route found
}

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

// create empty map
for(int y=0;y<m;y++)
{
for(int x=0;x<n;x++) map[x][y]=0;
}

// fillout the map matrix with a '+' pattern
for(int x=n/8;x<n*7/8;x++)
{
map[x][m/2]=1;
}
for(int y=m/8;y<m*7/8;y++)
{
map[n/2][y]=1;
}

// randomly select start and finish locations
int xA, yA, xB, yB;
switch(rand()%8)
{
case 0: xA=0;yA=0;xB=n-1;yB=m-1; break;
case 1: xA=0;yA=m-1;xB=n-1;yB=0; break;
case 2: xA=n/2-1;yA=m/2-1;xB=n/2+1;yB=m/2+1; break;
case 3: xA=n/2-1;yA=m/2+1;xB=n/2+1;yB=m/2-1; break;
case 4: xA=n/2-1;yA=0;xB=n/2+1;yB=m-1; break;
case 5: xA=n/2+1;yA=m-1;xB=n/2-1;yB=0; break;
case 6: xA=0;yA=m/2-1;xB=n-1;yB=m/2+1; break;
case 7: xA=n-1;yA=m/2+1;xB=0;yB=m/2-1; break;
}

std::cout<<"Map Size (X,Y): "<<n<<","<<m<<std::endl;
std::cout<<"Start: "<<xA<<","<<yA<<std::endl;
std::cout<<"Finish: "<<xB<<","<<yB<<std::endl;
// get the route
clock_t start = clock();
std::string route=pathFind(xA, yA, xB, yB);
if(route=="") std::cout<<"An empty route generated!"<<std::endl;
clock_t end = clock();
double time_elapsed = double(end - start);
std::cout<<"Time to calculate the route (ms): "<<time_elapsed<<std::endl;
std::cout<<"Route:"<<std::endl;
std::cout<<route<<std::endl<<std::endl;

// follow the route on the map and display it
if(route.length()>0)
{
int j; char c;
int x=xA;
int y=yA;
map[x][y]=2;
for(int i=0;i<route.length();i++)
{
c =route.at(i);
j=atoi(&c);
x=x+dx[j];
y=y+dy[j];
map[x][y]=3;
}
map[x][y]=4;

// display the map with the route
for(int y=0;y<m;y++)
{
for(int x=0;x<n;x++)
if(map[x][y]==0)
std::cout<<".";
else if(map[x][y]==1)
std::cout<<"O"; //obstacle
else if(map[x][y]==2)
std::cout<<"S"; //start
else if(map[x][y]==3)
std::cout<<"R"; //route
else if(map[x][y]==4)
std::cout<<"F"; //finish
std::cout<<std::endl;
}
}
getchar(); // wait for a (Enter) keypress
return(0);
}

```

This works fine on its own if you want to try it
To implement it to my project, I created a .cpp file and copied all of this code (apart from the main). I then said #include "Astar.cpp" in my main.cpp, where the main is and all other files are included.length();i++)>;y++)>;i++)>;y++)>.h>.h>

### #5025840A star

Posted by on 26 January 2013 - 01:17 PM

there is no point.... you aren't gonna help in anyway like that. I just need a pre-made implementation please

### #5025835A star

Posted by on 26 January 2013 - 01:07 PM

its the same, but without using namespace std, include "include.h", and with all the std::s. Do you really need it again?

### #5025832A star

Posted by on 26 January 2013 - 12:53 PM

Its posted at the beggining.... It's the same. I have gone back to what i had on the first place as by changing the names, i started getting hundreds of errors

### #5025827A star

Posted by on 26 January 2013 - 12:46 PM

I have tried renaming map and YES! I AM SURE I HAVE DELETED using namespace std.

### #5025823A star

Posted by on 26 January 2013 - 12:43 PM

Yes, I got rid of the include "include.h" Is there a pre-made code I can have?

### #5025822A star

Posted by on 26 January 2013 - 12:41 PM

If this doesn't work, can you give me a pre-made recipe or something please!

### #5024453A star

Posted by on 22 January 2013 - 03:38 PM

I have this A* code, but I am desperate, it keeps giving me errors and I have no idea about what is going on. The code is quite big, but I can post the build errors so you guys can spot where the problems are; I'm not sure if anyone can help, as to help, one needs to be an ace at this.

Build errors:

In function 'std::string pathFind(const int&, const int&, const int&, const int&)':|
|156|error: reference to 'map' is ambiguous|
|16|error: candidates are: int map [60][60]|

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|86|error:
template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map|

|156|error: reference to 'map' is ambiguous|
|16|error: candidates are: int map [60][60]|

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|86|error:
template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map|

|93|warning: unused variable 'xdx'|
|93|warning: unused variable 'ydy'|

||=== Build finished: 6 errors, 2 warnings ===|

```#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include <math.h>
#include <ctime>
#include "include.h"
using namespace std;

const int n=60; // horizontal size of the map
const int m=60; // vertical size size of the map
int map[n][m];
static int closed_nodes_map[n][m]; // map of closed (tried-out) nodes
static int open_nodes_map[n][m]; // map of open (not-yet-tried) nodes
static int dir_map[n][m]; // map of directions
const int dir=8; // number of possible directions to go at any position
// if dir==4
//static int dx[dir]={1, 0, -1, 0};
//static int dy[dir]={0, 1, 0, -1};
// if dir==8
static int dx[dir]={1, 1, 0, -1, -1, -1, 0, 1};
static int dy[dir]={0, 1, 1, 1, 0, -1, -1, -1};

class node
{
// current position
int xPos;
int yPos;
// total distance already travelled to reach the node
int level;
// priority=level+remaining distance estimate
int priority;  // smaller: higher priority

public:
node(int xp, int yp, int d, int p)
{xPos=xp; yPos=yp; level=d; priority=p;}

int getxPos() const {return xPos;}
int getyPos() const {return yPos;}
int getLevel() const {return level;}
int getPriority() const {return priority;}

void updatePriority(const int & xDest, const int & yDest)
{
priority=level+estimate(xDest, yDest)*10; //A*
}

// give better priority to going strait instead of diagonally
void nextLevel(const int & i) // i: direction
{
level+=(dir==8?(i%2==0?10:14):10);
}

// Estimation function for the remaining distance to the goal.
const int & estimate(const int & xDest, const int & yDest) const
{
static int xd, yd, d;
xd=xDest-xPos;
yd=yDest-yPos;

// Euclidian Distance
d=static_cast<int>(sqrt(xd*xd+yd*yd));

// Manhattan distance
//d=abs(xd)+abs(yd);

// Chebyshev distance
//d=max(abs(xd), abs(yd));

return(d);
}
};

// Determine priority (in the priority queue)
bool operator<(const node & a, const node & b)
{
return a.getPriority() > b.getPriority();
}

// A-star algorithm.
// The route returned is a string of direction digits.
string pathFind( const int & xStart, const int & yStart,
const int & xFinish, const int & yFinish )
{
static priority_queue<node> pq[2]; // list of open (not-yet-tried) nodes
static int pqi; // pq index
static node* n0;
static node* m0;
static int i, j, x, y, xdx, ydy;
static char c;
pqi=0;

// reset the node maps
for(y=0;y<m;y++)
{
for(x=0;x<n;x++)
{
closed_nodes_map[x][y]=0;
open_nodes_map[x][y]=0;
}
}

// create the start node and push into list of open nodes
n0=new node(xStart, yStart, 0, 0);
n0->updatePriority(xFinish, yFinish);
pq[pqi].push(*n0);
open_nodes_map[x][y]=n0->getPriority(); // mark it on the open nodes map

// A* search
while(!pq[pqi].empty())
{
// get the current node w/ the highest priority
// from the list of open nodes
n0=new node( pq[pqi].top().getxPos(), pq[pqi].top().getyPos(),
pq[pqi].top().getLevel(), pq[pqi].top().getPriority());

x=n0->getxPos(); y=n0->getyPos();

pq[pqi].pop(); // remove the node from the open list
open_nodes_map[x][y]=0;
// mark it on the closed nodes map
closed_nodes_map[x][y]=1;

// quit searching when the goal state is reached
//if((*n0).estimate(xFinish, yFinish) == 0)
if(x==xFinish && y==yFinish)
{
// generate the path from finish to start
// by following the directions
string path="";
while(!(x==xStart && y==yStart))
{
j=dir_map[x][y];
c='0'+(j+dir/2)%dir;
path=c+path;
x+=dx[j];
y+=dy[j];
}

// garbage collection
delete n0;
// empty the leftover nodes
while(!pq[pqi].empty()) pq[pqi].pop();
return path;
}

// generate moves (child nodes) in all possible directions
for(i=0;i<dir;i++)
{
int xdx = x+dx[i]; int ydy=y+dy[i];

if(!(xdx<0 || xdx>n-1 || ydy<0 || ydy>m-1 || map[xdx][ydy]==1 || closed_nodes_map[xdx][ydy]== 1))
{
// generate a child node
m0=new node( xdx, ydy, n0->getLevel(),
n0->getPriority());
m0->nextLevel(i);
m0->updatePriority(xFinish, yFinish);

// if it is not in the open list then add into that
if(open_nodes_map[xdx][ydy]==0)
{
open_nodes_map[xdx][ydy]=m0->getPriority();
pq[pqi].push(*m0);
// mark its parent node direction
dir_map[xdx][ydy]=(i+dir/2)%dir;
}
else if(open_nodes_map[xdx][ydy]>m0->getPriority())
{
// update the priority info
open_nodes_map[xdx][ydy]=m0->getPriority();
// update the parent direction info
dir_map[xdx][ydy]=(i+dir/2)%dir;

// replace the node
// by emptying one pq to the other one
// except the node to be replaced will be ignored
// and the new node will be pushed in instead
while(!(pq[pqi].top().getxPos()==xdx &&
pq[pqi].top().getyPos()==ydy))
{
pq[1-pqi].push(pq[pqi].top());
pq[pqi].pop();
}
pq[pqi].pop(); // remove the wanted node

// empty the larger size pq to the smaller one
if(pq[pqi].size()>pq[1-pqi].size()) pqi=1-pqi;
while(!pq[pqi].empty())
{
pq[1-pqi].push(pq[pqi].top());
pq[pqi].pop();
}
pqi=1-pqi;
}
else delete m0; // garbage collection
}
}
delete n0; // garbage collection
}
return ""; // no route found
}
```

### #5020436Projectile Movement C++

Posted by on 11 January 2013 - 02:03 PM

Soz

### #5019660Projectile Movement C++

Posted by on 09 January 2013 - 04:36 PM

Hi, I am using C++ and trying to make my player shoot projectiles. I have done OK so far, but the problem is that what I have done to move them is just use:

bulletX += xOffset( betwen player and mouse ) /100

bulletY += yOffset / 100

This gave me the right direction although there was an issue, the closer the mouse was to the player, the slower the bullet went ( as you can probably tell from the code above ). I have tried:

```
```

Dlen = sqrt(Dx*Dx + Dy*Dy)

Dx/=Dlen;

Dy/=Dlen

Bx=Bx+Dx*Bs;

By=By+Dy*Bs;

*Dlen = normalised value, Dx = xOffset, Dy = yOffset, Bx= bulletX, By= bulletY, Bs = bullet Speed.

but I was unsuccesful.... the bullet went in completely random directions very different to where I was aiming.

I also tried to find a ratio :

yOffset / xOffset and then always move X by 1 or -1 and y by 1 or -1 multiplied by the ratio.

Again unscuccesful.

Is there any way to achieve a constant speed when shooting projectiles from a player in the direction of the mouse cursor that will work? Please put an explained example.

PARTNERS