发布时间 : 星期五 文章OpenGL ES教程十二更新完毕开始阅读d4baf1305a8102d276a22fda
CGPoint touchPos = [t locationInView:t.view];
// Determine the location on the screen. We are interested in iPhone
// Screen co-ordinates only, not the world co-ordinates // because we are just trying to handle movement. // // (0, 0) // +-----------+ // | | // | 160 | // |-----------| 160 // | | | // | | | // |-----------| 320 // | | // | |
// +-----------+ (320, 480) //
if (touchPos.y < 160) { // We are moving forward
currentMovement = MTWalkForward;
} else if (touchPos.y > 320) { // We are moving backward
currentMovement = MTWAlkBackward;
} else if (touchPos.x < 160) { // Turn left
currentMovement = MTTurnLeft; } else { // Turn Right
currentMovement = MTTurnRight; } }
在玩家点击屏幕的任意位置后,就会将运动状态设置为指定的状态,同时,在松手时释放这样的状态:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
currentMovement = MTNone; }
这些时方法的实现。最后,我们需要一个来处理这些触摸事件,这一次,我们需要添加一个方法声明的接口,回到头文件,添加下面的方法:
- (void)handleTouches;
然后,切换回来,实现这个方法,就是这个方法,我们要通过计算来执行我们运动的3d世界。
3D空间的移动理论
让我们来看看第一次走路。当用户告诉我们向前走,我们需要了解的不仅是我们朝向的位置,同时还要了解我们的目标位置。这个朝向位置就是我们的当前位置,而看到的目标就是我们的目标位置,我们需要从当前位置向目标位置移动。
一图胜千言,下面的图片很好的说明了我们的当前位置和我们预定的目标位置。
在运动函数里面,我们知道这两点之间,x及z坐标的距离。我们要做的就是,将当前坐标乘以”速度”获得新的x,z坐标。像这样: