2D Diamond (isometric) map editor - Textures extended infinitely? - c++
I'm currently developing a 2D isometric map editor.
I display entity(cube, player) which contains points and textures.
Each cubes are composed by 12 points.(12 points, but handled as 3 sides of 4 points when displayed by sfml(sf::VertexArray).
(I know I include some '.cpp' times to times, I have a problem with my IDE(visual studio) which I'm trying to resolve, please do not care about it.)
main.cpp
#pragma once
#include "globalfunctions.h" //global functions + main headers + class headers
int main() {
int mapSize = 0;
int cubeSize = 0;
cout << "Map size: "; cin >> mapSize; cout << endl;
cout << "Cube size: "; cin >> cubeSize; cout << endl;
int windowWidth = (mapSize * cubeSize) - (cubeSize * 2);
int windowHeight = ((mapSize * cubeSize) - (cubeSize * 2)) / 2;
renderWindow window(windowWidth, windowHeight, mapSize, cubeSize);
int nbMaxTextures = 9;
for (int t = 0; t < nbMaxTextures; t++) {
window.loadTexture("test", t);
}
window.run();
return EXIT_SUCCESS;
}
globalfunctions.h
#pragma once
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <math.h>
//#include <sstream>
#include <vector>
using namespace std;
sf::Vector2u isometricToCartesian(int i, int j, int cubeSize) {
sf::Vector2u carth;
carth.x = (j - i) * (cubeSize / 2);
carth.y = (j + i) * (cubeSize / 4);
return carth;
}
sf::Vector2u cartesianToIsometric(int x, int y, int cubeSize) {//TODO
sf::Vector2u iso;
iso.x = 0;
iso.y = 0;
return iso;
}
#include "entity.h"
#include "renderWindow.h"
renderWindow.h
#pragma once
class renderWindow {
public:
renderWindow(float WIDTH, float HEIGHT, int MAPSIZE, int CUBESIZE);
void run();
void loadTexture(sf::String folder, int numTexture);
//SETTERS
//...
//GETTERS
//...
private:
int mCurrentLayerID;
int mMapSize;
int mCubeSize;
int mSelectedTexture;
vector<entity> mMap;
sf::RenderWindow mWindow;
vector<sf::Texture> mTextures;
sf::Texture mMemoryTexture;
void processEvent();
void update(sf::Time deltaTime);
void render();
//CUBE ACTION-------------------------------------------
void addCube(int layerID, float x, float y);
entity& getCube(int ID);
entity& getCubeAt(float x, float y);
vector<sf::VertexArray> loadCube(int cubeID);//UPDATE DATA LIKE COORDINATES -> create/chnge the vertex
void drawCube(int cubeID);//draw the vertex
//VARIABLES
vector<sf::VertexArray> verticesSide1;
vector<sf::VertexArray> verticesSide2;
vector<sf::VertexArray> verticesSide3;
//CUBE ACTION-------------------------------------------
};
#include "renderWindow.cpp"
renderWindow.cpp
#pragma once
renderWindow::renderWindow(float WIDTH, float HEIGHT, int MAPSIZE, int CUBESIZE) : mWindow(sf::VideoMode(WIDTH, HEIGHT), "") {
mMapSize = MAPSIZE;
mCubeSize = CUBESIZE;
mSelectedTexture = 6;
mCurrentLayerID = -1;
int x = 0;
int y = 0;
//default layer
for (int j = 0; j < mMapSize; j++) {
for (int i = 0; i < mMapSize; i++) {
x = isometricToCartesian(i, j, mCubeSize).x;
y = isometricToCartesian(i, j, mCubeSize).y;
addCube(0, x, y);
}
}
for (int c = 0; c < mMap.size(); c++) {
verticesSide1.push_back(loadCube(c)[0]);
verticesSide2.push_back(loadCube(c)[1]);
verticesSide3.push_back(loadCube(c)[2]);
//then only do that when something the cube's coordinate changed
}
}
void renderWindow::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
while (mWindow.isOpen()) {
processEvent();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
processEvent();
update(TimePerFrame);
}
render();
}
}
void renderWindow::loadTexture(sf::String folder, int numTexture) {
if (mMemoryTexture.loadFromFile("textures/" + folder + "/" + to_string(numTexture) + ".jpg"))
mTextures.push_back(mMemoryTexture);
else
cout << "Texture n°" << numTexture << " as failed to load." << endl;
}
//SETTERS
//...
//GETTERS
//...
//PRIVATE METHODE
void renderWindow::processEvent() {
sf::Event event;
while (mWindow.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
mWindow.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
mWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.MouseButtonPressed == sf::Mouse::Left)
getCubeAt(event.mouseButton.x, event.mouseButton.y).setTexture(0, mSelectedTexture);//TEST
getCubeAt(event.mouseButton.x, event.mouseButton.y).setTexture(1, mSelectedTexture + 1);//TEST
getCubeAt(event.mouseButton.x, event.mouseButton.y).setTexture(2, mSelectedTexture + 2);//TEST
break;
/*case sf::Event::MouseMoved:
cout << "(" << event.mouseMove.x << ", " << event.mouseMove.y << ")" << endl;
break;*/
}
}
}
void renderWindow::update(sf::Time deltaTime) {
//REMEMBER: distance = speed * time
//MOVEMENT, ANIMATIONS ETC. ...
}
void renderWindow::render() {
mWindow.clear();
for (int c = 0; c < mMap.size(); c++) {
drawCube(c);
}
mWindow.display();
}
//CUBE ACTION-------------------------------------------
void renderWindow::addCube(int layerID, float x, float y) {
//Thoses make the code more readable:
int half_cubeSize = mCubeSize / 2;
int oneQuarter_cubeSize = mCubeSize / 4;
int twoQuarter_cubeSize = oneQuarter_cubeSize * 2;
int treeQuarter_cubeSize = oneQuarter_cubeSize * 3;
mCurrentLayerID = layerID;
entity dummy(mMap.size(), 0, layerID);
dummy.addPoint(12);
dummy.addTexture(6);
dummy.addTexture(7);
dummy.addTexture(8);
//SIDE 1------------------------------------------------
dummy.setPoint(0, x, y + oneQuarter_cubeSize);
dummy.setPoint(1, x + half_cubeSize, y + twoQuarter_cubeSize);
dummy.setPoint(2, x + half_cubeSize, y + mCubeSize);
dummy.setPoint(3, x, y + treeQuarter_cubeSize);
//SIDE 2------------------------------------------------
dummy.setPoint(4, x + half_cubeSize, y + twoQuarter_cubeSize);
dummy.setPoint(5, x + mCubeSize, y + oneQuarter_cubeSize);
dummy.setPoint(6, x + mCubeSize, y + treeQuarter_cubeSize);
dummy.setPoint(7, x + half_cubeSize, y + mCubeSize);
//SIDE 3------------------------------------------------
dummy.setPoint(8, x, y + oneQuarter_cubeSize);
dummy.setPoint(9, x + half_cubeSize, y);
dummy.setPoint(10, x + mCubeSize, y + oneQuarter_cubeSize);
dummy.setPoint(11, x + half_cubeSize, y + twoQuarter_cubeSize);
mMap.push_back(dummy);
}
entity& renderWindow::getCube(int ID) {
for (int c = 0; c < mMap.size(); c++) {
if (mMap[c].getID() == ID)
return mMap[c];
}
}
entity& renderWindow::getCubeAt(float x, float y) {//TO DO
return entity(-1, 0, 0);
}
vector<sf::VertexArray> renderWindow::loadCube(int cubeID) {
vector<sf::VertexArray> vertices;
vertices.push_back(sf::VertexArray());
vertices.push_back(sf::VertexArray());
vertices.push_back(sf::VertexArray());
vertices[0].setPrimitiveType(sf::Quads);
vertices[0].resize(4);
vertices[1].setPrimitiveType(sf::Quads);
vertices[1].resize(4);
vertices[2].setPrimitiveType(sf::Quads);
vertices[2].resize(4);
sf::Vector2f tv0 = sf::Vector2f(0, 0);
sf::Vector2f tv1 = sf::Vector2f(mCubeSize, 0);
sf::Vector2f tv2 = sf::Vector2f(mCubeSize, mCubeSize);
sf::Vector2f tv3 = sf::Vector2f(0, mCubeSize);
sf::Vector2f v0 = sf::Vector2f(getCube(cubeID).getPoint(0, 0), getCube(cubeID).getPoint(0, 1));
sf::Vector2f v1 = sf::Vector2f(getCube(cubeID).getPoint(1, 0), getCube(cubeID).getPoint(1, 1));
sf::Vector2f v2 = sf::Vector2f(getCube(cubeID).getPoint(2, 0), getCube(cubeID).getPoint(2, 1));
sf::Vector2f v3 = sf::Vector2f(getCube(cubeID).getPoint(3, 0), getCube(cubeID).getPoint(3, 1));
sf::Vector2f v4 = sf::Vector2f(getCube(cubeID).getPoint(4, 0), getCube(cubeID).getPoint(4, 1));
sf::Vector2f v5 = sf::Vector2f(getCube(cubeID).getPoint(5, 0), getCube(cubeID).getPoint(5, 1));
sf::Vector2f v6 = sf::Vector2f(getCube(cubeID).getPoint(6, 0), getCube(cubeID).getPoint(6, 1));
sf::Vector2f v7 = sf::Vector2f(getCube(cubeID).getPoint(7, 0), getCube(cubeID).getPoint(7, 1));
sf::Vector2f v8 = sf::Vector2f(getCube(cubeID).getPoint(8, 0), getCube(cubeID).getPoint(8, 1));
sf::Vector2f v9 = sf::Vector2f(getCube(cubeID).getPoint(9, 0), getCube(cubeID).getPoint(9, 1));
sf::Vector2f v10 = sf::Vector2f(getCube(cubeID).getPoint(10, 0), getCube(cubeID).getPoint(10, 1));
sf::Vector2f v11 = sf::Vector2f(getCube(cubeID).getPoint(11, 0), getCube(cubeID).getPoint(11, 1));
vertices[0][0] = sf::Vertex(v0, tv0);
vertices[0][1] = sf::Vertex(v1, tv1);
vertices[0][2] = sf::Vertex(v2, tv2);
vertices[0][3] = sf::Vertex(v3, tv3);
vertices[1][0] = sf::Vertex(v4, tv0);
vertices[1][1] = sf::Vertex(v5, tv1);
vertices[1][2] = sf::Vertex(v6, tv2);
vertices[1][3] = sf::Vertex(v7, tv3);
vertices[2][0] = sf::Vertex(v8, tv0);
vertices[2][1] = sf::Vertex(v9, tv1);
vertices[2][2] = sf::Vertex(v10, tv2);
vertices[2][3] = sf::Vertex(v11, tv3);
return vertices;
}
void renderWindow::drawCube(int cubeID) {
mWindow.draw(verticesSide1[cubeID], &mTextures[getCube(cubeID).getTexture(0)]);
mWindow.draw(verticesSide2[cubeID], &mTextures[getCube(cubeID).getTexture(1)]);
mWindow.draw(verticesSide3[cubeID], &mTextures[getCube(cubeID).getTexture(2)]);
}
//CUBE ACTION-------------------------------------------
entity.h
#pragma once
class entity {
public:
entity();
entity(int id, int type, int numlayer);
void addPoint(int nbPoints);
void addTexture(int numTexture);
//SETTERS
void setPoint(int numPoint, float x, float y);
void setTexture(int textureID, int numTexture);
//GETTERS
int getID();
float getPoint(int numPoint, int numIndex);//if numIndex = 0 -> x || if numIndex = 1 -> y
int getType();
int getNumLayer();
int getTexture(int numTexture);
private:
int mID;
int mType;
int mNumLayer;
vector<sf::Vector2u> mPoints;
vector<int> mTextures;
};
#include "entity.cpp"
entity.cpp
#pragma once
entity::entity() {
mID = 0;
mType = -1;
mNumLayer = 0;
}
entity::entity(int id, int type, int numlayer) {
mID = id;
mType = type;
mNumLayer = numlayer;
}
void entity::addPoint(int nbPoints) {
mPoints.clear();
int newSize = 0;
for (int p = 0; p < nbPoints; p++) {
newSize++;
}
mPoints = vector<sf::Vector2u>(newSize);
}
void entity::addTexture(int numTexture) {
mTextures.push_back(numTexture);
}
//SETTERS
void entity::setPoint(int numPoint, float x, float y) {
mPoints[numPoint].x = x;
mPoints[numPoint].y = y;
}
void entity::setTexture(int textureID, int numTexture) {
mTextures[textureID] = numTexture;
}
//GETTERS
int entity::getID() {
return mID;
}
float entity::getPoint(int numPoint, int numIndex) {
if (numIndex == 0)
return mPoints[numPoint].x;
else
return mPoints[numPoint].y;
}
int entity::getType() {
return mType;
}
int entity::getNumLayer() {
return mNumLayer;
}
int entity::getTexture(int numTexture) {
return mTextures[numTexture];
}
I've done a lot of test, too much, so I won't post them right now, but if you have any question, feel free to ask.
Here is the problem described in the title :
And here, screens with only one face displayed(in the same order in the code):
The only thing I don't understand is that a cube displayed alone work perfectly fine if you enter the coordinates manually. Even the extended ones. But the coordinates formula is ok... (I noticed that the cube n°50 for a 15x15 map with 64x64 cube display a rectangle 'infinite' in width)
If the texture is extended(maybe to the infinite), it suggest that the coordinates are continuously increasing somewhere ? Then, why the cubes are still well placed ?
Here are the assets(64*64 png) :
Directories : textures/test/
Not really an answer (as the code will be rewritten anyway) so few hints for the new code instead (some of them are already mentioned in the comments).
Tileset
In the final isometric engine use sprites. They are faster and support pixel art. For my purposes I use compilation of these two free to use tilesets (64x64):
outside tileset
medieval building tileset
Both are compatible. I compiled and edited them to suite the needs of my engine. So this is what I use (still work in progress):
White color 0x00FFFFFF means transparent. The sprite is not enough. I added info about the height of tile and rotations.
If you see first 4 tiles from upper left corner they all are the same thing rotated by 90 degrees. So all my tiles have index of 4 tiles (the 90 degree rotations) int rot[4]. This way I can rotate the whole map or just view. I compile the set so the rotations are next to each other. There are 3 options:
tile[ix].rot[]={ ix,ix,ix,ix }; where ix is tile without rotation (ground)
tile[ix].rot[]={ ix,ix+1,ix,ix+1 }; where ix is tile with 2 rotations (those 2 tiles with chunk of chopped tree in the middle right)
tile[ix].rot[]={ ix,ix+1,ix+2,ix+3 }; where ix is tile with 4 rotations (like the first tile)
The indexes are valid of coarse only for the first tile only, the others have the whole rot[] array rotated by 1 value from neighbor. Some rotations are invisible (see the wide trees) but the tile is still present to allow rotations.
The tile height is important for placing tiles while editing and also for automatic map generations.
I plan to add also A* map for each tile so I can use path finding or compute watter flows and more.
Map editor
I prefer 3D maps. with bigger resolution you need to properly select the viewed area for viewing to maximize performance. Also a good idea is to create hollow underground so the rendering is much faster (this can be also done virtually during rendering process without the need of updating map).
I recommend to code these features:
make ground hollow
make ground solid
random terrain (diamond & square)
filter out small holes and smooth edges (add the slope tiles to cubic ones)
tile editor
Apart from the obvious paint editor you should add also another features like:
floor <-> ceiling
left <-> right
front <-> back
divide large sprite into regular tiles
copy/merge/paste
adjust lighting after left <-> right mirror operation
They are really handy while compiling/editing tileset resources. As you can see my tileset has many of tiles not present in the source tilesets. They were created by these functions + some minor paint editing... The colored masks on the bottom of the tileset are used to mask out and properly combine parts of tiles to create the missing ones ... (you can take one side form one tile and other from other ...)
[Notes]
For more info/ideas have a look at some related Q/As:
Improving performance of click detection on a staggered column isometric grid
How to procedurally generate isometric map
And here my Standalone no install Win32 Demo:
demo v1.000
demo v1.034
In OpenGL, when you are creating a OpenGL texture manually, you can assign 4 types:
GL_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP and GL_CLAMP_TO_BORDER
If you want to learn more from the openGL textures differences, take a look here. Basically, it extend the last pixel in a image to the rest of the reserved memory.
In order to solve your problem, try to load the texture, modifing the parameters. I don't know if sfml allow to do it with Texture.hpp header, in the reference appear setRepeated, try to set true to see if solve the problem. Other way, loadfromfile with a size sf::IntRect(0, 0, 32, 32) in example.
This code is not tested, but teorically, using OpenGL will work:
void renderWindow::loadTexture(sf::String folder, int numTexture)
{
if (mMemoryTexture.loadFromFile("textures/" + folder + "/" + to_string(numTexture) + ".jpg"))
mTextures.push_back(mMemoryTexture);
else
cout << "Texture n°" << numTexture << " as failed to load." << endl;
// Generate OpenGL Texture manually
GLuint texture_handle;
glGenTextures(1, &texture_handle);
// Attach the texture
glBindTexture(GL_TEXTURE_2D, texture_handle);
// Upload to Graphic card
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA,
mMemoryTexture.GetWidth(), mMemoryTexture.GetHeight(),
0,
GL_RGBA, GL_UNSIGNED_BYTE, mMemoryTexture.GetPixelsPtr()
);
// Set the values
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
Maybe this helps you to solve your problem.
I endend by finding better way to do this code, thanks to members of stackoverflow. For people who got there by looking for a solution to a similar problem, I invite you to look at the comments for some usefull links and comment.
Related
Low framerate with only map and minimap drawing (SFML)
I'm working on a small "game" like project as a practice, and I've managed to get my framerate down to not even 3 FPS. While the only thing that is being drawn is screen filling tiles and a minimap. Now I've found that the problem is with the minimap, without it caps at 60 FPS. But unfortunately I'm not experienced enough to find out what the real problem is with it... My draw function: void StateIngame::draw() { m_gui.removeAllWidgets(); m_window.setView(m_view); // Frame counter float ct = m_clock.restart().asSeconds(); float fps = 1.f / ct; m_time = ct; char c[10]; sprintf(c, "%f", fps); std::string fpsStr(c); sf::String str(fpsStr); auto fpsText = tgui::Label::create(); fpsText->setText(str); fpsText->setTextSize(16); fpsText->setPosition(15, 15); m_gui.add(fpsText); ////////////// // Draw map // ////////////// int camOffsetX, camOffsetY; int tileSize = m_map.getTileSize(); Tile tile; sf::IntRect bounds = m_camera.getTileBounds(tileSize); camOffsetX = m_camera.getTileOffset(tileSize).x; camOffsetY = m_camera.getTileOffset(tileSize).y; // Loop and draw each tile // x and y = counters, tileX and tileY is the coordinates of the tile being drawn for (int y = 0, tileY = bounds.top; y < bounds.height; y++, tileY++) { for (int x = 0, tileX = bounds.left; x < bounds.width; x++, tileX++) { try { // Normal view m_window.setView(m_view); tile = m_map.getTile(tileX, tileY); tile.render((x * tileSize) - camOffsetX, (y * tileSize) - camOffsetY, &m_window); } catch (const std::out_of_range& oor) {} } } bounds = sf::IntRect(bounds.left - (bounds.width * 2), bounds.top - (bounds.height * 2), bounds.width * 4, bounds.height * 4); for (int y = 0, tileY = bounds.top; y < bounds.height; y++, tileY++) { for (int x = 0, tileX = bounds.left; x < bounds.width; x++, tileX++) { try { // Mini map m_window.setView(m_minimap); tile = m_map.getTile(tileX, tileY); sf::RectangleShape miniTile(sf::Vector2f(4, 4)); miniTile.setFillColor(tile.m_color); miniTile.setPosition((x * (tileSize / 4)), (y * (tileSize / 4))); m_window.draw(miniTile); } catch (const std::out_of_range& oor) {} } } // Gui m_window.setView(m_view); m_gui.draw(); } The Tile class has a variable of type sf::Color which is set during map generating. This color is then used to draw the minimap instead of the 16x16 texture that is used for the map itself. So when I leave out the minimap drawing, and only draw the map itself, it's more fluid than I could wish for... Any help is appreciated!
You are generating the view completly new for every frame. Do this once on startup should be enought.
Tiles being drawn in the wrong location
I've finally managed to get my tiles drawn on the screen somewhat in a correct way. Although the location is a bit off and I can't seem to figure out why... I'm using SFML for drawing. Tile.hpp: #ifndef TILE_HPP #define TILE_HPP #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include "textureManager.hpp" class Tile { public: Tile(); Tile(sf::Vector2i coord, int biome); ~Tile(); sf::Vector2i getCoord() const { return coord; }; int getBiome() const { return biome; }; void setCoord(sf::Vector2i coord) { this->coord = coord; }; void setBiome(int biome) { this->biome = biome; }; void draw(int x, int y, sf::RenderWindow* rw); void update(sf::Texture& texture); private: sf::Vector2i coord; int biome; sf::Sprite sprite; }; #endif Tile.cpp #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include "textureManager.hpp" #include "tile.hpp" Tile::Tile() {} Tile::Tile(sf::Vector2i coord, int biome) { this->biome = biome; this->coord = coord; } Tile::~Tile(){} void Tile::draw(int x, int y, sf::RenderWindow* rw) { sprite.setPosition(x, y); rw->draw(sprite); } void Tile::update(sf::Texture& texture) { switch (biome) { // Not important here } } Now the more relevant part: the drawing void StatePlay::draw(const float dt) { game->window.setView(view); game->window.clear(sf::Color::Black); sf::Vector2f offset = camera.getLocation(); int newX = (offset.x / map.getTileSize()) - (map.chunkSize / 2); int newY = (offset.y / map.getTileSize()) - (map.chunkSize / 2); for (int x = 0; x < map.chunkSize; x++) { for (int y = 0; y < map.chunkSize; y++) { Tile tile = map.getTile(newX + x, newY + y); tile.draw((newX + x) * map.getTileSize(), (newY + y) * map.getTileSize(), &game->window); } } return; } StatePlay::StatePlay(Game* game) { this->game = game; sf::Vector2f pos = sf::Vector2f(game->window.getSize()); // 1366x768 view.setSize(pos); pos *= 0.5f; // 688x384 view.setCenter(pos); // Initialize map map.init(game->gameTime, game->textureManager.getImage("tileset.png")); float w = (float) map.getWidth(); // 500 float h = (float) map.getHeight(); // 500 w *= 0.5f; // 250 h *= 0.5f; // 250 w *= map.getTileSize(); // 250 * 32 = 8000 h *= map.getTileSize(); // 250 * 32 = 8000 // Move camera // Uses view::move from sfml to move the view with w and h // Also sets camera private to w and h values, return with camera::getLocation() camera.setLocation(&view, sf::Vector2f(w, h)); } The result is that I only see the ~10 tiles squared, in the bottom left corner of my screen, covering about 3/4. The correct tiles are chosen, but the draw location is wrong... It should draw the center of 64x64 (x 32px each) tiles, as much as fit on the screen.
I have fixed the problem. It was a very stupid mistake... At first without drawing anything, it is normal to center the view on 0.5f * sf::View::getSize() to get the view centered in your window. So the center was already at half of my window size. When using Camera::setLocation(), I used the sf::View::move() to move the view accordingly. So when trying to center it on the map, it added the x and y correctly, but also half of my window size. This resulted in having an offset which was incorrect. Substracting or leaving those values out has fixed this stupid problem. Thank you for the help.
C++ SFML collision is not accurate
I'm making a 2D game with SFML in C++ and I have a problem with collision. I have a player and a map made of tiles. Thing that doesn't work is that my collision detection is not accurate. When I move player up and then down towards tiles, it ends up differently. I am aware that source of this problem may be calculating player movement with use of delta time between frames - so it is not constant. But it smooths movement, so I don't know how to do it other way. I tried with constant speed valuses and to make collision fully accurate - speed had to be very low and I am not satisfied with that. void Player::move() { sf::Vector2f offsetVec; if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) offsetVec += sf::Vector2f(0, -10); if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) offsetVec += sf::Vector2f(0, 10); if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) offsetVec += sf::Vector2f(-10, 0); if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) offsetVec += sf::Vector2f(10, 0); this->moveVec += offsetVec; } void Player::update(float dt, Map *map) { sf::Vector2f offset = sf::Vector2f(this->moveVec.x * this->playerSpeed * dt, this->moveVec.y * this->playerSpeed * dt); sf::Sprite futurePos = this->sprite; futurePos.move(offset); if (map->isCollideable(this->pos.x, this->pos.y, futurePos.getGlobalBounds())) { this->moveVec = sf::Vector2f(0, 0); return; } this->sprite.move(offset); this->pos += offset; this->moveVec = sf::Vector2f(0, 0); return; } In player position update I create future sprite object, which is object after applying movement, to get it's boundaries and pass it to collision checker. To collision checker I also pass player pos, because my map is stored in 2d array of tile pointers, so I check only these in player range. bool Map::isCollideable(float x, float y, const sf::FloatRect &playerBounds) { int startX = int(x) / Storage::tileSize; int startY = int(y) / Storage::tileSize; Tile *tile; for (int i = startX - 10; i <= startX + 10; ++i) { for (int j = startY - 10; j <= startY + 10; ++j) { if (i >= 0 && j >= 0) { tile = getTile(i, j); if (tile != nullptr && playerBounds.intersects(tile->getGlobalBounds())) return true; } } } return false; } Full project on Github My solution I have changed if statement in update function to while statement, which decreases my offset vector till no collision is present. I still have to make some adjustments, but general idea is: void Player::update(float dt, Map *map) { int repeats = 0; sf::Vector2f offset = sf::Vector2f(this->moveVec.x * this->playerSpeed * dt, this->moveVec.y * this->playerSpeed * dt); sf::Sprite futurePos = this->sprite; while (map->isCollideable(this->pos.x, this->pos.y, futurePos, offset)) { offset = 0.7f * offset; repeats++; if (repeats > 5) { this->moveVec = sf::Vector2f(0, 0); return; } } this->sprite.move(offset); this->pos += offset; this->moveVec = sf::Vector2f(0, 0); return; } I also had to rework isCollideable method a little, so it accepts sf::Sprite and offset vector so it can calculate boundaries on it's own.
When the player collides with a tile, you should calculate the penetration, that is, the value of "how much the player went into the tile". When you have this value, nudge your player back that much.
This is just a thought but you could have some inaccuracies in your collision detection when you typecast the float x, and y to integers and then divide them. This could cause problems because some of the data in the float could be lost. If the float was 3.5 or 3.3 or 3.9 then it would become 3 which throws off your collision calculations.
2D water shader in SFML
I want to implement the algorithm for a 2D water surface described here and here. But instead of using two int arrays and calculating on the CPU I would like to use SFML's sf::RenderTexture's (FBO's basically) and a GLSL shader to run everything on the GPU. I want to use SFML, because it's so simple and I have worked with it before, so I know my way around it a little. I've made some good progress so far. I was able to set up 3 sf::RenderTextures and ping-pong between them correctly (because other than int array you can't read and write to the same sf::RenderTexture at the same time). I was also able to adapt the algorithm for the height field creation form being in the range -32.767 to 32.767 to the range 0 to 1 (or to be more precise -0.5 to 0.5 for the calculation). Also adding new ripples works to some extend. So up to this point you can actually see a little of waves going on. Here comes my problem now: The waves disappear really, really fast and I don't even apply any damping yet. According to the algorithm the ripples are not stopping if there is no damping applied. It's even the other way around. If I apply "amplification" the waves look close to what you would expect them to look like (but they still disappear without any damping applied to them). My first thought was that this is, because I use float's in range 0 - 1 instead of integers, but I only see this being a problem if multiplication is used, but I only use addition and subtraction. Here is my SFML C++ code : #include <SFML/Graphics.hpp> #include <iostream> int main() { sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML works!"); window.setFramerateLimit(12); sf::RenderTexture buffers[3]; buffers[0].create(500, 500); buffers[1].create(500, 500); buffers[2].create(500, 500); sf::RenderTexture* firstBuffer = buffers; sf::RenderTexture* secondBuffer = &buffers[1]; sf::RenderTexture* finalBuffer = &buffers[2]; firstBuffer->clear(sf::Color(128, 128, 128)); secondBuffer->clear(sf::Color(128, 128, 128)); finalBuffer->clear(sf::Color(128, 128, 128)); sf::Shader waterHeightmapShader; waterHeightmapShader.loadFromFile("waterHeightmapShader.glsl", sf::Shader::Fragment); sf::Sprite spritefirst; spritefirst.setPosition(0, 0); spritefirst.setTexture(firstBuffer->getTexture()); sf::Sprite spritesecond; spritesecond.setPosition(500, 0); spritesecond.setTexture(secondBuffer->getTexture()); sf::Sprite spritefinal; spritefinal.setPosition(0, 500); spritefinal.setTexture(finalBuffer->getTexture()); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if(event.type == sf::Event::Closed) window.close(); if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape) window.close(); } waterHeightmapShader.setParameter("mousePosition", sf::Vector2f(-1.f, -1.f)); // if mouse button is pressed add new ripples if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) { sf::Vector2i mousePosition = sf::Mouse::getPosition(window); if(mousePosition.x < 500 && mousePosition.y < 500) { sf::Vector2f mouse(mousePosition); mouse.x /= 500.f; mouse.y /= 500.f; mouse.y = 1 - mouse.y; std::cout << mouse.x << " " << mouse.y << std::endl; waterHeightmapShader.setParameter("mousePosition", mouse); } } waterHeightmapShader.setParameter("textureTwoFramesAgo", firstBuffer->getTexture()); waterHeightmapShader.setParameter("textureOneFrameAgo", secondBuffer->getTexture()); // create the heightmap secondBuffer->display(); finalBuffer->clear(sf::Color(128, 128, 128)); finalBuffer->draw(sf::Sprite(secondBuffer->getTexture()), &waterHeightmapShader); finalBuffer->display(); spritefirst.setTexture(firstBuffer->getTexture()); spritesecond.setTexture(secondBuffer->getTexture()); spritefinal.setTexture(finalBuffer->getTexture()); window.clear(); window.draw(spritefirst); window.draw(spritesecond); window.draw(spritefinal); window.display(); // swap the buffers around, first becomes second, second becomes third and third becomes first sf::RenderTexture* swapper = firstBuffer; firstBuffer = secondBuffer; secondBuffer = finalBuffer; finalBuffer = swapper; } return 0; } And here is my GLSL shader code : uniform sampler2D textureTwoFramesAgo; uniform sampler2D textureOneFrameAgo; uniform vec2 mousePosition; const float textureSize = 500.0; const float pixelSize = 1.0 / textureSize; void main() { // pixels position vec2 position = gl_TexCoord[0].st; vec4 finalColor = ((texture2D(textureOneFrameAgo, vec2(position.x - pixelSize, position.y)) + texture2D(textureOneFrameAgo, vec2(position.x + pixelSize, position.y)) + texture2D(textureOneFrameAgo, vec2(position.x, position.y + pixelSize)) + texture2D(textureOneFrameAgo, vec2(position.x, position.y - pixelSize)) - 2.0) / 2) - (texture2D(textureTwoFramesAgo, position) - 0.5); // damping // finalColor.rgb *= 1.9; // <---- uncomment this for the "amplifiction" ie. to see the waves better finalColor.rgb += 0.5; // add new ripples if(mousePosition.x > 0.0) { if(distance(position, mousePosition) < pixelSize * 5) { finalColor = vec4(0.9, 0.9, 0.9, 1.0); } } gl_FragColor = finalColor; } Please remember that this is all just about the height field creation. There is no shading of the water yet. Do you know why the waves disappear by them self without damping?
If I am reading the code correctly you sample the previous frame for the texture's colors/height and use four neighboring pixels/texels to determine the color/height of the current pixel. As you are calculating (scaling) these neighbors you might run into missing the texel that contains the color/height you are looking for. It might not be the heighest texel, just one next to it a little bit lower causing the unexpected damping. This is where you do not just use addition and subtraction: const float pixelSize = 1.0 / textureSize; By using this value you could just miss the texel you are looking for. EDIT Also: you are averaging the samples so the result will always be less than the maximum value of the samples. So instead of averaging you could select the maximum value. That might give weird results but also extra insight.
Here are some "Processing" codes which implements the same algorithm you've posted above, and its damping is correct, I hope you can get some points from it : // codes begin int Width = 800; int Height = 600; int FullSize = 0; //int Spacing = 10; int[] source, dest; PImage bg; void setup() { // if you want to run these codes by "Processing" // please make a picture named "HelloWorld.png" bg = loadImage("HelloWorld.png"); Width = bg.width; Height = bg.height; FullSize = Width * Height; size(Width, Height); source = new int[FullSize]; dest = new int[FullSize]; for (int i=0; i< FullSize; i++) source[i] = dest[i] = 0; } void draw() { for (int i=Width; i< FullSize-Width; i++) { // check for bounds int xi = i % Width; if ((xi==0) || (xi==Width-1)) continue; dest[i] = ( ((source[i-1]+ source[i+1]+ source[i-Width]+ source[i+Width]) >>1) ) -dest[i]; int dampFactor = 1000; dest[i] -= (dest[i] >> dampFactor); // Damping - Quick divde by 32 (5 bits) } //image(bg, 0, 0); loadPixels(); for (int i=Width; i< FullSize-Width; i++) { // check for bounds int xi = i % Width; if ((xi==0) || (xi==Width-1)) continue; int xoffset = dest[i-1] - dest[i+1]; int yoffset = dest[i-Width] - dest[i+Width]; int offset = i+xoffset+yoffset*Width; if (offset>0 && offset<FullSize) { // TODO: make better map pixels[i] = bg.pixels[offset]; } } //bg.updatePixels(); updatePixels(); //swap int[] temp = source; source = dest; dest = temp; } void mouseDragged() { if (mouseX > 0 && mouseX < Width && mouseY > 0 && mouseY < Height) source[mouseY*Width+mouseX] = (int)random(50, 100); } void mousePressed() { // TODO: make a area pulse value, like a radius circle if (mouseX > 0 && mouseX < Width && mouseY > 0 && mouseY < Height) source[mouseY*Width+mouseX] = (int)random(50, 100); } // codes end
How do I draw lines using XNA?
I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted. Can someone show me, using code, the simplest XNA implementation of drawing one or two lines on to the screen? Perhaps with a brief explanation (including the boilerplate)? I'm not a games programmer and I have little XNA experience. My ultimate goal is to draw some lines onto the screen which I will eventually transform with rotations, etc (by hand). However, for this first step.. I need to simply draw the lines! I remember back in my ancient OpenGL days it was fairly straightforward when drawing a line with a few method calls. Should I simply revert to using unmanaged directx calls?
When working with XNA, everything (even 2d primitives) have to be expressed in a way that a 3d card can understand, which means that a line is just a set of vertices. MSDN has a pretty good walkthrough here: http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF You'll find that it takes more code to render a primitive line than it would take to just setup a textured quad and rotate that, since in essence, your doing the same thing when rendering a line.
Following NoHayProblema's answer (I cannot comment yet). That answer, although the correct one for this old question, is incomplete. Texture2D constructor returns an uninitialized texture, which is never painted on screen. In order to use that approach, you need to set the texture's data like this: Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color); Int32[] pixel = {0xFFFFFF}; // White. 0xFF is Red, 0xFF0000 is Blue SimpleTexture.SetData<Int32> (pixel, 0, SimpleTexture.Width * SimpleTexture.Height); // Paint a 100x1 line starting at 20, 50 this.spriteBatch.Draw(SimpleTexture, new Rectangle(20, 50, 100, 1), Color.Blue); Take into account that the way you write the data into pixel must be consistent with the texture's SurfaceFormat. The example works because the texture is being formatted as RGB. Rotations can be applied in spriteBatch.Draw like this: this.spriteBatch.Draw (SimpleTexture, new Rectangle(0, 0, 100, 1), null, Color.Blue, -(float)Math.PI/4, new Vector2 (0f, 0f), SpriteEffects.None, 1f);
found a tutorial for that http://www.bit-101.com/blog/?p=2832 its using a BasicEffect (shader) and the built in draw user primitive in XNA 4.0 some code samples i find helpful: load content method basicEffect = new BasicEffect(GraphicsDevice); basicEffect.VertexColorEnabled = true; basicEffect.Projection = Matrix.CreateOrthographicOffCenter (0, GraphicsDevice.Viewport.Width, // left, right GraphicsDevice.Viewport.Height, 0, // bottom, top 0, 1); draw method basicEffect.CurrentTechnique.Passes[0].Apply(); var vertices = new VertexPositionColor[4]; vertices[0].Position = new Vector3(100, 100, 0); vertices[0].Color = Color.Black; vertices[1].Position = new Vector3(200, 100, 0); vertices[1].Color = Color.Red; vertices[2].Position = new Vector3(200, 200, 0); vertices[2].Color = Color.Black; vertices[3].Position = new Vector3(100, 200, 0); vertices[3].Color = Color.Red; GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, 2); have fun and vote up if this helped you. also pay a visit to the tutorial i got this from.
Well, you can do it in a very simple way without getting into the 3D horrible vector stuff. Just create a quick texture, for example: Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color); And then just draw a line using that texture: this.spriteBatch.Draw(SimpleTexture, new Rectangle(100, 100, 100, 1), Color.Blue); I hope this helps
The simplest best way, I think, is to get the image of just a white pixel then stretch that pixel in a rectangle to look like a line I made a Line class, class Line { Texture pixel = ((set this to a texture of a white pixel with no border)); Vector2 p1, p2; //this will be the position in the center of the line int length, thickness; //length and thickness of the line, or width and height of rectangle Rectangle rect; //where the line will be drawn float rotation; // rotation of the line, with axis at the center of the line Color color; //p1 and p2 are the two end points of the line public Line(Vector2 p1, Vector2 p2, int thickness, Color color) { this.p1 = p1; this.p2 = p2; this.thickness = thickness; this.color = color; } public void Update(GameTime gameTime) { length = (int)Vector2.Distance(p1, p2); //gets distance between the points rotation = getRotation(p1.X, p1.Y, p2.X, p2.Y); //gets angle between points(method on bottom) rect = new Rectangle((int)p1.X, (int)p1.Y, length, thickness) //To change the line just change the positions of p1 and p2 } public void Draw(SpriteBatch spriteBatch, GameTime gameTime) { spriteBatch.Draw(pixel, rect, null, color, rotation, new Vector2.Zero, SpriteEffects.None, 0.0f); } //this returns the angle between two points in radians private float getRotation(float x, float y, float x2, float y2) { float adj = x - x2; float opp = y - y2; float tan = opp / adj; float res = MathHelper.ToDegrees((float)Math.Atan2(opp, adj)); res = (res - 180) % 360; if (res < 0) { res += 360; } res = MathHelper.ToRadians(res); return res; } Hope this helps
There is also the "round line" code that "manders" has released on CodePlex: http://roundline.codeplex.com/ Here is the blog post about it: XNA RoundLine Code Released on CodePlex
Just stretch a white pixel. point = game.Content.Load<Texture2D>("ui/point"); public void DrawLine(Vector2 start, Vector2 end, Color color) { Vector2 edge = end - start; float angle = (float)Math.Atan2(edge.Y, edge.X); spriteBatch.Begin(); spriteBatch.Draw(point, new Rectangle((int)start.X, (int)start.Y, (int)edge.Length(), 1), null, color, angle, new Vector2(0, 0), SpriteEffects.None, 0); spriteBatch.End(); }
I wanted to draw rays so that I could debug rays created by explosions and where they intersect objects. This will draw a single pixel thin line between two points. This is what I did: Class to store some simple ray data. The XNA default ray class could work, but it doesn't store the length of the ray to intersection. public class myRay { public Vector3 position, direction; public float length; } A list to store the rays that are to be drawn: List<myRay> DebugRays= new List<myRay>(); Create a BasicEffect and pass it a "Matrix.CreateOrthographicOffCenter" projection with your desired resolution in the LoadContent method. Then run this in the draw method: private void DrawRays() { spriteBatch.Begin(); foreach (myRay ray in DebugRays) { //An array of 2 vertices - a start and end position VertexPositionColor[] Vertices = new VertexPositionColor[2]; int[] Indices = new int[2]; //Starting position of the ray Vertices[0] = new VertexPositionColor() { Color = Color.Orange, Position = ray.position }; //End point of the ray Vertices[1] = new VertexPositionColor() { Color = Color.Orange, Position = ray.position + (ray.direction * ray.length) }; Indices[0] = 0; Indices[1] = 1; foreach (EffectPass pass in BasicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineStrip, Vertices, 0, 2, Indices, 0, 1, VertexPositionColorTexture.VertexDeclaration); } } spriteBatch.End(); } So when an explosion happens in my game it does this (Psuedocode): OnExplosionHappened() { DebugRays.Clear() myRay ray = new myRay() { position = explosion.Position, direction = GetDirection(explosion, solid), //Used GetValueOrDefault here to prevent null value errors length = explosionRay.Intersects(solid.BoundingBox).GetValueOrDefault() }; DebugRays.Add(ray); } It's pretty simple (It possibly looks way more complicated than it is) and it'd be easy to put it into a separate class that you never have to think about again. It also lets you draw a whole lot of lines at once.
I encountered this problem my self and decided to make a class called LineBatch. LineBatch will draw lines without needing a spriteBatch or dots. The class is below. public class LineBatch { bool cares_about_begin_without_end; bool began; GraphicsDevice GraphicsDevice; List<VertexPositionColor> verticies = new List<VertexPositionColor>(); BasicEffect effect; public LineBatch(GraphicsDevice graphics) { GraphicsDevice = graphics; effect = new BasicEffect(GraphicsDevice); Matrix world = Matrix.Identity; Matrix view = Matrix.CreateTranslation(-GraphicsDevice.Viewport.Width / 2, -GraphicsDevice.Viewport.Height / 2, 0); Matrix projection = Matrix.CreateOrthographic(GraphicsDevice.Viewport.Width, -GraphicsDevice.Viewport.Height, -10, 10); effect.World = world; effect.View = view; effect.VertexColorEnabled = true; effect.Projection = projection; effect.DiffuseColor = Color.White.ToVector3(); cares_about_begin_without_end = true; } public LineBatch(GraphicsDevice graphics, bool cares_about_begin_without_end) { this.cares_about_begin_without_end = cares_about_begin_without_end; GraphicsDevice = graphics; effect = new BasicEffect(GraphicsDevice); Matrix world = Matrix.Identity; Matrix view = Matrix.CreateTranslation(-GraphicsDevice.Viewport.Width / 2, -GraphicsDevice.Viewport.Height / 2, 0); Matrix projection = Matrix.CreateOrthographic(GraphicsDevice.Viewport.Width, -GraphicsDevice.Viewport.Height, -10, 10); effect.World = world; effect.View = view; effect.VertexColorEnabled = true; effect.Projection = projection; effect.DiffuseColor = Color.White.ToVector3(); } public void DrawAngledLineWithRadians(Vector2 start, float length, float radians, Color color) { Vector2 offset = new Vector2( (float)Math.Sin(radians) * length, //x -(float)Math.Cos(radians) * length //y ); Draw(start, start + offset, color); } public void DrawOutLineOfRectangle(Rectangle rectangle, Color color) { Draw(new Vector2(rectangle.X, rectangle.Y), new Vector2(rectangle.X + rectangle.Width, rectangle.Y), color); Draw(new Vector2(rectangle.X, rectangle.Y), new Vector2(rectangle.X, rectangle.Y + rectangle.Height), color); Draw(new Vector2(rectangle.X + rectangle.Width, rectangle.Y), new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), color); Draw(new Vector2(rectangle.X, rectangle.Y + rectangle.Height), new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), color); } public void DrawOutLineOfTriangle(Vector2 point_1, Vector2 point_2, Vector2 point_3, Color color) { Draw(point_1, point_2, color); Draw(point_1, point_3, color); Draw(point_2, point_3, color); } float GetRadians(float angleDegrees) { return angleDegrees * ((float)Math.PI) / 180.0f; } public void DrawAngledLine(Vector2 start, float length, float angleDegrees, Color color) { DrawAngledLineWithRadians(start, length, GetRadians(angleDegrees), color); } public void Draw(Vector2 start, Vector2 end, Color color) { verticies.Add(new VertexPositionColor(new Vector3(start, 0f), color)); verticies.Add(new VertexPositionColor(new Vector3(end, 0f), color)); } public void Draw(Vector3 start, Vector3 end, Color color) { verticies.Add(new VertexPositionColor(start, color)); verticies.Add(new VertexPositionColor(end, color)); } public void End() { if (!began) if (cares_about_begin_without_end) throw new ArgumentException("Please add begin before end!"); else Begin(); if (verticies.Count > 0) { VertexBuffer vb = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), verticies.Count, BufferUsage.WriteOnly); vb.SetData<VertexPositionColor>(verticies.ToArray()); GraphicsDevice.SetVertexBuffer(vb); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, verticies.Count / 2); } } began = false; } public void Begin() { if (began) if (cares_about_begin_without_end) throw new ArgumentException("You forgot end."); else End(); verticies.Clear(); began = true; } }
Here is a simple way that I use to make lines by specifying a start coordinate, an end coordinate, width, and color of them: NOTE: you must add a file named "dot" to the content directory (the line will be made out of these). using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace Xna.LineHelper { public class LineManager { int loopCounter; int lineLegnth; Vector2 lineDirection; Vector2 _position; Color dotColor; Rectangle _rectangle; List<Texture2D> _dots = new List<Texture2D>(); FunctionsLibrary functions = new FunctionsLibrary(); public void CreateLineFiles(Vector2 startPosition, Vector2 endPosition, int width, Color color, ContentManager content) { dotColor = color; _position.X = startPosition.X; _position.Y = startPosition.Y; lineLegnth = functions.Distance((int)startPosition.X, (int)endPosition.X, (int)startPosition.Y, (int)endPosition.Y); lineDirection = new Vector2((endPosition.X - startPosition.X) / lineLegnth, (endPosition.Y - startPosition.Y) / lineLegnth); _dots.Clear(); loopCounter = 0; _rectangle = new Rectangle((int)startPosition.X, (int)startPosition.Y, width, width); while (loopCounter < lineLegnth) { Texture2D dot = content.Load<Texture2D>("dot"); _dots.Add(dot); loopCounter += 1; } } public void DrawLoadedLine(SpriteBatch sb) { foreach (Texture2D dot in _dots) { _position.X += lineDirection.X; _position.Y += lineDirection.Y; _rectangle.X = (int)_position.X; _rectangle.Y = (int)_position.Y; sb.Draw(dot, _rectangle, dotColor); } } } public class FunctionsLibrary { //Random for all methods Random Rand = new Random(); #region math public int TriangleArea1(int bottom, int height) { int answer = (bottom * height / 2); return answer; } public double TriangleArea2(int A, int B, int C) { int s = ((A + B + C) / 2); double answer = (Math.Sqrt(s * (s - A) * (s - B) * (s - C))); return answer; } public int RectangleArea(int side1, int side2) { int answer = (side1 * side2); return answer; } public int SquareArea(int side) { int answer = (side * side); return answer; } public double CircleArea(int diameter) { double answer = (((diameter / 2) * (diameter / 2)) * Math.PI); return answer; } public int Diference(int A, int B) { int distance = Math.Abs(A - B); return distance; } #endregion #region standardFunctions public int Distance(int x1, int x2, int y1, int y2) { return (int)(Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); } #endregion } }