IOS simple imitation casually walk AR function (principle)

The final effect of the first dynamic diagram,

IOS simple imitation casually walk AR function (principle)

~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~

1, first we want to customize a camera interface, you can use AVCaptureSession from the definition, do not need other buttons, only a preview of the interface;

2, we need to draw a simple radar map, you can use CGContextRef to achieve a simple, radar diagram is used to show you and your users in the vicinity (object) distance, with the actual object of latitude and longitude to calculate the distance between two points, by a certain proportion of mapping to the radar map, the distance between two points the calculation formula is as follows:

/ / calculate the distance of two points of latitude and longitude

– (float) DistanceFromCoordinates: (CLLocationCoordinate2D) myDotother: (CLLocationCoordinate2D) otherDot

{

DoubleEARTH_RADIUS =6378137.0;

DoubleradLat1= (myDot.latitude* M_PI /180.0);

DoubleradLat2= (otherDot.latitude* M_PI /180.0);

DoubleA = radLat1- radLat2;

Doubleb = (myDot.longitude- otherDot.longitude) /180.0 * M_PI;

Doubles =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;

Returns;

}

Is the object in the radar map on the display position according to the trigonometric function, on the edge of sinA= / cosA= / hypotenuse, adjacent side bevel, bevel we have, is the distance between two points, then we need to know a point of view, to get an edge through this edge radius with addition and subtraction, it you can calculate the object position in the radar on the map. So we have to calculate the azimuth of two points, look at the following picture:

IOS simple imitation casually walk AR function (principle)

An explanation of the azimuth of the wiki, we can use the tan2 function to calculate, the formula is as follows:

– (float) getHeadingForDirectionFromCoordinate: (CLLocationCoordinate2D) fromLoctoCoordinate: (CLLocationCoordinate2D) toLoc

{

FloatfLat = DegreesToRadians (fromLoc.latitude);

FloatfLng = DegreesToRadians (fromLoc.longitude);

FloattLat = DegreesToRadians (toLoc.latitude);

FloattLng = DegreesToRadians (toLoc.longitude);

Floatdegree = RadiansToDegrees (atan2 (sin (tLng-fLng) *cos (tLat), cos (fLat) *sin (tLat) -sin (fLat) *cos (tLat) *cos (tLng-fLng)));

If (degree > =0) {

Returndegree;

}else{

Return (360+degree);

}

}

3, to realize the radar map rotation and rotation follow the mobile phone, here we will use the compass principle, through the CLLocationManager management class, there is a CLHeading class, we can see the compass, the structure of this class:

IOS simple imitation casually walk AR function (principle)

There are north, North and magnetic values in X, y, Z three axis magnetic value, but when I use the three values when problems are found, when before and after the mobile phone, found that this value changes have stopped, if come to realize that moves is not smooth with this value, so I also use the gyroscope data, using the CMMotionManager management class to get the mobile phone swing angle, when used for mobile computing and mobile phone, mobile phone interface on the position of the object in the.

– (void) startMotion

{

If ([_mgrisDeviceMotionActive]! & & [_mgrisDeviceMotionAvailable])

{

/ / set the sampling interval

_mgr.deviceMotionUpdateInterval=0.1;

NSOperationQueue*queue = [[NSOperationQueuealloc]init];

[_mgrstartDeviceMotionUpdatesToQueue:queue

WithHandler:^ (CMDeviceMotion* _Nullable motion,

NSError* _Nullable error) {

DoublegravityX = motion.gravity.x;

DoublegravityY = motion.gravity.y;

DoublegravityZ = motion.gravity.z;

If (gravityY< =0& & gravityY> =-1)

{

The mobile phone gets / / tilt angle (zTheta angle, and the horizontal plane is mobile phone xyTheta mobile phone around its own rotation angle):

ZTheta (gravityZ, atan2 = sqrtf (gravityX*gravityX+gravityY*gravityY)) /M_PI*180.0;

}

[[NSOperationQueuemainQueue]addOperationWithBlock:^{

[selfupdataPoint];

}];

//[self performSelectorOnMainThread:@selector (updataPoint) withObject:nil waitUntilDone:NO];

}];

}

}

4, through the calculation of the angle interval to display the phone on the object display is also hidden, that is, in the radar map on the point into the fan-shaped visible area on the display of objects and moving, hidden beyond the. Another point is to calculate the collision detection of objects on the mobile phone if too much, can not be stacked together, through dislocation stagger, can pass the CGRectIntersectsRect to write an algorithm to detect two rectangular is met,

I simply realized the principle, of course, do a lot of practical problems,,,,

IOS simple imitation casually walk AR function (principle)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~