顶点数组的 SFML 变换实际上并未应用于顶点吗?

问题描述 投票:0回答:1

我正在尝试在我用 SFML 制作的游戏中实现 SAT 碰撞检测,但我注意到我用来让玩家使用的顶点数组中的顶点位置没有改变位置,即使我应用到它的变换工作正常.

这是 Player 类:

玩家.h

#include <SFML/Graphics.hpp>

#ifndef PLAYER_H
#define PLAYER_H

class Player : public sf::Drawable, public sf::Transformable
{
    private:
    sf::VertexArray square;
    sf::Vector2f velocity;
    float speed = 10.f;
    sf::Vector2f position;
    sf::Transform m_transform;

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    public:
        Player() = default;
        Player(int screen_width, int screen_height);
        void create(int screen_width, int screen_height);
        void move(bool w, bool a, bool s, bool d);
        void setVertexColor(sf::Color color, int vertex);
        int getVertexCount();
        sf::Vector2f getVertexPos(int n);
};

#endif

播放器.cpp

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


Player::Player( int screen_width, int screen_height)
{
    create(screen_width, screen_height);
}

void Player::create( int screen_width, int screen_height)
{
   float size = 60.f;

   square.setPrimitiveType(sf::Triangles);
   square.resize(6);
    
   square[0].position = sf::Vector2f(0.f, 0.f); //top left
   square[1].position = sf::Vector2f(0.f, size); //bottom left
   square[2].position = sf::Vector2f(size, 0.f); //top right
   square[3].position = sf::Vector2f(0.f, size); //bottom left
   square[4].position = sf::Vector2f(size, 0.f); //top right
   square[5].position = sf::Vector2f(size, size); // bottom left
   
   
   square[0].color = sf::Color(200,200,200);
   square[1].color = sf::Color::White;
   square[2].color = sf::Color(100,100,100);
   square[3].color = sf::Color::White;
   square[4].color = sf::Color(100,100,100);
   square[5].color = sf::Color(200,200,200);
   m_transform = getTransform();
}

void Player::move(bool w, bool a, bool s, bool d)
{
    float mod_velocity;
    sf::Vector2f t;
    sf::Vector2f pos;

    velocity = sf::Vector2f(0.f, 0.f);

    if (w)
    {
        velocity.y -= 1;
    }
    if (a)
    {
        velocity.x -= 1;
    }
    if (s)
    {
        velocity.y += 1;
    }
    if (d)
    {
        velocity.x += 1;
    }
    
    if ((velocity.x != 0.f) || (velocity.y != 0.f))
    {
        mod_velocity = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y));
        t = velocity/mod_velocity;
        velocity = t * speed;
        
    }

    m_transform.translate(velocity);

    for (int i = 0; i < 6; i++)
    {
    std::cout << "square[" << i << "].position:  " << square[i].position.x <<" " <<square[i].position.y <<std::endl; 
        
    }
    
}

void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= m_transform;

    // draw the vertex array
    target.draw(square, states);
}

int Player::getVertexCount()
{
    return square.getVertexCount();
}

sf::Vector2f Player::getVertexPos(int n)
{
    sf::Vertex vertex = square[n];
    return getTransform().transformPoint( vertex.position );

}

void Player::setVertexColor(sf::Color color, int vertex)
{
    square[vertex].color = color;
}

在类中,有一个 cout 显示顶点的位置,即使我移动玩家(并且它在屏幕上移动),它们也保持不变。如何获得顶点的实际位置?

c++ transform sfml
1个回答
0
投票

Player::create
方法中,您将
sf::Transformable
的副本分配给类成员
m_transformable
。然后您将修改此副本而不是更改原始对象。但是,
Player::getVertexPos
返回原始变换,而不是修改。 要解决这个问题,您可以将方法中的 return 语句更改为

return m_transformable.transformPoint(vertex.position);

...或将转换应用于类的原始

sf::Transformable
基类(否则你根本不需要这个基类)。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.