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

Topics I've Started

Attempt at A star algorithm not working

18 December 2013 - 04:17 PM

Help please, I am attempting to create an A* algorithm to do some pathfinding in C++.


The function I use is:

pathfinder.aStar(400, 300, 25, 25);

Which is a fairly simple diagonal movement.


My map is divided in 64x64 nodes. And for some reason, the pathfinding follows the following path of nodes:











And crashes as only 39x29 nodes were created and it wants to go onto (39,30)


My code is as follows:

 std::vector<Position*> Pathfinder::aStar(int x1, int y1, int x2, int y2)
	start = getNodeFromCoord(x1, y1);
	endNode = getNodeFromCoord(x2, y2);
	n = 0;

	start->opened = true;

	while (n == 0 || (current != endNode && n < 50))
		//Look for the smallest F value in the openList and make it the current point

		for (i = openList.begin(); i != openList.end(); ++ i)
			if (i == openList.begin() || (*i)->getFScore() <= current->getFScore())
				current = (*i);

		if ( current == endNode )

		//Change from open to closed list
		current->opened = false;
		current->closed = true;

		// Get all current's adjacent walkable points

		for (int x = -1; x < 2; x ++)
			for (int y = -1; y < 2; y ++)
				// If it's current point then pass
				if (x == 0 && y == 0)

				// Get this point

				child = getPoint(current->getX() + x, current->getY() + y);
				std::cout << current->getX() + x << " --- " << current->getY() + y << std::endl;
				std::cout<<child->getX() << "  " << child->getY() << std::endl << std::endl;

				// If it's closed or not walkable then pass
				if (child->closed || !child->walkable)

				// If we are at a corner
				if (x != 0 && y != 0)
					// if the next horizontal point is not walkable or in the closed list then pass
					if (!nodeIsWalkable(current->getX(), current->getY() + y) || getPoint(current->getX(), current->getY() + y)->closed)
					// if the next vertical point is not walkable or in the closed list then pass
					if (!nodeIsWalkable(current->getX() + x, current->getY()) || getPoint(current->getX() + x, current->getY())->closed)

				// If it's already in the openList
				if (child->opened)
					// If it has a worse g score than the one that pass through the current point
					// then its path is improved when it's parent is the current point
					if (child->getGScore() > child->getGScore(current))
						// Change its parent and g score
					// Add it to the openList with current point as parent
					child->opened = true;

					// Compute it's g, h and f score

	for (i = openList.begin(); i != openList.end(); ++ i)
		(*i)->opened = false;
	for (i = closedList.begin(); i != closedList.end(); ++ i)
		(*i)->closed = false;

	while (current->hasParent() && current != start)
		current = current->getParentNode();
		n ++;

	return path;

Any ideas? Thank you


Gravity Practice 2D

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


Screen Particles Help

25 May 2013 - 06:35 AM

Hello, I wanted to know how to make an effect of dust going past the screen. I'm talking in terms of Call of Duty where there is always stuff moving in the air. Is this some sort of particle implementation?


Could someone tell me how its done or at least point me in the direction of a good tutorial?


P.S: I use SFML, but simple concepts non-related to this engine will be useful.


Thank you

Advanced C++

03 April 2013 - 05:20 PM

Hi there,


September last year I started to learn C++ for game programming. I have got on pretty well, I finished a youtube series of tutorials and through my own research and help from some forums I have developed some very good projects which I'm proud of. The problem has come now that I'm trying to develop an A star Algorithm program. I can understand the theory, but I just cannot code it or understand recipes.


I am looking for some advanced C++ tutorials,


I know all the functions, classes, data types and all that simple stuff, what I want to learn is some things like maps, nodes, advanced poiters, vectors... Things which those simple tutorials don't teach.


Any help, links, blogs...?

Shooting in SFML

02 April 2013 - 01:15 PM

I have used this method to implement Projectile movement into my shooting game:


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






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


The problem is that this function changes the speed of the projectile depending on how close the mouse (aiming point) is to the player (shooting point). It also seems like it draws 6 separate bullets as it doesn't move it smoothly. Is there any other method I can use to shoot projectiles from the player (in a top-down 2D shooter) towards the mouse?