关于BOX2D的那些事儿(4)

http://hi.baidu.com/goodlad/blog/item/cd26b53d807de516bba167fd.html
Distance Joint是一种用来连接两个刚体的有距线段关节。你使用它的时候必须分别给两个刚体指定两个锚点,这两个点意味着此关节的长度。

  b2DistanceJointDef的结构

  struct b2DistanceJointDef : public b2JointDef

  {

  b2Vec2 anchorPoint1;

  b2Vec2 anchorPoint2;

  };

  继承于b2JointDef,只是多了两个锚点。

  下面是此关节定义的一个应用:

  b2DistanceJointDef jointDef;

  jointDef.body1 = myBody1;

  jointDef.body2 = myBody2;

  jointDef.collideConnected = true;

  jointDef.anchorPoint1 = myBody1->GetCenterPosition();

  jointDef.anchorPoint2 = myBody2->GetCenterPosition();

  参照Web例子,我们会发现定义一个关节其实也很简单

  1、指定关节定义

  2、创建关节

  3、结束时销毁关节

  七、 Chain、Bridge、Cradle

  Chain、Bridge两个例程是对Revolute Joint的应用,Revolute Joint是两个刚体共用一个锚点,它有一个自由度,在这里被叫作关节角度。

  为了指定一个Revolute你必须提供两个刚体和一个锚点,引擎会假定这两个刚体已经在正确位置上。

  它的结构如下:

  struct b2RevoluteJointDef : public b2JointDef

  {

  b2Vec2 anchorPoint;

  float32 lowerAngle;

  float32 upperAngle;

  float32 motorTorque;

  float32 motorSpeed;

  bool enableLimit;

  bool enableMotor;

  };

  它也是继承于b2JointDef,anchorPoint是锚点,lowerAngle为转动角底限,upperAngle为转动角上限,其他的这里暂时先不介绍。

  Chain、Bridge两个例子非常简单,和Distance Joint相差不大,算是对Joint使用的再次巩固。

  八、 Pulleys

  Pulleys是对Prismatic Joint、Pulley Joint的应用,Prismatic Joint是一种允许两个刚体沿指定轴相对移动的关节,不允许相对旋转,所以有一个自由度。

  它的结构下:

  struct b2PrismaticJointDef : public b2JointDef

  {

  b2Vec2 anchorPoint;

  b2Vec2 axis;

  float32 lowerTranslation;

  float32 upperTranslation;

  float32 motorForce;

  float32 motorSpeed;

  bool enableLimit;

  bool enableMotor;

  };

  我们这时暂时只应用到anchorPoint,axis,axis为轴,lowerTranslation为移动底限,upperTranslation为移动上限,而其他几项我们在Joint motor块讨论。

  Pulley Joint用来创建理想滑轮,滑轮连接两个刚体,一个上去,一个便下来,根据你的最初设定来决定你的绳长。

  length1 + length2 ==常数C

  你可以应用一个比例关系来模拟滑车装置,这会导致一边伸展得比另一边快,同时约束力也是一边大一边小,你可以用它来创建杠杆。

  length1 + ratio * length2 == 常数C

  举个例子,如果ratio(比例关系)是2,那么length1会变成length2的两倍,当作用在附加在刚体1上的绳上的力将会是作用在附加在刚体2上的力的一半。

  struct b2PulleyJointDef : public b2JointDef

  {

  b2Vec2 groundPoint1;

  b2Vec2 groundPoint2;

  b2Vec2 anchorPoint1;

  b2Vec2 anchorPoint2;

  float32 maxLength1;

  float32 maxLength2;

  float32 ratio;

  };

  groundPoint1、groundPoint2是刚体1、2上面绳子的顶点,anchorPoint1、anchorPoint2是刚体与绳子连接的点,maxLength1、maxLength2为两刚体的最大长度,ratio是比例系数。

  九、 Gears

  Gears内容里面有关于Revolute Joint、Prismatic Joint、Gear Joint的应用,前两种已经在前面讨论过,现在我们先看Gear Joint。

  Gear Joint直接翻译为齿轮关节,顾名思义就是用来处理齿轮类物体的相互关联。

  你在使用Gear Joint的时候必须先有一个附加了Prismatic Joint(此关节连接你的刚体和你的包容盒)的刚体和一个附加了Revolute Joint(此关节连接你的刚体和你的包容盒)的刚体咬合在一起。然后再用Gear Joint把这两个刚体连接到一起。

  和Pulley Joint一样也有ratio值,在这里这个值可以是负数,记住我们两必要关节一个是Revolute Joint,另一个是Prismatic Joint,所以

  coordinate1 + ratio * coordinate2 == 常数C

  这个例子已经应用了ratio值,你可以自己动手调调,来看看效果和作用。

  Gear Joint依赖于两个子关节,一般是在两个子关节之前删除,甚至于是在所有有关刚体被删除之前删除。

  结构如下:

  struct b2GearJointDef : public b2JointDef

  {

  b2Joint* joint1;

  b2Joint* joint2;

  float32 ratio;

  };

  继承于关节定义, joint1、joint2表示两个定义在刚体上面的关节,ratio表示比例关系。

  十、Joint(附加)

  其实上面讲了那么多种关节,但是我们都还没有真正讨论过joint,joint这里翻译为关节,它的结构为

  b2JointType m_type;

  b2Joint* m_prev;

  b2Joint* m_next;

  b2JointNode m_node1;

  b2JointNode m_node2;

  b2Body* m_body1;

  b2Body* m_body2;

  bool m_islandFlag;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值