HitTestResult* hitTestRectRound(RectBodyNode* a, RoundBodyNode* b) { Point ca = a->getCenter(); float wa = a->getWidth(); float ha = a->getHeight(); float aa = CC_DEGREES_TO_RADIANS(-a->getAngle()); float ra = a->getRadius(); Point cb = b->getCenter(); float rb = b->getRadius(); bool hit = isRoundCrossRect(cb, rb, ca, wa, ha, ccpForAngle(aa)); if(hit) { Point hp; float distance; Vector2 delta = ca - cb; float deltaLength = ccpLength(delta); hp = cb + delta/deltaLength * rb; distance = deltaLength - rb; return HitTestResult::create(HTRT_CROSS, hp, distance); } else { return HitTestResult::create(HTRT_NONE); } }
void CCCurl::update(float time) { // radius and angle float radius = m_fromRadius + time * (m_toRadius - m_fromRadius); float angle = m_initAngle + time * m_fDuration * m_angularVelocity; // set target position CCPoint v = ccpForAngle(angle) * radius; CCPoint p = ccpAdd(m_center, v); getTarget()->setPosition(p); }
//-------------------------------------------------------------------- void FKCW_Action_Curl::update(float time) { // 半径和角度 float radius = m_fromRadius + time * (m_toRadius - m_fromRadius); float angle = m_initAngle + time * m_fDuration * m_angularVelocity; // 设置目标点 CCPoint v = ccpForAngle(angle) * radius; CCPoint p = ccpAdd(m_center, v); getTarget()->setPosition(p); }
void Ball::collideWithPaddle(Paddle* paddle) { CCRect paddleRect = paddle->rect(); paddleRect.origin.x += paddle->getPosition().x; paddleRect.origin.y += paddle->getPosition().y; float lowY = CCRect::CCRectGetMinY(paddleRect); float midY = CCRect::CCRectGetMidY(paddleRect); float highY = CCRect::CCRectGetMaxY(paddleRect); float leftX = CCRect::CCRectGetMinX(paddleRect); float rightX = CCRect::CCRectGetMaxX(paddleRect); if (getPosition().x > leftX && getPosition().x < rightX) { bool hit = false; float angleOffset = 0.0f; if (getPosition().y > midY && getPosition().y <= highY + radius()) { setPosition( CCPointMake(getPosition().x, highY + radius()) ); hit = true; angleOffset = (float)M_PI / 2; } else if (getPosition().y < midY && getPosition().y >= lowY - radius()) { setPosition( CCPointMake(getPosition().x, lowY - radius()) ); hit = true; angleOffset = -(float)M_PI / 2; } if (hit) { float hitAngle = ccpToAngle(ccpSub(paddle->getPosition(), getPosition())) + angleOffset; float scalarVelocity = ccpLength(m_velocity) * 1.05f; float velocityAngle = -ccpToAngle(m_velocity) + 0.5f * hitAngle; m_velocity = ccpMult(ccpForAngle(velocityAngle), scalarVelocity); } } }
HitTestResult* hitTestRoundRect(RoundBodyNode* a, RectBodyNode* b) { Point ca = a->getCenter(); float ra = a->getRadius(); Point cb = b->getCenter(); float wb = b->getWidth(); float hb = b->getHeight(); float ab = CC_DEGREES_TO_RADIANS(-b->getAngle()); float rb = b->getRadius(); Point rrp; bool hit = isRoundCrossRect(ca, ra, cb, wb, hb, ccpForAngle(ab), &rrp); if( hit ) { float distance; float angle = ccpToAngle(rrp); Point hp = ccp(cos(angle), sin(angle)) * rb; if(hp.x > wb/2) hp.x = wb/2; else if(hp.x < -wb/2) hp.x = -wb/2; if(hp.y > hb/2) hp.y = hb/2; else if(hp.y < -hb/2) hp.y = -hb/2; hp = cb + ccpRotateByAngle(hp, POINT_ZERO, ab); distance = ccpDistance(ca, hp); return HitTestResult::create(HTRT_CROSS, hp, distance); } else { return HitTestResult::create(HTRT_NONE); } }