简体   繁体   中英

SFML unexpected sf::Vertex movement

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.05f) it becomes very clear that the afterimage of the Vertex (also diagonal or reverse) is being dragged 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. What can I do to stop the dragging and snap it to its actual position it shows me in the console?

PS I left out the float-wrappers because it didn't help and makes it less readable (also some currently unused functions are left out) :)

Thank you in advance!

#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);

        //Maps the coordinates to the vectors, top-left = [0] then clockwise
        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 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; }
};

I've been trying to remove that wiggling with no results.

I tried to round (or floor, as you tried) each coordinate. Doesn't work

I tried to multiply each coordinate by 100 (storing as int ) and then divide (by 100 again), with the hope of leave just 2 decimal positions. Doesn't work

I tried to multiply and divide, but by max (and min) int values. Doesn't work

In some cases, multiply and divide by a not so big int (like 10000) seems to work

My conclusion is that problem is related with float decimals , and in some cases, those decimals are rounded, causing the weird effect.

Sorry for this poor help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM