[英]Revolute Joints in box2d
我一直在嘗試按照此處的教程來構建旋轉關節。 本質上是一條手臂。 我需要將手臂(一個矩形)固定到正方形上的某個點並圍繞它旋轉。 施加力時,我希望這兩個形狀像一個一樣,固定在一起,而手臂像布娃娃一樣在盒子周圍飛來飛去。
不幸的是,這根本不起作用。 首先將手臂連接起來,然后當我施加力時,兩個形狀分開並以我無法解釋的奇怪方式表現出來。 它幾乎好像錨在一些不穩固的位置上。
我也在使用jbox2d和本教程中的很多代碼。
(我已經將初始錨點設置到中心,只是為了看看它是否可以工作)(有一些奇怪的轉換,因為我使用的是openGl)
這是要點:
public class New_char
{
Vec2 torso_pos, arm_pos;
Body torso, arm;
PolygonShape torso_shape, arm_shape;
BodyDef torso_def, arm_def;
FixtureDef torso_fix, arm_fix;
RevoluteJointDef torsoArmDef;
RevoluteJoint torsoArmJoint;
//float[] torSize = {0.5f, 0.5f}, armSize={0.75f, 0.10f};
public New_char(World world, float[] pos)
{
//this.torso_pos = new Vec2(pos[0], pos[1]) ; this.arm_pos = new Vec2(pos[0]+10,pos[1]+10);
//out.println(this.arm_pos+" thepos "+this.torso_pos);
this.torso_def = new BodyDef() ; this.arm_def = new BodyDef();
torso_def.type = BodyType.DYNAMIC ; arm_def.type = BodyType.DYNAMIC;
torso_def.position.set(320 / 30 / 2, 240 / 30 / 2) ; arm_def.position.set(320 / 30 / 2, 240 / 30 / 2);
this.torso_shape = new PolygonShape() ; this.arm_shape = new PolygonShape();
this.torso_shape.setAsBox(0.50f, 0.50f) ; this.arm_shape.setAsBox(0.75f, 0.10f);
this.torso_fix = new FixtureDef() ; this.arm_fix = new FixtureDef();
this.torso_fix.density = 0.1f ; this.arm_fix.density = 0.5f;
this.torso_fix.shape = this.torso_shape ; this.arm_fix.shape = this.arm_shape;
this.torso = world.createBody(this.torso_def) ; this.arm = world.createBody(this.arm_def);
this.torso.createFixture(this.torso_fix) ; this.arm.createFixture(this.arm_fix);
this.torsoArmDef = new RevoluteJointDef();
this.torsoArmDef.bodyA = this.torso ; this.torsoArmDef.bodyB = this.arm;
this.torsoArmDef.collideConnected = false;
this.torsoArmDef.localAnchorA.set(this.torso.getWorldCenter());
//Vec2 armpin = new Vec2(1f, 1f);
this.torsoArmDef.localAnchorB.set(this.arm.getWorldCenter());
this.torsoArmJoint = (RevoluteJoint)world.createJoint(this.torsoArmDef);
問題是getWorldCenter(在我發現的每個教程中都推薦使用)
解決方案是getLocalCenter:
this.torsoArmDef.localAnchorA.set(this.torso.getLocalCenter());
this.torsoArmDef.localAnchorB.set(this.arm.getLocalCenter());
現在,它神奇地旋轉!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.