下面代码展示如何计算上图中箭头的旋转方向
- (void)updateRoute:(NSArray<LocalCoordinate *> *)route {
//route 中放的是LocalCoordinate 它有x和y坐标值 两个属性
//用于记录上一个坐标
LocalCoordinate *lcoordinate = [[LocalCoordinate allco] initwithX:0.0 y:0.0];
NSInteger count = route.count;
for (NSInteger i = 0; i < count; i++) {
@autoreleasepool {
//获取当前坐标点
LocalCoordinate *coordinate = route[i];
//使用向量计算夹角
CGFloat v_x = [coordinate x];
CGFloat v_y = [coordinate y];
if (lcoordinate) {
v_x = [coordinate x]-[lcoordinate x];
v_y = [coordinate y]-[lcoordinate y];
}
double angle = 0.0;
double m = sqrt(pow(v_x, 2)+pow(v_y, 2));
if (m != 0) {
angle = v_x<0?-acos(v_y/m):acos(v_y/m);
}
//----------这段是计算当前箭头的位置 不需要的话可以忽略----------
double x_2 = 0.0;
double y_2 = 0.0;
x_2 = ([coordinate x]+[lcoordinate x])/2.0;
y_2 = ([coordinate y]+[lcoordinate y])/2.0;
SCNVector3 vector3 = SCNVector3Make(self.floor.worldTransform.m41+x_2, self.floor.worldTransform.m42+0.01, self.floor.worldTransform.m43+y_2);
//----------这段是计算当前箭头的位置 不需要的话可以忽略----------
SCNNode *node = self.routeNodes[i];
node.position = vector3;
node.transform = SCNMatrix4Translate(node.transform, 0, 1.1, 0);
//设置欧拉角
node.eulerAngles = SCNVector3Make(0, angle, 0);
lcoordinate = coordinate;
}
}
}