我正在用Java制作游戏引擎。我试图在我的游戏中添加盒子碰撞,这个盒子需要能够与玩家一起旋转。我搜索并找到了一个计算旋转点的新点的公式,然而,当我旋转我的点时,他们似乎遵循一个奇怪的不成比例的图8路径而不是围绕我的盒子中心的圆圈。
for (Point p : points) {
//Loops through every point on the box (Square)
//top, left, bottom, right
float pointX = p.getX();
float pointY = p.getY();
//rotation as radians
float cos = (float) Math.cos(rotation);
float sin = (float) Math.sin(rotation);
pointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
pointY = centerY -(pointY-centerY) * cos + (pointX-centerX) * sin;
p.setPos(pointX, pointY);
}
以下是旋转播放器时盒子发生的情况: https://gyazo.com/ff801ce8458269c2385e24b2dc5404f5
任何帮助都将不胜感激,我已经解决了这个问题已有近一个星期了,结果相同。
答案 0 :(得分:2)
问题是您使用新值pointY
计算pointX
。
感谢@Imus answer正确计算。
尝试:
float pointX = p.getX();
float pointY = p.getY();
//rotation as radians
float cos = (float) Math.cos(rotation);
float sin = (float) Math.sin(rotation);
float newPointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
float newPointY = centerY +(pointY-centerY) * cos - (pointX-centerX) * sin;
p.setPos(newPointX, newPointY);
答案 1 :(得分:0)
请记住,在java中,Y轴指向下方。
我自己没有测试,但您可以尝试以下几行:
pointX = centerX +(pointX-centerX) * cos + (pointY-centerY) * sin;
pointY = centerY +(pointY-centerY) * cos - (pointX-centerX) * sin;