当前位置: 移动技术网 > 移动技术>移动开发>IOS > ios实现简单随便移动的AR功能

ios实现简单随便移动的AR功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

先上个最终的效果动态图,

1、首先我们要自定义一个相机界面,可以用avcapturesession来自定义,不需要其他按钮,只有一个预览的界面;

2、我们要画一个简单的雷达图,可以用cgcontextref来简单实现,雷达图用来显示你跟你附近的用户(物体)的距离,通过跟实际物体的经纬度来算两点之间的距离,通过一定的比例来映射到雷达图上,两点之间的距离计算公式如下:

//两点的经纬度计算距离 
-(float) distancefromcoordinates:(cllocationcoordinate2d) mydot other:(cllocationcoordinate2d)otherdot 
{ 
  
 double earth_radius = 6378137.0; 
  
 double radlat1 = (mydot.latitude * m_pi / 180.0); 
 double radlat2 = (otherdot.latitude * m_pi / 180.0); 
 double a = radlat1 - radlat2; 
 double b = (mydot.longitude - otherdot.longitude) * m_pi / 180.0; 
 double s = 22 * asin(sqrt(pow(sin(a / 2), 2) 
        + cos(radlat1) * cos(radlat2) 
        * pow(sin(b / 2), 2))); 
 s = s * earth_radius; 
 s = round(s * 10000) / 10000; 
  
 return s; 
  
} 

要算物体在雷达图上的显示位置,根据三角函数,sina=对边/斜边,cosa=邻边/斜边,斜边我们已经有了,就是两点之间的距离,那么我们就需要知道一个角度,才能算出一条边,通过这条边跟半径的加减,就可以算出这个物体在雷达图上的位置。所以我们先要算两点的方位角,看下面的一张图:

这个维基上的一张方位角的解释图,我们可以同通过tan2函数来计算,公式如下:

- (float)getheadingfordirectionfromcoordinate:(cllocationcoordinate2d)fromloc tocoordinate:(cllocationcoordinate2d)toloc 
{ 
 float flat = degreestoradians(fromloc.latitude); 
 float flng = degreestoradians(fromloc.longitude); 
 float tlat = degreestoradians(toloc.latitude); 
 float tlng = degreestoradians(toloc.longitude); 
  
 float degree = radianstodegrees(atan2(sin(tlng-flng)*cos(tlat), cos(flat)*sin(tlat)-sin(flat)*cos(tlat)*cos(tlng-flng))); 
  
 if (degree >= 0) { 
  return degree; 
 } else { 
  return (360+degree); 
 } 
 
} 

3、要实现雷达图跟随手机旋转而转动,这里我们要用到指南针的原理,通过cllocationmanager管理类,里面有个clheading类,我们可以实现指南针,看这个类的结构:


里面有真北,磁北,还有磁力值在x,y,z三轴上的磁力值,不过当我用到这三个值的时候,发现有问题,在前后移动手机的时候,发现这个值变化有停顿,如果用这个值来实现移动会导致不流畅,所以我又用了陀螺仪数据,通过cmmotionmanager这个管理类来获取手机移动摆动的角度,用来计算手机前后移动的时候,物体在手机界面上下的位置。

-(void) startmotion 
{ 
  
 if (![_mgr isdevicemotionactive] && [_mgr isdevicemotionavailable]) 
 { 
   
 //设置采样间隔 
  _mgr.devicemotionupdateinterval = 0.1; 
 
 nsoperationqueue *queue = [[nsoperationqueue alloc] init]; 
 
  [_mgr startdevicemotionupdatestoqueue:queue 
   withhandler:^(cmdevicemotion * _nullable motion, 
      nserror * _nullable error) { 
          
      double gravityx = motion.gravity.x; 
      double gravityy = motion.gravity.y; 
      double gravityz = motion.gravity.z; 
          
          
   if (gravityy<=0 && gravityy>=-1) 
   { 
           
 //获取手机的倾斜角度(ztheta是手机与水平面的夹角, xytheta是手机绕自身旋转的角度): 
  ztheta = atan2(gravityz,sqrtf(gravityx*gravityx+gravityy*gravityy))/m_pi*180.0; 
 
} 
  [[nsoperationqueue mainqueue] addoperationwithblock:^{     
    [self updatapoint]; 
 
    }]; 
       
  //[self performselectoronmainthread:@selector(updatapoint) withobject:nil waituntildone:no]; 
      
  }]; 
     
 } 
} 


4、通过计算角度区间来显示手机上的物体显示还隐藏,也就是说在雷达图上的点进入扇形可见的区域就显示出物体并且移动,超出就隐藏起来。还有一点,就是要算碰撞检测的手机上物体与物体如果太多,就不能叠在一起,通过错位错开来,可以通过cgrectintersectsrect来写个算法检测两个矩形是否碰到了。

简单说了下我实现的原理,当然实际做的时候会遇到很多问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网