我正在创建一个可以在2D矩形房间周围反弹的弹跳物体。我已经阅读了关于使用velocityX和velocityY而不是法线方向角的另一个问题的答案。好的,听起来更容易,所以我实现了下面的数学方法。但是,有一点不同。我要求输入一个角度以及对象被击中的(水平/垂直)墙。
+(double) getDirection:(double) x1:(double) y1: (double) x2: (double) y2{
return (atan2(y2-y1,x2-x1) * 180) / 3.14;
}
+(double) getLengthDir_X:(double) dis: (double) dir{
return (dis*cos(dir));
}
+(double) getLengthDir_Y:(double) dis: (double) dir{
return (dis*sin(dir));
}
+ (float) getBouncedAngle: (float) dir: (int) HVVar{
if (dir < 0)dir+=360;
else if (dir >= 360)dir-=360;
NSLog(@"in dir %f", dir);
float initialVX = [self getLengthDir_X:100 :dir],
initialVY = [self getLengthDir_Y:100 :dir];
NSLog(@"x,y %f %f", initialVX, initialVY);
if (HVVar == 0){//horizontal wall
initialVX = -initialVX;
}else if (HVVar == 1){//vertical wall
initialVY = -initialVY;
}
NSLog(@"x,y %f %f", initialVX, initialVY);
float newDir = [self getDirection:0 :0 :initialVX :initialVY];
NSLog(@"dir : %f ---> dir : %f", dir, newDir);
return newDir;
}
但我得到了非常奇怪的结果。我有时会弄错角度。我错过了什么吗?
2009-08-13 23:31:19.854 X[2936:20b] in dir -0.049634
2009-08-13 23:31:19.854 X[2936:20b] x,y 99.876846 -4.961388
2009-08-13 23:31:19.855 X[2936:20b] x,y -99.876846 -4.961388
2009-08-13 23:31:19.857 X[2936:20b] dir : -0.049634 ---> dir : -177.246017
2009-08-13 23:31:21.220 X[2936:20b] in dir -177.246017
2009-08-13 23:31:21.220 X[2936:20b] x,y 25.124613 -96.792320
2009-08-13 23:31:21.221 X[2936:20b] x,y -25.124613 -96.792320
2009-08-13 23:31:21.222 X[2936:20b] dir : -177.246017 ---> dir : -104.604294
2009-08-13 23:31:21.253 X[2936:20b] in dir -104.604294
2009-08-13 23:31:21.253 X[2936:20b] x,y -59.644127 80.265671
2009-08-13 23:31:21.255 X[2936:20b] x,y 59.644127 80.265671
2009-08-13 23:31:21.256 X[2936:20b] dir : -104.604294 ---> dir : 53.411633
答案 0 :(得分:2)
你的sin和cos函数期望以弧度为单位的参数。你在学位上给他们论证。