Hey everyone and thanks for reading,
I'm trying to make an Endless Runner and created a
vector<TILE*>* tiles = new vector<TILE*>[LANE_HEIGHT] //LANE_HEIGHT is the number of vertical TILEs
TILE is basically a babysitter class for a sf::VertexArray that populates it, applies texture, draws the object and moves the sf::Vertex points (moving is the problem)
I loop through all arrays with
for (int i = 0; i < LANE_HEIGHT;i++) {
for (int j = 0; j < tiles.size(); j++) {
if(tiles[j] != nullptr)
tiles[j]->move(sf::Vector2f(0.2f,0)); //Framerate is set to 30 to make this clearer
}
}
My problem here is that most floats applied to
tiles[j]->move()
cause the object to '
wiggle', making it look like it's in shallow water.
I first thought it was a rounding error so I went through the whole class and wrapped everything in float(x), no success
I get the right results in numbers but when setting the applied float to
float(0.1f)
it becomes very clear: The left side of the Vertex is 'being left behind' by the rest and after a couple of frames it's jumping to its wanted position, then repeats
Since every object in the array is doing this and overlaps onto the previous one, the wiggle effect is born
I'm losing my mind.
P.S. I left out the float-wrappers because it didn't help and makes it less readable and 'boldified' the affected parts
Thank you in advance!
Farbi
#pragma once
#include <iostream>
#include <vector>
#include "SFML/Graphics.hpp"
class TILE{
private:
const float WIDTH; //Width of the object
const float HEIGHT; //Height of the object
sf::VertexArray body; //Body for drawing purposes (holds all Vertex points)
sf::Vector2f globalPos; //Global position in the game window
sf::Texture* texture; //Stores applied texture
public:
TILE::TILE(sf::Vector2f dim) : WIDTH(dim.x), HEIGHT(dim.y) { }
TILE::TILE(sf::Vector2f position, sf::Texture* tex, sf::Vector2f dim) : WIDTH(dim.x), HEIGHT(dim.y){
globalPos = position;
texture = tex;
body = sf::VertexArray(sf::TriangleFan, 4);
body[0].position = sf::Vector2f(globalPos.x + (-(WIDTH / 2.f)), globalPos.y + (-(HEIGHT / 2.f)));
body[1].position = sf::Vector2f(globalPos.x + (WIDTH / 2.f), globalPos.y + (-(HEIGHT / 2.f)));
body[2].position = sf::Vector2f(globalPos.x + (WIDTH / 2.f), globalPos.y + (HEIGHT / 2.f));
body[3].position = sf::Vector2f(globalPos.x + (-(WIDTH / 2.f)), globalPos.y + (HEIGHT / 2.f));
body[0].texCoords = sf::Vector2f(0.f, 0.f);
body[1].texCoords = sf::Vector2f(float(texture->getSize().x), 0.f);
body[2].texCoords = sf::Vector2f(float(texture->getSize().x), float(texture->getSize().y));
body[3].texCoords = sf::Vector2f(0.f, float(texture->getSize().y));
}
TILE::~TILE() {
texture = nullptr;
}
void draw(sf::RenderWindow &window) { window.draw(body, texture); }
sf::Vector2f getBB_TopLeft() { return body[0].position; }
sf::Vector2f getBB_BotRight() { return body[2].position; }
void setTexture(sf::Texture* tex) {
texture = tex;
body[0].texCoords = sf::Vector2f(0.f, 0.f);
body[1].texCoords = sf::Vector2f(float(texture->getSize().x), 0.f);
body[2].texCoords = sf::Vector2f(float(texture->getSize().x), float(texture->getSize().y));
body[3].texCoords = sf::Vector2f(0.f, float(texture->getSize().y));
}
void setGlobalPos(sf::Vector2f position) {
globalPos = position;
body = sf::VertexArray(sf::TriangleFan, 4);
body[0].position = sf::Vector2f(globalPos.x + (-(WIDTH / 2.f)), globalPos.y + (-(HEIGHT / 2.f)));
body[1].position = sf::Vector2f(globalPos.x + (WIDTH / 2.f), globalPos.y + (-(HEIGHT / 2.f)));
body[2].position = sf::Vector2f(globalPos.x + (WIDTH / 2.f), globalPos.y + (HEIGHT / 2.f));
body[3].position = sf::Vector2f(globalPos.x + (-(WIDTH / 2.f)), globalPos.y + (HEIGHT / 2.f));
}
void move(sf::Vector2f dir) {
globalPos += dir;
body[0].position += dir;
body[1].position += dir;
body[2].position += dir;
body[3].position += dir;
}
sf::Vector2f getGlobalPos() { return globalPos; }
sf::VertexArray getBody() { return body; }
sf::Vector2f getDimensions() { return sf::Vector2f(WIDTH, HEIGHT); }
};