• Content count

  • Joined

  • Last visited

Community Reputation

14 Neutral

About mypel16000

  • Rank
  1. Attempt at A star algorithm not working

    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
  2. Attempt at A star algorithm not working

    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); } } }
  3. Attempt at A star algorithm not working

    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
  4. 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:   5,5 6,3 6,5 7,3 7,4 7,5 38,28 38,29   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; openList.push_back(start); 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 ) { break; } //Change from open to closed list openList.remove(current); current->opened = false; closedList.push_back(current); 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) { continue; } // 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) { continue; } // 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) { continue; } // 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) { continue; } } // 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 child->setParentNode(current); child->computeScores(endNode); } } else { // Add it to the openList with current point as parent openList.push_back(child); child->opened = true; // Compute it's g, h and f score child->setParentNode(current); child->computeScores(endNode); } } } n++; } //Reset 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) { path.push_back(current->getPosition()); current = current->getParentNode(); n ++; } return path; } Any ideas? Thank you  
  5. Gravity Practice 2D

    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?
  6. Gravity Practice 2D

    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  
  7. Screen Particles Help

    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
  8. Advanced C++

    @BaneTrapper Yes, that's the advice everyone used to give me, so I started developing a game, and, you are right. After developing something simple, I was able to add a lot of features to my game engine! However, I believe that A* pathfinding algorithm is somewhat too complex to just think it through. I've been trying to implement someone else's, but it uses queues, maps, nodes and thinks I have no idea what they mean. I'm trying to read Bjarne Stroustrup¡s C++ book, but it's too long and mixes the stuff i know with what i dont...
  9. Advanced C++

    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...?
  10. Shooting in SFML

    OH my GOD!! Thank you Arhim, if you look at my code, the plan was to write    bulletY += ((move_YOffset*0.1)) ;   y no   bulletY += ((shot_YOffset*0.1)) ;   Just a silly coding mistake... THANKS EVERYONE!
  11. Shooting in SFML

    Here's my code:   void Projectile::shot_CheckDirection(sf::RenderWindow& window, Player& player, int mouseX, int mouseY) { bulletX = (int)player.playerSprite.GetPosition().x ; bulletY = (int)player.playerSprite.GetPosition().y ; shot_XOffset = mouseX -( bulletX ); shot_YOffset = mouseY -( bulletY ); shot_RotationAngle = (atan2(shot_XOffset, shot_YOffset)); bulletSprite.SetRotation( (shot_RotationAngle * 180 / 3.14159265) ); shot_Length= sqrt(shot_XOffset*shot_XOffset + shot_YOffset*shot_YOffset); } void Projectile::shot_Move (sf::RenderWindow& window) { move_XOffset = (int)shot_XOffset; move_YOffset = (int)shot_YOffset; move_XOffset /= shot_Length; move_YOffset /= shot_Length; bulletX += ((shot_XOffset*0.1)) ; bulletY += ((shot_YOffset*0.1)) ; bulletSprite.SetPosition((int)bulletX, (int)bulletY); window.Draw(bulletSprite); frameCounter++; }
  12. Shooting in SFML

    No, it was different, I didn't know how to use this method. They are floats, but that's not the point. The point is that the closer you get the mouse to the player (as the distance gets smaller), the slower the projectiles go. But when the distance is larger, the projectiles go so fast, you only see like 3 images in the screen of the bullet sprite because its so quick!
  13. Shooting in SFML

    I have used this method to implement Projectile movement into my shooting game:   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.   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?
  14. Adding a storyline

    @andy474: I think you are forgetting that this forum is the "for begginers forum". Yes it is hard to interpret such a poorly elaborated answer.   @Bcullis: I know, I'ts just that I'm making the framework of my game still as my graphic designer hasn't caught up to me. I can't carry on the basics and I don't have a map yet. I'll try some methods, but I was just asking if there was a good accepted method to do this. Thanks for the advice though :)   @haegarr: Very good explanation, it kind of gives me an idea of what I need to implement into my game. I ill start having a class "World" with all these flags and simply have another class for the storyline that takes in a reference to the world and has a series of if statements an an int to keep track of the mission the player's in. Kind of like:   - IF( mission == 4 && 6700 < playerX > 6650 && 8700 < playerY > 8650 && playerAbilityConversation > 50) bla bla bla You think thats OK?
  15. Adding a storyline

    Yeah well... how do I do that?