【问题标题】:How to repair the spacing between blocks when I rotate them?旋转块时如何修复块之间的间距?
【发布时间】:2019-05-27 23:14:31
【问题描述】:

您好,我目前正在尝试旋转正方形内的块。但是当我开始旋转它们时,它开始在我不想要的块之间创建奇怪的空间。你能帮我解决块之间的空格问题吗?这是一些代码和屏幕截图。

https://imgur.com/a/BLuO7FF

我已经检查过所有角度和半径是否计算正确,我没有发现任何问题。

世界.h

#pragma once
#include <SFML/Graphics.hpp>

class World
{
public:
    World(sf::Vector2f Wpos);

    float AngleToRadian(int angle);
    void RotateWorld();
    void draw(sf::RenderWindow &window);

    sf::Texture tx;
    sf::Sprite** Block;
    sf::Vector2f Pos;
    sf::Vector2i Size;
    float** radius;
    float** angle;
}; 

世界.cpp

#include "World.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>

#define PI 3.14159

World::World(sf::Vector2f Wpos)
{
    Pos = Wpos;
    Size = sf::Vector2i(10, 10);

    Block = new sf::Sprite*[Size.y];
    radius = new float*[Size.y];
    angle = new float*[Size.y];

    for (int i = 0; i < Size.y; i++)
    {
        Block[i] = new sf::Sprite[Size.x];
        radius[i] = new float[Size.x];
        angle[i] = new float[Size.x];
    }

    tx.loadFromFile("Img/Block.png");
    sf::Vector2i off(Size.x * tx.getSize().x / 2, Size.y * tx.getSize().y / 2); //tx size is 32px x 32px

    for (int y = 0; y < Size.y; y++)
    {
        for (int x = 0; x < Size.x; x++)
        {
            Block[y][x].setTexture(tx);
            Block[y][x].setOrigin(tx.getSize().x / 2, tx.getSize().y / 2);
            Block[y][x].setPosition(x*tx.getSize().x + Wpos.x - off.x + Block[y][x].getOrigin().x, y*tx.getSize().y + Wpos.y - off.y + Block[y][x].getOrigin().y);

            radius[y][x] = sqrt(pow(Pos.x - Block[y][x].getPosition().x, 2) + pow(Pos.y - Block[y][x].getPosition().y, 2));

            angle[y][x] = (atan2(Block[y][x].getPosition().y - Pos.y, Block[y][x].getPosition().x - Pos.x) * 180.0) / PI;

            if ((atan2(Block[y][x].getPosition().y - Pos.y, Block[y][x].getPosition().x - Pos.x) * 180.0) / PI < 0)
            {
                angle[y][x] += 360;
            }

            //angle[y][x] = round(angle[y][x]);
            /*radius[y][x] = round(radius[y][x]);*/
        }
    }
}

void World::RotateWorld()
{
    float dx = 0, dy = 0;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
    {
        for (int y = 0; y < Size.y; y++)
        {
            for (int x = 0; x < Size.x; x++)
            {
                Block[y][x].rotate(1);
                if (angle[y][x] >= 360)
                {
                    angle[y][x] = 0;
                }
                angle[y][x]++;

                dx = cos(AngleToRadian(angle[y][x])) * radius[y][x];
                dy = sin(AngleToRadian(angle[y][x])) * radius[y][x];

                Block[y][x].setPosition(Pos.x + dx, Pos.y + dy);
            }
        }
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
    {
        for (int y = 0; y < Size.y; y++)
        {
            for (int x = 0; x < Size.x; x++)
            {
                Block[y][x].rotate(-1);
                if (angle[y][x] >= 360)
                {
                    angle[y][x] = 0;
                }
                angle[y][x]--;

                dx = cos(AngleToRadian(angle[y][x])) * radius[y][x];
                dy = sin(AngleToRadian(angle[y][x])) * radius[y][x];

                Block[y][x].setPosition(Pos.x + dx, Pos.y + dy);
            }
        }
    }
}

我希望它可以在没有任何空格的情况下旋转。如果有人能帮助我,我将非常感激。

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    我会尝试使用getGlobalBounds() 方法而不是sf::Texture 大小获取器来设置sf::Sprite 的原点。

    差异似乎很小,可能就是这种情况。

        Block[y][x].setTexture(tx);
        Block[y][x].setOrigin(Block[y][x].getGlobalBouds().width / 2, Block[y][x].getGlobalBouds().height / 2);
        Block[y][x].setPosition(x*Block[y][x].getGlobalBouds().width + Wpos.x - off.x + Block[y][x].getOrigin().x, y*Block[y][x].getGlobalBouds().height + Wpos.y - off.y + Block[y][x].getOrigin().y);
    

    【讨论】:

    • 我修复了问题。我只需要在函数“AngleToRadian”中将参数更改为浮点类型而不是 int。但是感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2018-08-09
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多