libgdx - 在圆形路径中移动 body
libgdx - moving a body in a circular path
我正在尝试围绕另一个 body 进行圆周运动。从现在开始我在两个物体之间有一个 DistanceJoint
但我的问题是如何找到正确的方向来施加我的力。
我试过这个功能,但显然不起作用:
public void updateCircular(float speed, Vector2 center){
Vector2 radius = center.sub(this.body.getPosition());
Vector2 force = radius.rotate90(1).nor().scl(speed);
this.body.setLinearVelocity(force.x, force.y);
}
center
是我的静态body的锚点,这个函数是我移动的body的class调用的,其实我的想法是调用这个渲染部分中的方法,所以每次我都会将两个物体之间的距离向量旋转 90 度以找到切线向量。
这似乎是个坏主意,那么我如何才能在每一帧中找到切线向量以便将我的第一个 object 移动到第二个周围?
Vector2中心每帧都在修改,你需要复制一份。
public void updateCircular(float speed, Vector2 center){
Vector2 radius = center.cpy().sub(this.body.getPosition());
Vector2 force = radius.rotate90(1).nor().scl(speed);
this.body.setLinearVelocity(force.x, force.y);
}
我正在尝试围绕另一个 body 进行圆周运动。从现在开始我在两个物体之间有一个 DistanceJoint
但我的问题是如何找到正确的方向来施加我的力。
我试过这个功能,但显然不起作用:
public void updateCircular(float speed, Vector2 center){
Vector2 radius = center.sub(this.body.getPosition());
Vector2 force = radius.rotate90(1).nor().scl(speed);
this.body.setLinearVelocity(force.x, force.y);
}
center
是我的静态body的锚点,这个函数是我移动的body的class调用的,其实我的想法是调用这个渲染部分中的方法,所以每次我都会将两个物体之间的距离向量旋转 90 度以找到切线向量。
这似乎是个坏主意,那么我如何才能在每一帧中找到切线向量以便将我的第一个 object 移动到第二个周围?
Vector2中心每帧都在修改,你需要复制一份。
public void updateCircular(float speed, Vector2 center){
Vector2 radius = center.cpy().sub(this.body.getPosition());
Vector2 force = radius.rotate90(1).nor().scl(speed);
this.body.setLinearVelocity(force.x, force.y);
}