【问题标题】:Make a shape move towards mouse in SFML在 SFML 中使形状向鼠标移动
【发布时间】:2021-04-09 12:58:18
【问题描述】:

这是我的代码:

void Ant::Update(float dt, sf::RenderWindow& window) {
// calculate difference between mouse pos and ant pos
float x = sf::Mouse::getPosition(window).x;
float y = sf::Mouse::getPosition(window).y;

if (this->pos.x + radius > x) {
    this->pos.x -= speed * dt;
}
if (this->pos.y + radius > y) {
    this->pos.y -= speed * dt;
}
if (this->pos.x + radius < x) {
    this->pos.x += speed * dt;
}
if (this->pos.y + radius < y) {
    this->pos.y += speed * dt;
}

this->antCircle.setPosition(this->pos.x, this->pos.y);
}

所以我想要一个圆平滑(如旋转)面对电脑鼠标。现在,它不会循环旋转以向鼠标移动;它只是突然改变方向以跟随鼠标。我知道这涉及一些触发,但我希望有人可以帮助我解决这个问题。谢谢!

编辑 1: 我试过这个

void Ant::Update(float dt, sf::RenderWindow& window) {
// calculate difference between mouse pos and ant pos
float x = sf::Mouse::getPosition(window).x;
float y = sf::Mouse::getPosition(window).y;

if (this->stackSpeed.x > 3) {
    this->stackSpeed.x = 0;
}
if (this->stackSpeed.y > 3) {
    this->stackSpeed.y = 0;
}

if (this->pos.x + radius > x) {
    //this->pos.x -= speed * dt;
    this->stackSpeed.x -= speed * dt;
}
if (this->pos.y + radius > y) {
    //this->pos.y -= speed * dt;
    this->stackSpeed.y -= speed * dt;
}
if (this->pos.x + radius < x) {
    //this->pos.x += speed * dt;
    this->stackSpeed.x += speed * dt;
}
if (this->pos.y + radius < y) {
    //this->pos.y += speed * dt;
    this->stackSpeed.y += speed * dt;
}

this->pos.x += this->stackSpeed.x;
this->pos.y += this->stackSpeed.y;

this->antCircle.setPosition(this->pos.x, this->pos.y);

}

运气不好。这只会让球在整个屏幕上振动。

【问题讨论】:

    标签: c++ sfml shapes


    【解决方案1】:

    实现更平滑运动的一种简单方法是让圆有自己的速度,然后根据位置修改该速度。

    void Ant::Update(float dt, sf::RenderWindow& window) {
    
        // calculate difference between mouse pos and ant pos
        float x = sf::Mouse::getPosition(window).x;
        float y = sf::Mouse::getPosition(window).y;
        
        if (this->pos.x + radius > x) {
            this->speed.x -= force * dt;
        }
        if (this->pos.y + radius > y) {
            this->speed.y -= force * dt;
        }
        if (this->pos.x + radius < x) {
            this->speed.x += force * dt;
        }
        if (this->pos.y + radius < y) {
            this->speed.y += force * dt;
        }
    
        this->pos.x += this->speed.x;
        this->pos.y += this->speed.y;
        
        this->antCircle.setPosition(this->pos.x, this->pos.y);
    }
    

    您可能还想确保速度不会变得太大。给它一个不允许超过的最大值。 或者使用一些适当的物理和数学来进行更真实的计算。

    【讨论】:

    • 我对你所说的有点困惑。你改变的是速度而不是位置......
    • @jbcallv 好吧,两者都是。首先更新速度。然后使用速度更新位置。我省略了更新 pos.xpos.y 因为我认为这从示例中显而易见。
    • 变量名的选择也很糟糕...一个名为 speed 的成员和一个名为 speed 的全局变量。
    • 仅供参考,在成员函数中引用类成员时不需要使用this-&gt;
    • "一个名为 speed 的成员和一个全局..." 我是 C++ 新手。你指的是哪些变量?我称为速度的那个还是您添加的称为速度的 2D 矢量?感谢您让我知道这件事->
    【解决方案2】:

    我找到了解决方案。我使用了距离公式 sqrt((x2-x1)^2 + (y2-y1)^2):

    void Ant::Update(float dt, sf::RenderWindow& window) {
    // calculate difference between mouse pos and ant pos
    float x = sf::Mouse::getPosition(window).x;
    float y = sf::Mouse::getPosition(window).y;
    
    float xDist = x - (this->pos.x + radius);
    float yDist = y - (this->pos.y + radius);
    float dist = sqrt((xDist * xDist) + (yDist * yDist));
    
    if (dist > 1) {
        this->pos.x += xDist * dt * speed;
        this->pos.y += yDist * dt * speed;
    }
    
    this->antCircle.setPosition(this->pos.x, this->pos.y);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 2023-01-22
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多