02 力反馈 OpenHaptics ConsoleExamples-FrictionlessSphere

本文目的,分析如何产生一个球面上的力(方向垂直于球面)

继续分析官方demo
在 FrictionlessSphere demo 中,创建了一个球面,由于球面各店的法向量均不相同,相对于上一个demo中平面的力计算要稍微复杂一点点。

力的方向

在这里插入图片描述

  • 首先判断装置位置与球心之间的距离,当距离小于半径时代表穿透
  • 穿透时计算球心指向装置位置的向量vec,单位化
  • 接着根据穿透的程度以及平面硬度(弹簧系数),由胡克定律得到最终的力

demo源码如下

其中 main 函数没有变化,只是回调函数的逻辑发生更改

HDCallbackCode HDCALLBACK FrictionlessSphereCallback(void *data)
{
   const double sphereRadius = 40.0;
   const hduVector3Dd spherePosition(0,0,0);
   // 硬度
   const double sphereStiffness = .25;

   hdBeginFrame(hdGetCurrentDevice());
  
   // device 位置.
   hduVector3Dd position;
   hdGetDoublev(HD_CURRENT_POSITION, position);
   
   // device 位置和球心距离
   double distance = (position-spherePosition).magnitude();

   // 当刺破球面时
   if (distance < sphereRadius)
   {
       // 计算穿刺距离
       double penetrationDistance = sphereRadius-distance;

       // 球心到位置方向向量
       hduVector3Dd forceDirection = (position-spherePosition)/distance;

       // 计算力
       double k = sphereStiffness;
       hduVector3Dd x = penetrationDistance*forceDirection;
       hduVector3Dd f = k*x;
       hdSetDoublev(HD_CURRENT_FORCE, f);
   }

   hdEndFrame(hdGetCurrentDevice());

   HDErrorInfo error;
   if (HD_DEVICE_ERROR(error = hdGetError()))
   {
       hduPrintError(stderr, &error, "Error during main scheduler callback\n");
       if (hduIsSchedulerError(&error))
       {
           return HD_CALLBACK_DONE;
       }        
   }
   return HD_CALLBACK_CONTINUE;
}
发布了17 篇原创文章 · 获赞 7 · 访问量 956

猜你喜欢

转载自blog.csdn.net/qq_29639589/article/details/100326031
02
今日推荐