如何比较 sfml 中的位置?
How to compare positions in sfml?
我是 sfml 的新手,我正在制作一个简单的游戏。我需要比较 2 个职位,但我找不到该怎么做。
我该怎么做?我虽然我可以这样做:
if (somesprite.getPosition() < (some x,some y)) { some code}
所以我只需要找出如何比较两个位置。
预先感谢您的回答,这将使我更接近于找到正确的方法。
- 托斯梅尔
getPosition()
returns 具有减法重载的 sf::Vector2<T>
。
从另一个中减去一个 sf::Vector2<T>
,所得 sf::Vector2<T>
的长度将是位置之间的距离。
#include <SFML/System/Vector2.hpp>
#include <cmath>
template<typename T>
T Vector2length(const sf::Vector2<T>& v) {
return std::sqrt(v.x * v.x + v.y * v.y);
}
void some_func() {
auto spos = somesprite.getPosition();
decltype(spos) xy(some_x, some_y);
auto distance_vec = spos - xy;
if( Vector2length(distance_vec) < max_distance ) {
// do stuff
}
}
由于 sf::Vector2<T>
缺少 length()
和其他通常与笛卡尔向量相关的常用函数,上述方法的替代方法是继承 sf::Vector2<T>
并扩展它:
#include <SFML/System/Vector2.hpp>
#include <cmath>
// extending sf::Vector2<T> with functions that could be nice to have
template<typename T>
struct Vector2_ext : sf::Vector2<T> {
using sf::Vector2<T>::Vector2;
// converting from a sf::Vector2
Vector2_ext(const sf::Vector2<T>& o) : sf::Vector2<T>(o) {}
// converting back to a sf::Vector2
operator sf::Vector2<T>&() { return *this; }
operator sf::Vector2<T> const&() const { return *this; }
// your utility functions
T length() const { return std::sqrt(this->x * this->x + this->y * this->y); }
};
// deduction guide
template<typename T>
Vector2_ext(T, T)->Vector2_ext<T>;
有了这个,你可以在需要的时候在sf::Vector2<T>
和Vector2_ext<T>
之间来回转换。
int some_func(sf::Sprite& somesprite, float some_x, float some_y, float max_distance) {
auto distance =
// somesprite.getPosition() - sf::Vector2(some_x, some_y) returns a sf::Vector2<T>
// that we convert to a temporary Vector2_ext<T> and call its length() function:
Vector2_ext(somesprite.getPosition() - sf::Vector2(some_x, some_y)).length();
if(distance < max_distance) {
// do stuff
}
}
我是 sfml 的新手,我正在制作一个简单的游戏。我需要比较 2 个职位,但我找不到该怎么做。
我该怎么做?我虽然我可以这样做:
if (somesprite.getPosition() < (some x,some y)) { some code}
所以我只需要找出如何比较两个位置。
预先感谢您的回答,这将使我更接近于找到正确的方法。
- 托斯梅尔
getPosition()
returns 具有减法重载的 sf::Vector2<T>
。
从另一个中减去一个 sf::Vector2<T>
,所得 sf::Vector2<T>
的长度将是位置之间的距离。
#include <SFML/System/Vector2.hpp>
#include <cmath>
template<typename T>
T Vector2length(const sf::Vector2<T>& v) {
return std::sqrt(v.x * v.x + v.y * v.y);
}
void some_func() {
auto spos = somesprite.getPosition();
decltype(spos) xy(some_x, some_y);
auto distance_vec = spos - xy;
if( Vector2length(distance_vec) < max_distance ) {
// do stuff
}
}
由于 sf::Vector2<T>
缺少 length()
和其他通常与笛卡尔向量相关的常用函数,上述方法的替代方法是继承 sf::Vector2<T>
并扩展它:
#include <SFML/System/Vector2.hpp>
#include <cmath>
// extending sf::Vector2<T> with functions that could be nice to have
template<typename T>
struct Vector2_ext : sf::Vector2<T> {
using sf::Vector2<T>::Vector2;
// converting from a sf::Vector2
Vector2_ext(const sf::Vector2<T>& o) : sf::Vector2<T>(o) {}
// converting back to a sf::Vector2
operator sf::Vector2<T>&() { return *this; }
operator sf::Vector2<T> const&() const { return *this; }
// your utility functions
T length() const { return std::sqrt(this->x * this->x + this->y * this->y); }
};
// deduction guide
template<typename T>
Vector2_ext(T, T)->Vector2_ext<T>;
有了这个,你可以在需要的时候在sf::Vector2<T>
和Vector2_ext<T>
之间来回转换。
int some_func(sf::Sprite& somesprite, float some_x, float some_y, float max_distance) {
auto distance =
// somesprite.getPosition() - sf::Vector2(some_x, some_y) returns a sf::Vector2<T>
// that we convert to a temporary Vector2_ext<T> and call its length() function:
Vector2_ext(somesprite.getPosition() - sf::Vector2(some_x, some_y)).length();
if(distance < max_distance) {
// do stuff
}
}