Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将线性脉冲应用于推送点-Android上的Box2D_Android_Iphone_Box2d_Jbox2d - Fatal编程技术网

将线性脉冲应用于推送点-Android上的Box2D

将线性脉冲应用于推送点-Android上的Box2D,android,iphone,box2d,jbox2d,Android,Iphone,Box2d,Jbox2d,我想得到一个形状,从中底移动到我接触的点。问题是解决与接触点的角度(或度?) float angle = ? float power = calculatePower(touchY); Vec2 impulse = new Vec2(angle, power); Vec2 point = body.getWorldCenter(); // to prevent rotation of shape body.applyLinearImpulse(impulse, point); 有人有什么建议吗

我想得到一个形状,从中底移动到我接触的点。问题是解决与接触点的角度(或度?)

float angle = ?
float power = calculatePower(touchY);
Vec2 impulse = new Vec2(angle, power);
Vec2 point = body.getWorldCenter(); // to prevent rotation of shape
body.applyLinearImpulse(impulse, point);
有人有什么建议吗

编辑:已解决

感谢安德鲁斯的回答。以下是工作代码:

Point delta = new Point(touchX - bodyX, touchY - bodyY);
double angle = Math.atan2(delta.y, delta.x);
Vec2 direction = new Vec2((float)Math.cos(angle), (float)-Math.sin(angle));
float power = calculatePower(touchY);
Vec2 impulse = new Vec2(power * direction.x, power * direction.y);
Vec2 point = body.getWorldCenter();
body.applyLinearImpulse(impulse, point);

伪代码如下所示
atan2
是标准的数学函数

Vec2 delta  = touchPoint - body.position;
float angle = math.atan2(delta.y, delta.x);
该行
Vec2脉冲=新的Vec2(角度、功率)看起来很奇怪。您使用的是
angle
作为
x
值和
power
作为
y
。这没有多大意义。我认为你的目标是在角度方向上运用力量的冲动。在这种情况下,代码应该如下所示:

Vec2 dir     = Vec2(math.cos(angle), math.sin(angle));
Vec2 impulse = power * dir;
但是由于
delta
dir
仅在幅值上不同,因此可以将脉冲计算简化为:

Vec2 delta   = touchPoint - body.position;
Vec2 dir     = delta.normalize();
Vec2 impulse = power * dir

我正在用
Vec2脉冲=power*dir
进行测试。Vec2不是一个浮点数。如何实现这个
Vec2脉冲=新的Vec2(?,)
?@arcone:向量支持标量乘法。但我不能100%确定jbox2d是否支持它。作为一种解决方法,您可以这样做:
Vec2 pulse=new Vec2(power*dir.x,power*dir.y)