在我的iOS应用程序中,我有一个用cocos3d创建的3D对象。 它在x和y轴周围正确移动。但它不想绕z移动! 图片展示了它如何随着黑色箭头移动,以及它如何与红色箭头一起移动。
来自scene.m的代码
-(void) touchEvent: (uint) touchType at: (CGPoint) touchPoint
{
switch (touchType) {
case kCCTouchBegan:
break;
case kCCTouchMoved:
[self rotateMainNodeFromSwipeAt: touchPoint];
break;
case kCCTouchEnded:
break;
default:
break;
}
// For all event types, remember where the touchpoint was, for subsequent events.
_lastTouchEventPoint = touchPoint;
}
#define kSwipeScale 0.6
-(void) rotateMainNodeFromSwipeAt: (CGPoint) touchPoint
{
CC3Camera *camera = self.activeCamera;
// Get the direction and length of the movement since the last touch move event,
// in 2D screen coordinates. The 2D rotation axis is perpendicular to this movement.
CGPoint swipe2d = ccpSub(touchPoint, _lastTouchEventPoint);
CGPoint axis2d = ccpPerp(swipe2d);
// Project the 2D axis into a 3D axis by mapping the 2D X & Y screen coords
// to the camera's rightDirection and upDirection, respectively.
CC3Vector axis = CC3VectorAdd(CC3VectorScaleUniform(camera.rightDirection, axis2d.x),
CC3VectorScaleUniform(camera.upDirection, axis2d.y));
//CC3Vector axis = CC3VectorScaleUniform(camera.upDirection, axis2d.y);
GLfloat angle = ccpLength(swipe2d) * kSwipeScale;
// Rotate the node under direct finger control, by directly rotating by the angle
// and axis determined by the swipe.
[self.modelNode rotateByAngle:angle aroundAxis:axis];
}
为什么它不想绕z轴移动?
答案 0 :(得分:0)
您的意思是想围绕相机的Z轴旋转节点吗?如果是这样,您上面导出的axis
(与CC3DemoMashUpScene
中的代码相同)会使用相机的rightDirection
和upDirection
来创建根据定义,axis
垂直于相机的Z轴。 rightDirection
和upDirection
定义了一个垂直于相机指向方向的平面,因此这些向量的任何线性组合都将产生一个位于该平面内的向量。
要围绕Z轴创建旋转,axis
必须包含一些Z分量。相机的forwardDirection
可以提供此Z轴组件。对于在相机的Z轴周围仅旋转 的非常简单的示例,请尝试将axis
设置为:
CC3Vector axis = CC3VectorScaleUniform(cam.forwardDirection, axis2d.x);
或axis2d.x
和axis2d.y
的某种组合。