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

Author Topic: How to make a grid?  (Read 5561 times)

0 Members and 1 Guest are viewing this topic.

phil652

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
How to make a grid?
« on: August 01, 2013, 11:15:54 pm »
I'm trying to make a grid for a game which will only be dots like this image without all the lines. I want to make it so its the same distance between every dots.
http://cdn2.sbnation.com/uploads/chorus_image/image/13111257/eve_online_hacking_game.0_cinema_640.0.jpg

I am using SFML 2.1

Even just an idea would help.
Thank you
« Last Edit: August 01, 2013, 11:28:21 pm by phil652 »

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: How to make a grid?
« Reply #1 on: August 02, 2013, 01:01:47 am »
Something like this?

const int nofRows;
const int nofCols;
const int distance; //distance between dots
const float offset = distance/2.f;
const float height = std::sqrt(std::pow(distance,2) - std::pow(offset,2));

for (int i=0; i<nofRows; ++i){
        for (int j=0; j<nofCols; ++j){
                if (i%2==0) dot.setPosition(j*distance, i*height);
                else dot.setPosition(j*distance+offset, i*height);
        }
}

Odd rows get offset by half the distance. Gap between rows is the height of the equilateral triangles.
« Last Edit: August 02, 2013, 07:47:56 pm by Kojay »

phil652

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: How to make a grid?
« Reply #2 on: August 02, 2013, 06:08:32 pm »
Okay, thank you for your reply, what is dot?
Do I need to set it as an image or a circle shape?

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: How to make a grid?
« Reply #3 on: August 02, 2013, 06:47:36 pm »
That was a mere suggestion in kind of pseudo-code.

dot is whatever you want it to be. It can be a CirleShape, a Sprite, a class you write yourself with a setPosition() method. You can draw the same one multiple times; or you can have multiple and put them in a container - in which case
dot[i][j].setPosition()
is more like what you 'd have in your code.

phil652

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: How to make a grid?
« Reply #4 on: August 02, 2013, 07:10:24 pm »
Now I am able to get a dot but I can't figured out how to duplicate it.
Is there anywhere I could find a tutorial about this?

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: How to make a grid?
« Reply #5 on: August 02, 2013, 07:47:18 pm »
Have you checked the SFML Graphic tutorials to get familiar with how everything works?

Here's the above code in a basic working example

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(300, 150), "Dots");
    sf::CircleShape dot(5.f);
    dot.setFillColor(sf::Color::Yellow);

        const int nofRows = 5; //number of rows
        const int nofCols = 10; //number of columns
        const int distance = 30; //distance between dots
        const float offset = distance/2.f; //offset for odd rows
        const float height = std::sqrt(std::pow(distance,2.f) - std::pow(offset,2.f)); //height of triangles

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();
                for (int i=0; i<nofRows; ++i){
                        for (int j=0; j<nofCols; ++j){
                                if (i%2==0) dot.setPosition(j*distance, i*height); //even rows
                                else dot.setPosition(j*distance+offset, i*height); //odd rows
                                window.draw(dot);
                        }
                }
                window.display();
        }

        return 0;
}
 

(had forgotten to take square root for correct height)
Good luck.

phil652

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: How to make a grid?
« Reply #6 on: August 02, 2013, 09:14:54 pm »
Thank you very much this is exactly what I was looking for  :)
If someone else read this and wondering why the code does not work. He just forgot #include <cmath> :p

Thanks again!