kmMat4* hueRotation(kmMat4* m, float r) { kmMat4 mat0; kmMat4 mat1; kmMat4 temp; // Make an identity matrix. kmMat4Identity(&mat0); // Rotate the grey vector into positive Z. // Sin = 1/sqrt(2). // Cos = 1/sqrt(2). kmMat4RotationX(&temp, M_PI_4); kmMat4Multiply(&mat1, &temp, &mat0); // Sin = -1/sqrt(3). // Cos = sqrt(2/3). kmMat4RotationY(&temp, -0.615479709); kmMat4Multiply(&mat0, &temp, &mat1); // Shear the space to make the luminance plane horizontal. float lx, ly, lz; xformRGB(&mat0, rwgt, gwgt, bwgt, &lx, &ly, &lz); float zsx = lx / lz; float zsy = ly / lz; shearZMatrix(&temp, zsx, zsy); kmMat4Multiply(&mat1, &temp, &mat0); // Rotate the hue. float rad = r * M_PI / 180; kmMat4RotationZ(&temp, rad); kmMat4Multiply(&mat0, &temp, &mat1); // Unshear the space to put the luminance plane back. shearZMatrix(&temp, -zsx, -zsy); kmMat4Multiply(&mat1, &temp, &mat0); // Rotate the grey vector back into place. // Sin = 1/sqrt(3). // Cos = sqrt(2/3); kmMat4RotationY(&temp, 0.615479709); kmMat4Multiply(&mat0, &temp, &mat1); // Sin = -1/sqrt(2). // Cos = 1/sqrt(2). kmMat4RotationX(&temp, -M_PI_4); kmMat4Multiply(&mat1, &temp, &mat0); kmMat4Fill(m, mat1.mat); return m; }
static void dlUpdateMatrix( dlObject *object ) { kmMat4 translation, rotation, scale, temp; CALL("%p", object); /* translation */ kmMat4Translation( &translation, object->translation.x, object->translation.y, object->translation.z ); /* rotation */ kmMat4RotationX( &rotation, kmDegreesToRadians(object->rotation.x) ); kmMat4Multiply( &rotation, &rotation, kmMat4RotationY( &temp, kmDegreesToRadians(object->rotation.y) ) ); kmMat4Multiply( &rotation, &rotation, kmMat4RotationZ( &temp, kmDegreesToRadians(object->rotation.z) ) ); /* scale */ kmMat4Scaling( &scale, object->scale.x, object->scale.y, object->scale.z ); /* build matrix */ kmMat4Multiply( &translation, &translation, &rotation ); kmMat4Multiply( &object->matrix, &translation, &scale ); object->transform_changed = 0; }
bool Player::init() { if (!Node::init()) return false; _tank = Sprite3D::create("model/tank/tank_buttom.c3b"); NavMeshAgentParam param; param.radius = 2.0f; param.height = 8.0f; param.maxSpeed = 8.0f; _agent = NavMeshAgent::create(param); AgentUserData *data = new AgentUserData{ 0.0f }; _agent->setUserData(data); _tank->setScale(1.5f); _tank->addComponent(_agent); _tank->setPosition3D(Vec3(-40.0f, 1.0f, 30.0f)); _barrel = Sprite3D::create("model/tank/barrel.c3b"); _tank->addChild(_barrel); _barrel->setPosition3D(Vec3(0.30f, 2.0f, 1.6f)); _barrel->setAnchorPoint(ccp(0.5, 0.5)); _barrel->setScale(0.5f); float fRadSeed = 3.14159f / 180.0f; kmMat4 kMat; kmMat4Identity(&kMat); kmMat4RotationX(&kMat, 270 * fRadSeed); Quaternion quat(kMat); _barrel->setRotationQuat(quat); this->scheduleUpdate(); this->addChild(_tank); this->setCameraMask((unsigned short)CameraFlag::USER1); return true; }
void Player::setPlayerAngle(int angle) { float fRadSeed = 3.14159f / 180.0f; kmMat4 kMat; kmMat4Identity(&kMat); kmMat4RotationX(&kMat, (angleCount - angle) * fRadSeed); Quaternion quat(kMat); m_barrel->setRotationQuat(quat); }
bool Player::init() { m_rotationAngle = 0.f; if (!Node::init()) return false; if (m_sPlayerInfo.m_ID == 1) { m_tank = Sprite3D::create("model/boss.c3b"); } else { m_tank = Sprite3D::create("model/WoT_IS7/tank_buttom.c3b"); } NavMeshAgentParam param; param.radius = 2.0f; param.height = 8.0f; param.maxSpeed = 8.0f; m_agent = NavMeshAgent::create(param); // sprite->_agent->setOrientationRefAxes(Vec3(-1.0f, 0.0f, 1.0f)); AgentUserData *data = new AgentUserData{ 0.0f }; m_agent->setUserData(data); m_tank->setScale(1.5f); m_tank->addComponent(m_agent); // _tank->setTag(1); this->scheduleUpdate(); m_barrel = Sprite3D::create("model/WoT_IS7/barrel.c3b"); m_tank->addChild(m_barrel); m_barrel->setPosition3D(Vec3(0.30f, 2.0f, 1.6f)); m_barrel->setAnchorPoint(ccp(0.5, 0.5)); m_barrel->setScale(0.5f); m_tank->setPosition3D(Vec3(-40.0f, 1.0f, 30.0f)); float fRadSeed = 3.14159f / 180.0f; kmMat4 kMat; kmMat4Identity(&kMat); kmMat4RotationX(&kMat, 270 * fRadSeed); Quaternion quat(kMat); m_barrel->setRotationQuat(quat); //将坦克模型加载到Node节点 float width = m_barrel->getContentSize().width; float height = m_barrel->getContentSize().height; log("barrel width = %f height = %f", width, height); // this->addChild(_barrel); this->addChild(m_tank); // this->setPosition3D(Vec3(-40.0f, 1.0f, 30.0f)); this->setCameraMask((unsigned short)CameraFlag::USER1); return true; }
/** * Builds a rotation matrix from pitch, yaw and roll. The resulting * matrix is stored in pOut and pOut is returned */ kmMat4* kmMat4RotationYawPitchRoll(kmMat4* pOut, const kmScalar pitch, const kmScalar yaw, const kmScalar roll) { kmMat4 yaw_matrix; kmMat4RotationY(&yaw_matrix, yaw); kmMat4 pitch_matrix; kmMat4RotationX(&pitch_matrix, pitch); kmMat4 roll_matrix; kmMat4RotationZ(&roll_matrix, roll); kmMat4Multiply(pOut, &pitch_matrix, &roll_matrix); kmMat4Multiply(pOut, &yaw_matrix, pOut); return pOut; }
void CSpriteEx::rotateX(float degree) { static float fRadSeed = 3.14159f/180.0f; //创建个旋转矩阵 kmMat4 kMat; kmMat4Identity(&kMat); kmMat4RotationX(&kMat, degree*fRadSeed); Vertex3F* v[4] = {&_quad.bl.vertices, &_quad.br.vertices, &_quad.tl.vertices, &_quad.tr.vertices}; Vertex3F* vOri[4] = {&m_sQuadOri.bl.vertices, &m_sQuadOri.br.vertices, &m_sQuadOri.tl.vertices, &m_sQuadOri.tr.vertices}; //向量矩阵相乘 for(int i = 0; i < 4; ++i) { float x = kMat.mat[0]*vOri[i]->x + kMat.mat[4]*vOri[i]->y + kMat.mat[8]*vOri[i]->z + kMat.mat[12]; float y = kMat.mat[1]*vOri[i]->x + kMat.mat[5]*vOri[i]->y + kMat.mat[9]*vOri[i]->z + kMat.mat[13]; float z = kMat.mat[2]*vOri[i]->x + kMat.mat[6]*vOri[i]->y + kMat.mat[10]*vOri[i]->z + kMat.mat[14]; v[i]->x = x; v[i]->y = y; v[i]->z = z; } }
const kmMat4& Node::getNodeToParentTransform() const { if (_transformDirty) { // Translate values float x = _position.x; float y = _position.y; float z = _positionZ; if (_ignoreAnchorPointForPosition) { x += _anchorPointInPoints.x; y += _anchorPointInPoints.y; } // Rotation values // Change rotation code to handle X and Y // If we skew with the exact same value for both x and y then we're simply just rotating float cx = 1, sx = 0, cy = 1, sy = 0; if (_rotationZ_X || _rotationZ_Y) { float radiansX = -CC_DEGREES_TO_RADIANS(_rotationZ_X); float radiansY = -CC_DEGREES_TO_RADIANS(_rotationZ_Y); cx = cosf(radiansX); sx = sinf(radiansX); cy = cosf(radiansY); sy = sinf(radiansY); } bool needsSkewMatrix = ( _skewX || _skewY ); // optimization: // inline anchor point calculation if skew is not needed // Adjusted transform calculation for rotational skew if (! needsSkewMatrix && !_anchorPointInPoints.equals(Point::ZERO)) { x += cy * -_anchorPointInPoints.x * _scaleX + -sx * -_anchorPointInPoints.y * _scaleY; y += sy * -_anchorPointInPoints.x * _scaleX + cx * -_anchorPointInPoints.y * _scaleY; } // Build Transform Matrix // Adjusted transform calculation for rotational skew kmScalar mat[] = { cy * _scaleX, sy * _scaleX, 0, 0, -sx * _scaleY, cx * _scaleY, 0, 0, 0, 0, _scaleZ, 0, x, y, z, 1 }; kmMat4Fill(&_transform, mat); // XXX // FIX ME: Expensive operation. // FIX ME: It should be done together with the rotationZ if(_rotationY) { kmMat4 rotY; kmMat4RotationY(&rotY,CC_DEGREES_TO_RADIANS(_rotationY)); kmMat4Multiply(&_transform, &_transform, &rotY); } if(_rotationX) { kmMat4 rotX; kmMat4RotationX(&rotX,CC_DEGREES_TO_RADIANS(_rotationX)); kmMat4Multiply(&_transform, &_transform, &rotX); } // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { kmMat4 skewMatrix = { 1, (float)tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0, (float)tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; kmMat4Multiply(&_transform, &_transform, &skewMatrix); // adjust anchor point if (!_anchorPointInPoints.equals(Point::ZERO)) { // XXX: Argh, kmMat needs a "translate" method. // XXX: Although this is faster than multiplying a vec4 * mat4 _transform.mat[12] += _transform.mat[0] * -_anchorPointInPoints.x + _transform.mat[4] * -_anchorPointInPoints.y; _transform.mat[13] += _transform.mat[1] * -_anchorPointInPoints.x + _transform.mat[5] * -_anchorPointInPoints.y; } } if (_useAdditionalTransform) { kmMat4Multiply(&_transform, &_transform, &_additionalTransform); } _transformDirty = false; } return _transform; }