Bullet之了解Force Torque Impulse

本文介绍了Bullet物理引擎中力的概念,包括力(Force)、扭矩(Torque)和冲量(Impulse)的作用方式及实现原理。通过源码分析,解释了如何通过不同的方法改变刚体的运动状态。

Bullet之了解Force Torque Impulse

分类: cocos2d-x 物理引擎 167人阅读 评论(0) 收藏 举报

在了解Force Impulse Torque 之前,先来看看setLinearVelocity,setAngularVelocity

一个是线速度,一个是角速度

理解起来是很简单的设置body的线速度和角速度

  1. // 线速度  
  2. const btVector3 &   getLinearVelocity () const  
  3. void    setLinearVelocity (const btVector3 &lin_vel)  
  4.   
  5. // 角速度  
  6. const btVector3 &   getAngularVelocity () const  
  7. void    setAngularVelocity (const btVector3 &ang_vel)  

现在来了解Force

  1. void    applyCentralForce (const btVector3 &force)  
  2. void    applyForce (const btVector3 &force, const btVector3 &rel_pos)  

applyCentralForce 给body提供一个作用力,查看源码

  1. void applyCentralForce(const btVector3& force)  
  2. {  
  3. <span style="white-space:pre">    </span>m_totalForce += force*m_linearFactor;  
  4. }  

可知在body原有作用力的基础上再加force*m_linearFactor,至于这个m_linearFactor

  1. void setLinearFactor(const btVector3& linearFactor)  
  2. {  
  3.     m_linearFactor = linearFactor;  
  4.     m_invMass = m_linearFactor*m_inverseMass;  
  5. }  

就是对施加的力三个方向各缩放一定的倍数。

 

applyForce 同样是给body提供一个作用力,

  1. void    applyForce(const btVector3& force, const btVector3& rel_pos)   
  2. {  
  3.     applyCentralForce(force);   // 首先直接施加力  
  4.     applyTorque(rel_pos.cross(force*m_linearFactor));   // 然后施加扭转力  
  5. }  

向量的叉乘得到垂直于这两个向量的另一个向量

rel_pos.cross(force*m_linearFactor)可以得到扭转力

假设force = (0, 0, -10), rel_pos = (0, 1, 0),

Force * rel_pos = (-10, 0, 0),得到绕x轴的扭转力

 

记住applyForce并不是单独施加力

 

现在来看Torque

前面的applyForce已经提到过,提供一个扭转力

  1. void    applyTorque(const btVector3& torque)  
  2. {  
  3.     m_totalTorque += torque*m_angularFactor;  
  4. }  

假设torque = (10, 5, -10)

就是绕torque.normalize这个轴旋转。

 

现在来了解Impulse

  1. void applyCentralImpulse(const btVector3& impulse)  
  2. {  
  3.     m_linearVelocity += impulse *m_linearFactor * m_inverseMass;  
  4. }  
  5.       
  6. void applyTorqueImpulse(const btVector3& torque)  
  7. {  
  8.     m_angularVelocity += m_invInertiaTensorWorld * torque * m_angularFactor;  
  9. }  
  10.       
  11. void applyImpulse(const btVector3& impulse, const btVector3& rel_pos)   
  12. {  
  13.     if (m_inverseMass != btScalar(0.))  
  14.     {  
  15.         applyCentralImpulse(impulse);  
  16.         if (m_angularFactor)  
  17.         {  
  18.             applyTorqueImpulse(rel_pos.cross(impulse*m_linearFactor));  
  19.         }  
  20.     }  
  21. }  

把所有的Impulse都列了出来,关于冲量的介绍可以看百科。

既然要用,简单理解impulse * 质量的倒数 = 增加的速度(线速度或者角速度)

看源码就知道了applyCentralImpulseapplyImpulse就跟applyCenterForce 和 applyForce 差不多

 

如果要用Force使body移动就要每帧都去施加力,Impulse是瞬间提供一个速度,只在需要时施加

对于set****Factor只有在apply***时才使用,而set***Velocity不使用。

 

void clearForces ()

清除施加的Force和Torque,但body还是会受重力的影响。

 

_ballBody->setActivationState(ACTIVE_TAG);

当然在施加作用力时,要将body的状态设置为活动,只有活动的对象才能被模拟,这也是优化的一个方面。


没有源码,没有示例,只是简单了解,不足很多,只为学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值