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

Author Topic: Dofus Maps System  (Read 13917 times)

0 Members and 1 Guest are viewing this topic.

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Dofus Maps System
« on: August 27, 2016, 11:23:38 pm »
Hello,
I want to make a maps system like dofus, I explain:



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




yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #1 on: August 28, 2016, 01:28:48 am »
I found it, sorry.


yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #2 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

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #3 on: August 29, 2016, 11:10:08 am »
Please someone? :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Dofus Maps System
« Reply #4 on: August 29, 2016, 12:45:08 pm »
Wasn't it already answered in your previous thread (Isometric Maps Questions)?
Laurent Gomila - SFML developer

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #5 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Dofus Maps System
« Reply #6 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.
« Last Edit: August 29, 2016, 02:37:30 pm by Laurent »
Laurent Gomila - SFML developer

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #7 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:


- 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();
};
« Last Edit: August 29, 2016, 05:08:44 pm by yovano_c »

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #8 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
« Last Edit: August 29, 2016, 05:34:29 pm by yovano_c »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Dofus Maps System
« Reply #9 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
Laurent Gomila - SFML developer

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #10 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 :/

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #11 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

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #12 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

yovano_c

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #13 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 :/

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Re: Dofus Maps System
« Reply #14 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?