Jump to content

  • Log In with Google      Sign In   
  • Create Account


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

#5078536 Gravity Practice 2D

Posted by mypel16000 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


#5031189 Animations

Posted by mypel16000 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:




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

#5027264 Implementation - SFML

Posted by mypel16000 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;

			SearchState = astarsearch.SearchStep();



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

			int len = 0;

			cout << "Open:\n";
			MapSearchNode *p = astarsearch.GetOpenListStart();
			while( p )
				((MapSearchNode *)p)->PrintNodeInfo();
				p = astarsearch.GetOpenListNext();


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

			len = 0;

			cout << "Closed:\n";
			p = astarsearch.GetClosedListStart();
			while( p )
				p = astarsearch.GetClosedListNext();

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

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

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

				MapSearchNode *node = astarsearch.GetSolutionStart();

				cout << "Displaying solution\n";
				int steps = 0;

				for( ;; )
					node = astarsearch.GetSolutionNext();

					if( !node )

					steps ++;


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

				// Once you're done with the solution you can free the nodes up

		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 ++;


	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

#5026552 Collision in walls - SFML

Posted by mypel16000 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

#5026506 Collision in walls - SFML

Posted by mypel16000 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


|     |

|     |____



#5025840 A star

Posted by mypel16000 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

#5025835 A star

Posted by mypel16000 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?

#5025832 A star

Posted by mypel16000 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

#5025827 A star

Posted by mypel16000 on 26 January 2013 - 12:46 PM

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

#5025823 A star

Posted by mypel16000 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?

#5025822 A star

Posted by mypel16000 on 26 January 2013 - 12:41 PM

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

#5024453 A star

Posted by mypel16000 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

        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

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

            // Euclidian Distance

            // Manhattan distance

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


// 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;

    // reset the node maps

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

    // A* search
        // 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
        // mark it on the closed nodes map

        // 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))

            // 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
            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(),
                m0->updatePriority(xFinish, yFinish);

                // if it is not in the open list then add into that
                    // mark its parent node direction
                else if(open_nodes_map[xdx][ydy]>m0->getPriority())
                    // update the priority info
                    // update the parent direction info

                    // 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].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;
                    pq[pqi].push(*m0); // add the better node instead
                else delete m0; // garbage collection
        delete n0; // garbage collection
    return ""; // no route found



#5020436 Projectile Movement C++

Posted by mypel16000 on 11 January 2013 - 02:03 PM


#5019660 Projectile Movement C++

Posted by mypel16000 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)







*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.

#5018215 SFML and Code::Blocks

Posted by mypel16000 on 06 January 2013 - 09:57 AM

You just don't realize it..... It's not about the code. I wrote that in 2 secs on game dev. I didn't copy it directly! I have fixed it already and it's an issue with the latest version of CodeBlocks.