SFML community forums

Help => Graphics => Topic started by: yovano_c on August 27, 2016, 11:23:38 pm

Title: Dofus Maps System
Post by: yovano_c on August 27, 2016, 11:23:38 pm
Hello,
I want to make a maps system like dofus, I explain:

(http://image.noelshack.com/fichiers/2016/34/1472332741-screen-shot-2016-08-27-at-11-18-32-pm.png)

560 tiles, 14 * 40 tiles per map, but tiles are not rotated, just put on two staggered rows..
I don't know how to explain well but I used to do like this :

int save = mSpriteWidth / 2;
sf::Vector2f vec(0, 0);
vec.x = (x * save) - (y * save);
vec.y = ((y * save) + (x * save)) / 2;
mPosition.x = x;
mPosition.y = y;
convex.setPosition(vec);
 

its my code to put a isometric tile on my map, but it gives me this for a 14 by 40 map, thanks for your help

(http://image.noelshack.com/fichiers/2016/34/1472332997-screen-shot-2016-08-27-at-11-23-04-pm.png)

Title: Re: Dofus Maps System
Post by: yovano_c on August 28, 2016, 01:28:48 am
I found it, sorry.

(http://image.noelshack.com/fichiers/2016/34/1472340506-screen-shot-2016-08-28-at-1-28-04-am.png)
Title: Re: Dofus Maps System
Post by: yovano_c on August 28, 2016, 11:32:38 am
Sorry, but i didn"t know how to get the tile when the user click on it... i try to do the getGlobalBounds().contains(mousePoint) but getGlobalBounds return a rect and I want to say my convex shape real bounds... thanks to you
Title: Re: Dofus Maps System
Post by: yovano_c on August 29, 2016, 11:10:08 am
Please someone? :/
Title: Re: Dofus Maps System
Post by: Laurent on August 29, 2016, 12:45:08 pm
Wasn't it already answered in your previous thread (Isometric Maps Questions)?
Title: Re: Dofus Maps System
Post by: yovano_c on August 29, 2016, 02:30:47 pm
Nope i have another question, i want to do a function like this:

IsoTile* getTileWithClick(sf::Vector2f mousePosition);

but i try to do the getGlobalBounds().contains(mousePosition) but getGlobalBounds return a rect and I want to say my convex shape real bounds... thanks to you
Title: Re: Dofus Maps System
Post by: Laurent on August 29, 2016, 02:35:26 pm
So yes, it was already answered but without the details.

Since you have a function that maps a (X, Y) tile index to a (x, y) tile position in real world, it should be possible to find the inverse mapping (world position to tile index). Apply this inverse formula to your mouse position, and you get the clicked tile, without actually computing the real bounding shape of your tiles.

As long as you work with a regular grid, it's more efficient to work with indices rather than bounding shapes. It gives you the direct result with a single formula, instead of testing all your tiles one by one.
Title: Re: Dofus Maps System
Post by: yovano_c on August 29, 2016, 04:55:11 pm
No I think you don't understand me, or i don't understand you :'/

I Have already done:
(http://image.noelshack.com/fichiers/2016/35/1472482202-screen-shot-2016-08-29-at-4-49-46-pm.png)

- Gray tiles are not walkable tiles
- white tiles are walkable tiles
- blue tile is the player tile

I want to move the player with pathfinding to the correct tile when mouse clicked into one tile.
I implement the tile system position on map -> real world with this function :

void IsoTile::setPositionOnMap(int x, int y)
{
    mPositionInMap.x = x;
    mPositionInMap.y = y;
   
    if (y % 2 == 1)
    {
        mPositionInWindow.x = x * mSpriteWidth + mSpriteWidth / 2;
        mPositionInWindow.y = y * mSpriteHeight / 2;
    }
    else
    {
        mPositionInWindow.x = x * mSpriteWidth;
        mPositionInWindow.y = y * mSpriteHeight / 2;
    }
    mPositionInWindow.x *=  mScale.x;
    mPositionInWindow.y *=  mScale.y;
    mSprite.setScale(mScale);
    mConvex.setScale(mScale);
    mSprite.setPosition(mPositionInWindow);
    mConvex.setPosition(mPositionInWindow);
}
 

PS: mScale is a vector2f that includes the width/height of the tiles and width/height of the window.

So, if I works like you tell me with coordinates it won"t work because there is 4 transparent edge include in each IsoTile.

and for the convexshape at the isotile initialization:
mConvex.setOutlineColor(sf::Color::Red);
    mConvex.setOutlineThickness(0.8);
    mConvex.setPointCount(4);
    mConvex.setPoint(0, sf::Vector2f(0, mSpriteHeight / 2));
    mConvex.setPoint(1, sf::Vector2f(mSpriteWidth / 2, 0));
    mConvex.setPoint(2, sf::Vector2f(mSpriteWidth, mSpriteHeight / 2));
    mConvex.setPoint(3, sf::Vector2f(mSpriteWidth / 2, mSpriteHeight));
 

IsoTile.hpp
#include "Entity.hpp"

class IsoTile: public Entity
{
private:
    sf::Vector2i mPositionInMap;
    sf::Vector2f mPositionInWindow;
    sf::Vector2f mScale;
public:
    bool isWalkable = true;
    int mId = -1;
   
    IsoTile();
    IsoTile(int tileWidth, int tileHeight);
   
    const sf::Vector2f getPositionInWindow() { return mPositionInWindow; };
    const sf::Vector2f getTileScale() { return mScale; };
    const sf::Vector2i getPositionOnMap() { return mPositionInMap; }
   
    void setPositionOnMap(int x, int y);
    void setPositionOnMap(sf::Vector2i &position);
   
    void setId(int id);
    int getId() { return mId; };
   
    void setTexturePosition(int x, int y);
   
    void setDimension(int width, int height);
    void setDimension(sf::Vector2i dim);
   
    static int getIdWithPosition(int x, int y);
};
 

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

enum Direction { Up, Down, Left, Right };

class Entity
{
public:
    sf::Sprite mSprite;
    sf::ConvexShape mConvex;
   
    int mSpriteWidth = 50;
    int mSpriteHeight = 50;
    Direction mDirection;
   
    Entity();
    Entity(int width, int height);
    void setDimension(int width, int height);
    void setDimension(sf::Vector2i dim);
    void update();
};
Title: Re: Dofus Maps System
Post by: yovano_c 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
Title: Re: Dofus Maps System
Post by: Laurent on August 29, 2016, 06:20:54 pm
Quote
No I think you don't understand me, or i don't understand you
I think it's you. But I don't know how to explain it better, so hopefully others can do it:
https://www.google.com/#q=mouse%20coordinate%20to%20iso%20tile%20index&rct=j
Title: Re: Dofus Maps System
Post by: yovano_c 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 :/
Title: Re: Dofus Maps System
Post by: yovano_c 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
Title: Re: Dofus Maps System
Post by: Jonny on August 30, 2016, 02:33:38 pm
Why not just split the convex shape into two triangles then check if the mouse position is inside them? There are definitely resources on the internet with advice on how to check if a point is within a triangle, as I've used them before ;) (hint: barycentric)

Hope this helps
Title: Re: Dofus Maps System
Post by: yovano_c 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 :/
Title: Re: Dofus Maps System
Post by: Jonny on August 31, 2016, 04:15:19 pm
I prefer my solution and it's works perfectly without scales the tiles, please take a look at my repo for testing :/

Is the problem solved now then? or is there a new one?
Title: Re: Dofus Maps System
Post by: yovano_c 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..
Title: AW: Dofus Maps System
Post by: eXpl0it3r on August 31, 2016, 05:49:06 pm
If you don't make the effort to try and narrow down the problem, don't expect people to help you. As a programmer you should learn how to debug your code on your own. ;)
Title: Re: Dofus Maps System
Post by: yovano_c on August 31, 2016, 06:32:26 pm
I search but I ask if someone can join me in my project
Title: Re: Dofus Maps System
Post by: DarkRoku12 on September 01, 2016, 06:41:32 am
i played Dofus several years...

But the reallity is, if you can't found a clear solution and no ones want to research for you.

My recommendation is:

You must pick an engine that use any script languagues.

Use the tool and the script facilities to find a solution insted of mess up with c++. And even ask on the engine forum.

When you catch up the solution and the necessary formulas then implement those thing in c++ with sfml.
Title: Re: Dofus Maps System
Post by: yovano_c 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
Title: Re: Dofus Maps System
Post by: chrisadams on May 04, 2021, 08:35:14 am
sorry to posting on old thread but I really need help with this map thing. I am currently working on making a simple replica of Dofus map on this website https://dofusmaps.com. I am able to get the one layer (Continent Amakanien) of the map work perfectly but the layers of other 5 divine dimensions fail to load with the error
Quote
Cross-Origin Request Blocked: The Same Origin Policy disallows
reading the remote resource

I tried adding the following line too

Quote
Access-Control-Allow-Origin: http://localhost:3000

still throws the same error. can anyone here guide me with this? I need to complete this one urgently.