void TransformHelp::matrixToNode(const CCAffineTransform &matrix, Node &node) { /* * In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform * but not used the tx, ty value. we simulate the function here */ helpPoint1.x = 0; helpPoint1.y = 1; helpPoint1 = CCPointApplyAffineTransform(helpPoint1, matrix); helpPoint1.x -= matrix.tx; helpPoint1.y -= matrix.ty; helpPoint2.x = 1; helpPoint2.y = 0; helpPoint2 = CCPointApplyAffineTransform(helpPoint2, matrix); helpPoint2.x -= matrix.tx; helpPoint2.y -= matrix.ty; node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f); node.skewY = atan2f(helpPoint2.y, helpPoint2.x); node.scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b); node.scaleY = sqrt(matrix.c * matrix.c + matrix.d * matrix.d); node.x = matrix.tx; node.y = matrix.ty; }
NoPL_FunctionValue Node::evaluateFunction(const char* functionName, const NoPL_FunctionValue* argv, unsigned int argc) { NoPL_FunctionValue returnVal; returnVal.type = NoPL_DataType_Uninitialized; if(node) { if(argc == 0) { if(!strcmp(functionName, "worldPositionX")) { returnVal.numberValue = node->convertToWorldSpaceAR(cocos2d::CCPointZero).x; returnVal.type = NoPL_DataType_Number; } else if(!strcmp(functionName, "worldPositionY")) { returnVal.numberValue = node->convertToWorldSpaceAR(cocos2d::CCPointZero).y; returnVal.type = NoPL_DataType_Number; } } else if(!strcmp(functionName, "hitTest")) { if(argc == 2 && argv[0].type == NoPL_DataType_Number && argv[1].type == NoPL_DataType_Number) { returnVal.booleanValue = (node->hitTest(cocos2d::CCPoint(argv[0].numberValue, argv[1].numberValue))); returnVal.type = NoPL_DataType_Boolean; } else if(argc == 1 && argv[0].type == NoPL_DataType_Pointer) { cocos2d::CCObject* arg = (cocos2d::CCObject*)argv[0].pointerValue; Node* otherNode = dynamic_cast<Node*>(arg); if(otherNode && otherNode->getCCNode()) { //get the transforms for each node cocos2d::CCAffineTransform thisTransform = node->nodeToWorldTransform(); cocos2d::CCAffineTransform nodeTransform = otherNode->getCCNode()->nodeToWorldTransform(); //create a rect for each node in the same coord system cocos2d::CCRect thisRect; thisRect.origin = CCPointApplyAffineTransform(cocos2d::CCPoint(0,0), thisTransform); thisRect.size = CCSizeApplyAffineTransform(node->getContentSize(), thisTransform); cocos2d::CCRect nodeRect; nodeRect.origin = CCPointApplyAffineTransform(cocos2d::CCPoint(0,0), nodeTransform); nodeRect.size = CCSizeApplyAffineTransform(otherNode->getCCNode()->getContentSize(), nodeTransform); //return the result of the hit test returnVal.booleanValue = thisRect.intersectsRect(nodeRect); returnVal.type = NoPL_DataType_Boolean; } } } } if(returnVal.type == NoPL_DataType_Uninitialized) return Node_Base::evaluateFunction(functionName, argv, argc); return returnVal; }
void GHBoneSkin::transform() { if(sprite == NULL || bone == NULL)return; float degrees = bone->degrees(); CCPoint posOffset = positionOffset; CCPoint bonePos = ccp(((GHBone*)bone->getParent())->getPosition().x, ((GHBone*)bone->getParent())->getPosition().y); CCAffineTransform transformOffset = CCAffineTransformTranslate(CCAffineTransformMakeIdentity(), bonePos.x, bonePos.y); CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformMakeIdentity(), CC_DEGREES_TO_RADIANS(degrees - angleOffset)); //we need 2 points //origin - upward point, in order to calculate new position and new angle CCPoint origin = ccp(0,0); CCPoint upward = ccp(0, -10); CCAffineTransform transform3 = CCAffineTransformRotate(CCAffineTransformMakeIdentity(), CC_DEGREES_TO_RADIANS(degrees - angleOffset - connectionAngle)); posOffset = CCPointApplyAffineTransform(posOffset, transform3); origin = CCPointApplyAffineTransform(origin, transform); upward = CCPointApplyAffineTransform(upward, transform); origin = CCPointApplyAffineTransform(origin, transformOffset); upward = CCPointApplyAffineTransform(upward, transformOffset); //now that we have the 2 points - lets calculate the angle float newAngle = (atan2(upward.y - origin.y, upward.x - origin.x)*180.0)/M_PI + 90.0; sprite->setPosition(ccp(origin.x + posOffset.x, origin.y - posOffset.y)); sprite->setRotation(newAngle); }
void MissileAttack::resetPosition() { CCPoint p(0, distance); CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformIdentity, angle); p = CCPointApplyAffineTransform(p, transform); missile->setRotation(-angle / M_PI * 180); missile->setPosition(p); p = CCPoint(0, 120); transform = CCAffineTransformRotate(CCAffineTransformIdentity, angle); p = CCPointApplyAffineTransform(p, transform); aim->setRotation(-angle / M_PI * 180); aim->setPosition(p); }
//------------------------------------------------------------------------------ void QLabel::CalculateSize( void) { CCPoint bottom_left( 0.0f, 0.0f); CCSize content_size = m_CCFontNode->getContentSize(); CCPoint top_right( content_size.width, content_size.height); CCAffineTransform to_parent = CCAffineTransformConcat(m_CCFontNode->nodeToParentTransform(), m_CCNode->nodeToParentTransform()); bottom_left = CCPointApplyAffineTransform( bottom_left, to_parent); top_right = CCPointApplyAffineTransform( top_right, to_parent); xText = bottom_left.x; yText = bottom_left.y; wText = top_right.x - bottom_left.x; hText = top_right.y - bottom_left.y; }
CCPoint CCNode::convertToWorldSpace(CCPoint nodePoint) { CCPoint ret; if(CC_CONTENT_SCALE_FACTOR() == 1) { ret = CCPointApplyAffineTransform(nodePoint, nodeToWorldTransform()); } else { ret = ccpMult( nodePoint, CC_CONTENT_SCALE_FACTOR() ); ret = CCPointApplyAffineTransform(ret, nodeToWorldTransform()); ret = ccpMult( ret, 1/CC_CONTENT_SCALE_FACTOR() ); } return ret; }
void NormalAttack::resetPosition() { CCPoint p(0, distance); CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformIdentity, angle); p = CCPointApplyAffineTransform(p, transform); sprite->setRotation(-angle / M_PI * 180); sprite->setPosition(p); }
CCRect CCRectApplyAffineTransform(const CCRect& rect, const CCAffineTransform& anAffineTransform) { float top = CCRect::CCRectGetMinY(rect); float left = CCRect::CCRectGetMinX(rect); float right = CCRect::CCRectGetMaxX(rect); float bottom = CCRect::CCRectGetMaxY(rect); CCPoint topLeft = CCPointApplyAffineTransform(CCPointMake(left, top), anAffineTransform); CCPoint topRight = CCPointApplyAffineTransform(CCPointMake(right, top), anAffineTransform); CCPoint bottomLeft = CCPointApplyAffineTransform(CCPointMake(left, bottom), anAffineTransform); CCPoint bottomRight = CCPointApplyAffineTransform(CCPointMake(right, bottom), anAffineTransform); float minX = std::min(std::min(topLeft.x, topRight.x), std::min(bottomLeft.x, bottomRight.x)); float maxX = std::max(std::max(topLeft.x, topRight.x), std::max(bottomLeft.x, bottomRight.x)); float minY = std::min(std::min(topLeft.y, topRight.y), std::min(bottomLeft.y, bottomRight.y)); float maxY = std::max(std::max(topLeft.y, topRight.y), std::max(bottomLeft.y, bottomRight.y)); return CCRectMake(minX, minY, (maxX - minX), (maxY - minY)); }
CCAffineTransform CCSkin::nodeToWorldTransformAR() { CCAffineTransform displayTransform = m_sTransform; CCPoint anchorPoint = m_obAnchorPointInPoints; anchorPoint = CCPointApplyAffineTransform(anchorPoint, displayTransform); displayTransform.tx = anchorPoint.x; displayTransform.ty = anchorPoint.y; return CCAffineTransformConcat(displayTransform, m_pBone->getArmature()->nodeToWorldTransform()); }
void CCDisplayFactory::updateDisplay(CCBone *bone, float dt, bool dirty) { CCNode *display = bone->getDisplayRenderNode(); CS_RETURN_IF(!display); switch(bone->getDisplayRenderNodeType()) { case CS_DISPLAY_SPRITE: if (dirty) { (static_cast<CCSkin*>(display))->updateArmatureTransform(); } break; case CS_DISPLAY_PARTICLE: updateParticleDisplay(bone, display, dt); break; case CS_DISPLAY_ARMATURE: updateArmatureDisplay(bone, display, dt); break; default: display->setAdditionalTransform(bone->nodeToArmatureTransform()); break; } #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX if (dirty) { CCDecorativeDisplay *decoDisplay = bone->getDisplayManager()->getCurrentDecorativeDisplay(); CCColliderDetector *detector = decoDisplay->getColliderDetector(); if (detector) { do { #if ENABLE_PHYSICS_BOX2D_DETECT CC_BREAK_IF(!detector->getBody()); #elif ENABLE_PHYSICS_CHIPMUNK_DETECT CC_BREAK_IF(!detector->getBody()); #endif CCAffineTransform displayTransform = display->nodeToParentTransform(); CCPoint anchorPoint = display->getAnchorPointInPoints(); anchorPoint = CCPointApplyAffineTransform(anchorPoint, displayTransform); displayTransform.tx = anchorPoint.x; displayTransform.ty = anchorPoint.y; CCAffineTransform t = CCAffineTransformConcat(displayTransform, bone->getArmature()->nodeToParentTransform()); detector->updateTransform(t); } while (0); } } #endif }
void CCDisplayFactory::updateDisplay(CCBone *bone, CCDecorativeDisplay *decoDisplay, float dt, bool dirty) { CS_RETURN_IF(!decoDisplay); #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT if (dirty) { CCColliderDetector *detector = decoDisplay->getColliderDetector(); if (detector) { do { #if ENABLE_PHYSICS_BOX2D_DETECT CC_BREAK_IF(!detector->getBody()); #elif ENABLE_PHYSICS_CHIPMUNK_DETECT CC_BREAK_IF(!detector->getBody()); #endif CCNode *node = decoDisplay->getDisplay(); CCAffineTransform displayTransform = node->nodeToParentTransform(); CCPoint anchorPoint = node->getAnchorPointInPoints(); anchorPoint = CCPointApplyAffineTransform(anchorPoint, displayTransform); displayTransform.tx = anchorPoint.x; displayTransform.ty = anchorPoint.y; CCAffineTransform t = CCAffineTransformConcat(displayTransform, bone->getArmature()->nodeToParentTransform()); detector->updateTransform(t); } while (0); } } #endif CCNode *display = decoDisplay->getDisplay(); switch(decoDisplay->getDisplayData()->displayType) { case CS_DISPLAY_SPRITE: updateSpriteDisplay(bone, display, dt, dirty); break; case CS_DISPLAY_PARTICLE: updateParticleDisplay(bone, display, dt, dirty); break; case CS_DISPLAY_ARMATURE: updateArmatureDisplay(bone, display, dt, dirty); break; default: { display->setAdditionalTransform(bone->nodeToArmatureTransform()); } break; } }
bool MissileAttack::enable() { if (distance < 120) { CCPoint p(0, -10); CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformIdentity, angle); p = CCPointApplyAffineTransform(p, transform); CCMoveBy * move = CCMoveBy::create(0.1f, p); CCActionInterval * rev = move->reverse(); CCAction * seq = CCSequence::create(move, rev, NULL); getParent()->runAction(seq); return false; } return true; }
bool MissileAttack::hit(cocos2d::CCSprite *player) { if (isHit) return false; CCPoint p1(0, distance - missile->getContentSize().height / 2); CCAffineTransform transform = CCAffineTransformRotate(CCAffineTransformIdentity, angle); p1 = CCPointApplyAffineTransform(p1, transform); CCPoint p2 = player->getPosition(); float dist = sqrtf(powf(p1.x - p2.x, 2) + powf(p1.y - p2.y, 2)); if (dist < player->getContentSize().width / 2) return isHit = true; return false; }