- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试为游戏制作一个简单的物理引擎。我很清楚这是在重新发明轮子,但它更多的是一种学习练习。我从没想过它会像 box2d 那样完整。
我在实现 2d vector 时遇到问题。该问题与以下事实有关:在游戏世界中,我想将北向表示为 0 弧度,将东向表示为 1/2 PI 弧度,即分别为 0 度和 90 度。然而在数学中(或者更具体地说是 Java 的数学类),我相信像正弦和余弦这样的三角函数假设“东”是零弧度,而我认为北是 1/2 PI 弧度?
无论如何,我已经编写了 vector 类的一个小版本,它仅演示了错误的代码。
public class Vector {
private final double x;
private final double y;
public Vector(double xIn, double yIn) {
x = xIn;
y = yIn;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return Math.sqrt((x * x) + (y * y));
}
public double getTheta() {
return Math.atan(y / x);
}
public double bearingTo(Vector target) {
return (Math.atan2(target.getY() - y, target.getX() - x));
}
public static Vector fromPolar(double magnitude, double angle) {
return new Vector(magnitude * Math.cos(angle),
magnitude * Math.sin(angle));
}
}
这是演示该问题的测试代码:
public class SOQuestion {
public static void main(String[] args) {
//This works just fine
Vector origin = new Vector(0, 0);
Vector target = new Vector(10, 10);
double expected = Math.PI * 0.25;
double actual = origin.bearingTo(target);
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
//This doesn't work
origin = new Vector(0, 0);
target = new Vector(10, 0);
expected = Math.PI * 0.5; //90 degrees, or east.
actual = origin.bearingTo(target); //Of course this is going to be zero, because in mathematics right is zero
System.out.println("Expected: " + expected);
System.out.println("Actual: " + actual);
//This doesn't work either
Vector secondTest = Vector.fromPolar(100, Math.PI * 0.5); // set the vector to the cartesian coordinates of (100,0)
System.out.println("X: " + secondTest.getX()); //X ends up being basically zero
System.out.println("Y: " + secondTest.getY()); //Y ends up being 100
} }
要求是:
fromPolar(magnitude,angle)
应返回一个 vector ,其中 x
和 y
初始化为适当的值(假设北在)零弧度,东为 1/2 PI 弧度。例如 fromPolar(10,PI)
应该构造一个 x: 0 和 y: -1 的 vector
getTheta()
应返回一个大于或等于 0 且小于 2 PI 的值。 Theta 是它所调用的 vector 的角度分量。例如,当调用 getTheta()
时,具有 x:10 和 y:10 的 vector 将返回 1/4 PI 的值。
bearingTo(target)
应返回一个大于或等于 0 且小于 2 PI 的值。该值表示相对于另一个 vector 的方位。
测试代码表明,当您尝试获取 (0,0) 处的一个点到 (10,0) 处的另一点的方位角时,它不会产生预期的结果,它应该是 90 度或 1/2 PI 弧度。
同样,尝试从极坐标初始化 vector 会将 x 和 y 坐标设置为意外值。我试图避免说“不正确的值”,因为它不是不正确,只是不符合要求。
我对代码进行了很多修改,在这里添加 PI 的分数或在那里去掉它,切换正弦和余弦,但所有这些都只能解决部分问题,而不是整个问题。
最后我制作了这个代码的一个可以在线执行的版本http://tpcg.io/OYVB5Q
最佳答案
典型的极坐标0
指向东方,并且逆时针旋转。您的坐标从北开始,可能是顺时针方向。修复代码的最简单方法是首先使用以下公式进行角度之间的转换:
flippedAngle = π/2 - originalAngle
该公式是对称的,因为它可以在“您的”坐标和“标准”坐标之间进行双向转换。因此,如果您将代码更改为:
public double bearingTo(Vector target) {
return Math.PI/2 - (Math.atan2(target.getY() - y, target.getX() - x));
}
public static Vector fromPolar(double magnitude, double angle) {
double flippedAngle = Math.PI/2 - angle;
return new Vector(magnitude * Math.cos(flippedAngle),
magnitude * Math.sin(flippedAngle));
}
它开始像您的 tests suggest 一样工作。 。您还可以应用一些三角学知识来不执行此 Math.PI/2 - 角度
计算,但我不确定这是否真的使代码更清晰。
如果您希望“方位角”在 [0, 2*π]
范围内(即始终非负),您可以使用此版本的 bearingTo
(还修复了theta
):
public class Vector {
private final double x;
private final double y;
public Vector(double xIn, double yIn) {
x = xIn;
y = yIn;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return Math.sqrt((x * x) + (y * y));
}
public double getTheta() {
return flippedAtan2(y, x);
}
public double bearingTo(Vector target) {
return flippedAtan2(target.getY() - y, target.getX() - x);
}
public static Vector fromPolar(double magnitude, double angle) {
double flippedAngle = flipAngle(angle);
return new Vector(magnitude * Math.cos(flippedAngle),
magnitude * Math.sin(flippedAngle));
}
// flip the angle between 0 is the East + counter-clockwise and 0 is the North + clockwise
// and vice versa
private static double flipAngle(double angle) {
return Math.PI / 2 - angle;
}
private static double flippedAtan2(double y, double x) {
double angle = Math.atan2(y, x);
double flippedAngle = flipAngle(angle);
// additionally put the angle into [0; 2*Pi) range from its [-pi; +pi] range
return (flippedAngle >= 0) ? flippedAngle : flippedAngle + 2 * Math.PI;
}
}
关于java - 假设北角为零弧度,如何在极坐标和笛卡尔坐标之间进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576502/
一旦我看到了用C++进行某种假设的方法,例如: int x=7; assume (x==7);//if not right a red error will appear and program wi
我正在尝试测试我的数据库类。这是它的简化示例。 class Database: """ it has more methods but I show only the most important "
这只是一个思考练习,我会对任何意见感兴趣。尽管如果它有效,我可以想出一些我会使用它的方法。 传统上,如果你想对由数组或范围等形成的嵌套循环的结果执行一个函数,你会这样写: def foo(x, y)
当某些假设无效时,MSTest 是否有办法不运行测试?就像 JUnit 的“Assume.*”方法一样: //Setup Assume.assumeEquals(2, count); //Only r
为什么会出现这个警告?如果我检查边界,这并不是一个真正的假设。以及如何修复? 如果num_actions_to_skip设置为 1,而不是 2,错误消失。 谢谢 error: assuming sig
书理解和使用 C 指针 , by Richard Reese 说: The null concept is an abstraction supported by the null pointer c
所以我有两个假设,一个是 h : A -> B,另一个是 h2 : A。如何让 h3 : B 出现在我的假设中? 最佳答案 pose proof (h h2) as h3. 引入h3 : B作为新假设
我知道发生冲突的可能性很小,但如果我生成了一批 1000 个 GUID(例如),是否可以安全地假设它们都是唯一的以节省对每个 GUID 的测试? 奖励问题 测试 GUID 唯一性的最佳方法是什么?也许
这个问题已经有答案了: Jackson JSON: get node name from json-tree (5 个回答) 已关闭 7 年前。 我正在尝试迭代 JsonNode 树,并且我编写了以下
我无法弄清楚如何在 Sympy 中假设复数的正实部。Mathematica 代码示例: a = InverseFourierTransform[ R/(I omega - lambda) + Con
这个问题在这里已经有了答案: 关闭 14 年前。 重复: Do web sites really need to cater for browsers that don’t have Javascr
我使用hypothesis 已经有一段时间了。我想知道如何重用 @given parts。 我有一些大约 20 行,我将整个 @given 部分复制到几个测试用例之上。 一个简单的测试例子 @give
您好,我的 C++ 代码中有一个错误。我有 2 个 .cpp 文件和 1 个 .h 文件,我试图从头文件访问 5 个字符串和 1 个 int,但我收到一条错误消息,提示“缺少显式类型(假设为‘int’
我正在尝试使用 IAR 开发一个项目。这是错误消息:错误 [Pe260]:缺少显式类型(假定为“int”) 问候。 当我尝试:void send_data_byte(unsigned char dat
我正在处理一个数组,我想在其中添加它的一些值。在某些时候,为了仅通过一次计算即可完成此操作,它会要求数组外的索引。 有没有办法说,“如果索引在数组之外,则假定值为 0”? 有点像这样:
在 Python 2 中,我想评估一个包含文字表示的字符串。我想安全地执行此操作,所以我不想使用 eval()——相反,我已经习惯了使用 ast.literal_eval()的任务。 但是,我还想在纯
我正在对时间进行大量计算,通过添加秒数来构建相对于其他时间对象的时间对象。该代码应该在嵌入式设备和服务器上运行。大多数文档都说 time_t 是某种算术类型,通常存储自纪元以来的时间。假设 time_
我正在编写一个程序,其中大多数使用的库函数返回-1 并设置错误号。程序的行为是在发生错误时退出。要从程序外部确定确切的退出点和错误(例如使用 gdb),我想使用以下方法: err = func_1(.
这是我今天考试的一道题: 在 C 中,假设指针是严格类型化的(即,指向 int 的指针不能用于指向 char)。这会降低它的表达能力吗?如果不是,您为什么以及如何补偿此限制?如果是,如何?您还需要添加
我将星期几存储在数据库中,其中星期日 = 1,星期一 = 2 等。 在数据库查询中,我需要将日期转换为 System.DayOfWeek。 根据 MSDN : The value of the con
我是一名优秀的程序员,十分优秀!