同时绕两个点旋转
Rotating around 2 points at the same time
基本上,我试图同时围绕两个点旋转一个正方形。
这里,当鼠标向左或向右移动时,我试图围绕它的中心旋转正方形,当鼠标向上或向下移动时,我围绕任何其他点旋转(50:100)。然而,我的方块在屏幕上跳跃,当我试图只围绕中心旋转时,它继续以50:100的速度旋转。有什么建议如何解决这个问题?
#include <SFML/Graphics.hpp>
using namespace sf;
Event evt;
int main(int argc, char* argv)
{
RenderWindow window(VideoMode(600, 600), "test");
RectangleShape test(Vector2<float>(20.0f, 20.0f));
test.setFillColor(Color::Red);
test.setPosition(300, 300);
test.setOrigin(10, 10);
Clock deltaTime;
Clock timer;
timer.restart();
int mouseX = 0, mouseY = 0;
int curMouseX = 0, curMouseY = 0;
float offset = 100;
bool moving = false;
while (window.isOpen())
{
while (window.pollEvent(evt)) {
switch (evt.type)
{
case Event::MouseMoved:
curMouseX = mouseX;
curMouseY = mouseY;
mouseX = evt.mouseMove.x;
mouseY = evt.mouseMove.y;
moving = true;
break;
}
}
float elaspedTime = deltaTime.restart().asSeconds();
if (curMouseX != mouseX && moving) {
test.setOrigin(10, 10);
test.rotate(360 * elaspedTime);
// test.setOrigin(50, 100); Tried this to avoid jumping. When uncommented, doesn't rotate around the center.
}
if (curMouseY != mouseY && moving) {
test.setOrigin(50, 100);
test.rotate(60 * elaspedTime);
}
window.clear();
window.draw(test);
window.display();
moving = false;
}
return 0;
}
不是使用从形状继承的sf::Transfomable
类,而是为它创建一个sf::Transform
并使用rotate (float angle, const Vector2f ¢er)
你只需要将鼠标每次移动的位置存储在sf::Vector2f
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- 如何找到两个不同坐标系中对应点之间的旋转和平移(变换矩阵)
- 旋转两个球
- 比较两个字符串以查看它们是否在旋转
- 我们如何计算旋转和平移两个立体相机在opencv StereoRectify(r,t自变量)中使用
- OpenGL:两个物体有两个独立的光源围绕它们旋转
- 同时绕两个点旋转
- 是否有可能得到两个图像之间的旋转和缩放只有一个冲浪描述符
- Box2d旋转时两个物体碰撞时的奇怪行为
- 光不断地覆盖一个旋转立方体的两个相同的边
- OpenCV SURF -获得两个图像之间的旋转角度
- 求两个全等三角形之间的旋转