Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - yovano_c

Pages: [1] 2 3
1
General / Pathfinding Problem
« on: October 21, 2016, 06:44:36 pm »
Hello,

I've been working on that issue since a lot of time... and I think today that I'm close to the solution, but I need your help.

I explain:

I just want to my player follow the path that my pathfinding system just found when he clicks on a tile, on an isometric map. But I want to use the speed attribute of my player class, which is represent the time to get through one tile... from middle of a tile to middle of the other, I have getPositionCenter() method from my tile class to get the middle of this. But, the center of the neighbors tiles are not at the same distance because of the isometric system.

I don't really know how to implements this in my code, and don't know exactly how to do it so...

PS: I also have like you can see on screenshots below I kept 2 variables to keep track of the path following if it can help: getCurrentTile() and getDestinationTile(), with setters too. I have getSpeed() of course for the player, and getPositionCenter() for Tile and Player class.


I send each frame the mClock.restart().asSeconds() and the path to my update() function to my Player class like that:

When a left click is done:

sf::Vector2i localPosition = sf::Mouse::getPosition(mWindow);
sf::Vector2f worldPos = mWindow.mapPixelToCoords(localPosition);

mTileClicked = mMap.getTileWithClick(worldPos);
if (mTileClicked != nullptr)
{
    std::cout << "ID: " << mTileClicked->getId() << " - isWalkable = " << mTileClicked->isWalkable;
    std::cout << " - Position: (" << mTileClicked->getPositionOnMap().x << "," << mTileClicked->getPositionOnMap().y << ")" << std::endl;

    if (mTileClicked->isWalkable && mTileClicked != mPlayer.getCurrentTile() && mTileClicked != mPlayer.getDestinationTile())
    {
        mPlayer.setDestinationTile(mTileClicked);
        mPathfinder.findPath(mPlayer.getCurrentTile(), mTileClicked);
    }
}


then on update part, before drawing:

mElapsed = mClockUpdate.restart();
mPlayer.update(mElapsed.asSeconds(), &mPathfinder.path);


My player update function:

void Player::update(float dt, std::list<MapTile*> *path)
{

    if (!path->empty())
    {
        const float unitsPerTick = this->getSpeed() * dt; // Speed in Units per tick

        std::cout << "Units per tick: " << unitsPerTick << std::endl;

        auto waypoint = path->front();

        std::cout << "Player Position: X:" << this->getPositionCenter().x << ", Y:" << this->getPositionCenter().y << std::endl;
        std::cout << "Tile Position: X:" << waypoint->getPositionCenter().x << ", Y:" << waypoint->getPositionCenter().y << std::endl;


        //sf::Vector2f movementVector = sf::Vector2f(waypoint->getPositionCenter().x - this->getPositionCenter().x,
        //waypoint->getPositionCenter().y - this->getPositionCenter().y);

        auto movementVector = waypoint->getPositionCenter() - this->getPositionCenter();

        std::cout << "Movement Vector 1: X:" << movementVector.x << ", Y:" << movementVector.y << std::endl;

        float moveVecLength = sqrt((movementVector.x * movementVector.x) + (movementVector.y * movementVector.y));

        std::cout << "Movement Vector Length: " << moveVecLength << std::endl;

        if (moveVecLength < unitsPerTick)
        {
            this->move(movementVector.x, movementVector.y);
            this->setCurrentTile(waypoint);
            std::cout << waypoint->getId() << " -> ";
            path->pop_front();
        }
        else {
            movementVector.x = movementVector.x / moveVecLength;
            movementVector.y = movementVector.y / moveVecLength;

            movementVector = sf::Vector2f(movementVector.x * unitsPerTick, movementVector.y * unitsPerTick);

            std::cout << "Movement Vector 2: X:" << movementVector.x << ", Y:" << movementVector.y << std::endl;

            this->move(movementVector.x, movementVector.y);
        }
    } else {
        counterAnimation = 0;
    }

    switch (mDirection) {
        case Up: setRect(0); break;
        case Down: setRect(0); break;
        case Left: setRect(0); break;
        case Right: setRect(0); break;
        case DownLeft: setRect(0); break;
        case UpLeft: setRect(0); break;
        case UpRight: setRect(0); break;
        case DownRight: setRect(0); break;
        default: break;
    }

    if (rand() % 4 == 1)
        this->counterAnimation++;
    if (this->counterAnimation >= (this->nbAnimation - 1))
        this->counterAnimation = 0;
}

And I obtain that to the console with my std::cout, and my player move really slowly (0.0X each time), but the
std::cout << waypoint->getId() << " -> ";
appear on the console so it's good for that, and if I move of only one tile, it's stopped when the player reach the center, but it's very slow, thanks....


ID: 426 - isWalkable = 1 - Position: (6,30)
Path found in 78 µs
Units per tick: 0.033536
Player Position: X:1088.03, Y:735.983
Tile Position: X:1088, Y:736
Movement Vector 1: X:-0.03479, Y:0.017395
Movement Vector Length: 0.0388964
Movement Vector 2: X:-0.0299955, Y:0.0149978
Units per tick: 0.035656
Player Position: X:1088, Y:735.998
Tile Position: X:1088, Y:736
Movement Vector 1: X:-0.00476074, Y:0.00238037
Movement Vector Length: 0.00532267
316 -> Units per tick: 0.035542
Player Position: X:1088, Y:736
Tile Position: X:1024, Y:768
Movement Vector 1: X:-64, Y:32
Movement Vector Length: 71.5542
Movement Vector 2: X:-0.0317897, Y:0.0158949
Units per tick: 0.035124
Player Position: X:1087.97, Y:736.016
Tile Position: X:1024, Y:768
Movement Vector 1: X:-63.9683, Y:31.9841
Movement Vector Length: 71.5187
Movement Vector 2: X:-0.0314159, Y:0.0157079

Thanks

2
Graphics / Re: ConvexShape Problem
« on: October 16, 2016, 11:36:43 pm »
It's working nicely now, thanks to you, I was dumb to forget that x)

3
Graphics / Re: ConvexShape Problem
« on: October 16, 2016, 11:23:26 pm »
I have an IsoTile class like that :

#ifndef IsoTile_hpp
#define IsoTile_hpp

#include "Entity.hpp"

class IsoTile: public Entity
{
private:
    sf::VertexArray mLine;
    float mThickness = 0;
    sf::Vector2f mPositionInWindow;
    sf::Vector2f mScale;
   
public:
    IsoTile(int tileWidth, int tileHeight, int thickness);
   
    const sf::Vector2f getPositionInWindow() { return mPositionInWindow; };
    const sf::Vector2f getTileScale() { return mScale; };
   
    void setPositionInWindow(float x, float y);
    void setPositionInWindow(sf::Vector2f &position);
   
    void setTexturePosition(int x, int y);
   
    void setDimension(int width, int height, int thickness);
    void setDimension(sf::Vector2i dim, int thickness);
   
    float getThickness() { return mThickness; };
};

#endif /* IsoTile_hpp */
 

And the constructor :

IsoTile::IsoTile(int tileWidth, int tileHeight, int thickness) : Entity(tileWidth, tileHeight)
{
    setDimension(tileWidth, tileHeight, thickness);
    getConvex().setFillColor(sf::Color::White);
    mScale.x = 1;
    mScale.y = 1;
}

The setFillColor here doesn't working...

4
Graphics / ConvexShape Problem
« on: October 16, 2016, 10:48:38 pm »
Hello,

I have an Entity class like that :

#ifndef Entity_hpp
#define Entity_hpp

#include <vector>
#include <cmath>
#include <iostream>
#include <SFML/Graphics.hpp>

class Entity
{
public:
    Entity(int width, int height);
   
    void update();
   
    void setDimension(int width, int height);
    void setDimension(sf::Vector2i dim);
    const sf::Vector2i getDimension() { return sf::Vector2i(mSpriteWidth, mSpriteHeight); };
   
    sf::Sprite getSprite() { return mSprite; };
    sf::ConvexShape getConvex() { return mConvex; };
   
    void setPosition(sf::Vector2f position) { mPosition = position; };
    sf::Vector2f getPosition() { return mPosition; };
   
private:
    sf::Sprite mSprite;
    sf::ConvexShape mConvex;
    int mSpriteWidth = 50;
    int mSpriteHeight = 50;
    sf::Vector2f mPosition;
};

#endif /* Entity_hpp */
 

My issue:
If in a class that inherit from my Entity class, I want to get my convex shape (or mSprite) with getConvex() (or getSprite()) it's doesn't showing anything... but if I put these variables from private to protected and I use them directly it's working perfectly, and I don't know why, someone could explain to me?

Thanks

5
Graphics / Re: Isometric Maps Pathfinding
« on: September 04, 2016, 10:32:40 am »
I have another issuees :/

in the main loop, i Done that for the player follow the path found by the pathfinding:

if (tileClicked != nullptr && tileClicked->isWalkable)
            p.followPath(pf.path);
 

but my player move only on the first elements of the path and I don't know why...

PS:
void Player::followPath(std::vector<MapTile*> path)
{
    mapTile = path.front();
   
    if (path.front() != path.back())
    {
        //std::cout << path.front()->getId() << " -> ";
        float px = mapTile->mConvex.getPosition().x;
        float py = mapTile->mConvex.getPosition().y;
        float ex = mTile.mConvex.getPosition().x;
        float ey = mTile.mConvex.getPosition().y;
        float x = (ex - px);
        float y = (ey - py);
        float v = sqrtf((x * x) + (y * y));
        float unitx = (x / v);
        float unity = (y / v);
        float movex = -mMovementSpeed * unitx;
        float movey = -mMovementSpeed / 2 * unity;
       
        if (mTile.getPositionInWindow() != mapTile->getPositionInWindow())
        {
            isMoving = true;
           
            if (movex < 0)
            {
                if (movey < 0)
                    move(UpLeft);
                else if (movey > 0)
                    move(DownLeft);
                else if (movey == 0)
                    move(Left);
            }
            else if (movex > 0)
            {
                if (movey < 0)
                    move(UpRight);
                else if (movey > 0)
                    move(DownRight);
                else if (movey == 0)
                    move(Right);
            }
            else if (movex == 0)
            {
                if (movey < 0)
                    move(Up);
                else if (movey > 0)
                    move(Down);
            }
        }
        else
        {
            //std::cout << mapTile->getId() << std::endl;
            path.erase(path.begin());
            //std::cout << path.front()->getId() << std::endl;
        }
    }
    else
    {
        counterAnimation = 0;
        isMoving = false;
    }
   
}

 

6
Graphics / Re: Isometric Maps Pathfinding
« on: September 04, 2016, 01:36:08 am »
Oh dude you are really really really great!!! I just change the offsets array like this but now it's working perfectly! :

const sf::Vector2i evenOffset[] = {
        {-1, 0},
        {-1, -1}, {-1, 1},
        {0, -2}, {0, 2},
        {0, -1}, {0, 1},
        {1, 0}
    };
   
    const sf::Vector2i oddOffset[] = {
        {-1, 0},
        {0, -1}, {0, 1},
        {0, -2}, {0, 2},
        {1, -1}, {1, 1},
        {1, 0}
    };
 

7
Graphics / Re: Isometric Maps Pathfinding
« on: September 04, 2016, 01:00:41 am »
Thanks to you but no, look at the map, for even or odd row it's different.. sometimes you have to start a x = 0 or x = -1 and after i'm loosing myself x')

8
Graphics / Isometric Maps Pathfinding
« on: September 04, 2016, 12:30:22 am »
Hello, I have to found the neighbours tiles of my current tiles for implementing A* pathfinding system to my game.

I use this for now :
for (int x = 1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if ((x == 0 && y == 0)
                    || (x == 1 && y == 0)
                    || (x == -1 && y == 0))
                    continue;
               
                int checkX = mapTile->getPositionOnMap().x + x;
                int checkY = mapTile->getPositionOnMap().y + y;
               
                if (checkX >= 0 && checkX < mWidth && checkY >= 0 && checkY < mHeight)
                {
                    neighbours.push_back(&mMap[checkY][checkX]);
                }
            }
        }
    }

 

But isn't good... i want to put the right 8 tiles around on the list but i can"t figure it out...

Screen of ID's & Positions on the map for help, thanks


9
Graphics / Re: Dofus Maps System
« on: September 01, 2016, 08:49:39 am »
I found the solution, and it's working perfectly, I created the isometric map engine with thickness of the tile too :) Go see it on my github if you want to see

10
Graphics / Re: Dofus Maps System
« on: August 31, 2016, 06:32:26 pm »
I search but I ask if someone can join me in my project

11
Graphics / Re: Dofus Maps System
« on: August 31, 2016, 05:27:17 pm »
Download and test on my github repo, it's working but there is a little bug I don"t know why..

12
Graphics / Re: Dofus Maps System
« on: August 30, 2016, 08:11:51 pm »
I prefer my solution and it's works perfectly without scales the tiles, please take a look at my repo for testing :/

13
Graphics / Re: Dofus Maps System
« on: August 30, 2016, 10:49:40 am »
When I remove the scales factors, my solutions works perfectly, even if I think there is really easier way to do that but please someone can look at my github repo https://github.com/chrisyoyo/DofusMap and try to help me, really thanks to you

14
Graphics / Re: Dofus Maps System
« on: August 29, 2016, 11:27:38 pm »
Please look what I've done in my github, i cannot understand what you try tell me :/

15
Graphics / Re: Dofus Maps System
« on: August 29, 2016, 05:28:47 pm »
PS: I found a solution, but it works only if we clicks on the left corner of the convex shape, If you want to see what I've done, i created a repo :) -> https://github.com/chrisyoyo/DofusMap

Pages: [1] 2 3