【发布时间】: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);
}
运气不好。这只会让球在整个屏幕上振动。
【问题讨论】: