bool RigSegmentCollides(const Rig& targetRig, const CCPoint& newVertex, const CCPoint& oldVertex) { if(targetRig.size() < 3) { return false; } for(int v = 0; v < targetRig.size(); v++) { CCPoint edgeStart = targetRig[v]; CCPoint edgeEnd = targetRig[ (v+1) % targetRig.size() ]; if(edgeStart.equals(oldVertex) || edgeEnd.equals(oldVertex)) { continue; } if(ccpSegmentIntersect(edgeStart, edgeEnd, newVertex, oldVertex)) { return true; } } return false; }
bool AnimatedRain::CreatePath(CCPoint from, CCPoint to) { if (from.equals(to)) { isMoving = false; return false; } PathFinder *p = new PathFinder(); p->setDestination(to); p->setSource(from); if (path != NULL) { if (path->count() > 0) { path->removeAllObjects(); path->release(); path = NULL; } } path = p->makeRainPath(&from, &to); path->retain(); delete p; return true; }
//For pathfinding, the difference from the Red is that it calculate the next step of the player and predict his movement void BluEnemy::CalculatePath() { GameLevelMap* pLevelMap = PathPlanner::GetInstance()->GetLevelMap(); //Get the source and destination Points CCPoint sourceTileCoords = pLevelMap->tileCoordForPosition(mEntitySprite->getPosition()); CCPoint destTileCoords = pLevelMap->tileCoordForPosition(PathPlanner::GetInstance()->GetPlayerPosition()); /*** Predict the player movement, calculate the destination target using the player direction */ CCPoint tmpDestTileCoords = ccp(destTileCoords.x + PathPlanner::GetInstance()->GetPlayerDirection().x, destTileCoords.y - PathPlanner::GetInstance()->GetPlayerDirection().y); if(pLevelMap->isValidAndNotWallTile(tmpDestTileCoords)) { //if the next position of the player is valid, set as destination destTileCoords = tmpDestTileCoords; } this->mShortestPath = NULL; //check that there is a path if(sourceTileCoords.equals(destTileCoords)) return; //check if the touch is in a wall if(PathPlanner::GetInstance()->GetLevelMap()->IsWallAtTileCoords(destTileCoords)) return; mShortestPathArray = PathPlanner::GetInstance()->Start_A_Star(sourceTileCoords, destTileCoords); }
直线与线段是否相交.cpp ///@brief 判断线段与直线是否相交 /// /// ///@param[in] p0,p1--线段两个端点, q0,q1--直线两个点值 ///@pre ///@return true---在线段上, false---不在线段上 ///@retval ///@post bool LineMenu::straightLineIsIntersect(const CCPoint p0, const CCPoint p1, const CCPoint q0, const CCPoint q1) { /// 先判断q0与q1能否组成一条直线 if (!q0.equals(q1)) { /// 当q0 == p0 q1 == p1时,结果为0。 只需要判断线段是否跨立直线即可。 if (0 <= determinant(p0.x-q0.x, p0.y-q0.y, q1.x-q0.x, q1.y-q0.y) * determinant(q1.x-q0.x, q1.y-q0.y, p1.x-q0.x, p1.y-q0.y)) { CCLOG("straight line and segment line is intesect!"); return false; } CCLOG("straight line and segment line isn't intesect!"); return false; } CCLOG("straigth line cannnot be make up of q0 and q1!"); return false; }
void CCNode::setAnchorPoint(const CCPoint& point) { if( ! point.equals(m_obAnchorPoint)) { m_obAnchorPoint = point; m_obAnchorPointInPoints = ccp(m_obContentSize.width * m_obAnchorPoint.x, m_obContentSize.height * m_obAnchorPoint.y ); } }
void CCLens3D::setPosition(const CCPoint& pos) { if( !pos.equals(m_position)) { m_position = pos; m_bDirty = true; } }
void CCLens3D::setPoint(const CCPoint& point) { if( !point.equals(m_tPoint)) { m_tPoint = point; m_bDirty = true; } }
// 衝突更新 void MyShipInterface::_updateCollision( const CCPoint& in_rVec ) { MyShipObj* pObj = (MyShipObj*)getChildByTag( eTAG_CHILD_OBJ ); CCPoint pos = pObj->getPosition(); // 画面範囲内に収まるように補正する。 CrollCollision2DLine::SEGMENT seg1; { // 線分との交差判定でチェック seg1.x = pos.x; seg1.y = pos.y; seg1.vecX = in_rVec.x; seg1.vecY = in_rVec.y; } // 画面4点の交差チェック { CCSize objSize = pObj->getSize(); CCPoint crossPos = CCPointZero; CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); static CrollCollision2DLine::SEGMENT upperLine(origin.x, origin.y + objSize.height, visibleSize.width, 0); static CrollCollision2DLine::SEGMENT underLine(origin.x, origin.y + visibleSize.height - objSize.height, visibleSize.width, 0); static CrollCollision2DLine::SEGMENT leftLine(origin.x + objSize.width, origin.y, 0, visibleSize.height); static CrollCollision2DLine::SEGMENT rightLine(origin.x + visibleSize.width - objSize.width, origin.y, 0, visibleSize.height); // X軸方向の衝突処理 if( in_rVec.x < 0 ) { CrollCollision2DLine::Check(seg1, leftLine, &crossPos.x, &crossPos.y); } else if( 0 < in_rVec.x ) { CrollCollision2DLine::Check(seg1, rightLine, &crossPos.x, &crossPos.y); } // Y軸方向の衝突処理 if( in_rVec.y < 0 ) { CrollCollision2DLine::Check(seg1, upperLine, &crossPos.x, &crossPos.y); } else if( 0 < in_rVec.y ) { CrollCollision2DLine::Check(seg1, underLine, &crossPos.x, &crossPos.y); } // 交差位置に設定 if( crossPos.equals(CCPointZero) == false ) { pos = crossPos; } else { pos = pos + in_rVec; } pObj->setPosition(pos); } }
bool PopStar::isInitFinish() { Star* star = stars[ROW_NUM-1][COL_NUM-1]; if (star) { CCPoint pos = star->getPosition(); CCPoint destPos = star->getDestPos(); if ( pos.equals(destPos) ) { return true; } } return false; }
void MyObject::setProperties(CCPoint speed,CCPoint top_bottom,CCPoint left_right) { this->speed = speed; this->top_bottom = top_bottom; this->left_right = left_right; if (!left_right.equals(ccp(0,0))) { isVect = false; spriteBody->SetLinearVelocity(b2Vec2(-2,0)); }else { isVect = true; spriteBody->SetLinearVelocity(b2Vec2(0,-2)); } this->schedule(schedule_selector(MyObject::checkBound)); }
void Enemy::RandomMovement() { mShortestPathArray.clear(); cocos2d::cc_timeval now; //Get the source and destination Points CCPoint sourceTileCoords = this->GetSpriteTile(); //Get a random point CCSize sizeMap = PathPlanner::GetInstance()->GetLevelMap()->getTileMap()->getMapSize(); CCTime::gettimeofdayCocos2d(&now, NULL); srand(now.tv_sec); int index = rand() % (int)randomPoints.count(); CCPoint destTileCoords = *(dynamic_cast<CCPoint*>(randomPoints.objectAtIndex(index))); // in case of a tentative number of get a valid tile > 3 CCPoint tmpDestTile; int counter = 0; do{ counter++; CCTime::gettimeofdayCocos2d(&now, NULL); srand(now.tv_sec); tmpDestTile.x = rand() % (int)sizeMap.width; tmpDestTile.y = rand() % (int)sizeMap.height; if( PathPlanner::GetInstance()->GetLevelMap()->isValidAndNotWallTile(tmpDestTile) ) { //valid tile destTileCoords = tmpDestTile; break; } }while(counter <=3); //check that there is a path if(sourceTileCoords.equals(destTileCoords)) return; mShortestPathArray = PathPlanner::GetInstance()->Start_A_Star(sourceTileCoords, destTileCoords); mHasReachedTarget = false; }
bool BatchManager::addItem(GameEntitySprite* entity, CCNode* layer, CCPoint parallax) { char* name = entity->getNodeInfo().texture; bool found = false; std::list<BatchManagerItem*>::iterator item; for (item = items.begin(); item != items.end(); item++) { BatchManagerItem* i = dynamic_cast<BatchManagerItem*>(*item); if (!i) continue; char* iName = i->sprite->getNodeInfo().texture; if (xmlStrcasecmp((xmlChar*) iName, (xmlChar*) name) == 0 && parallax.equals(i->parallax) ) { found = true; break; } } // Add sprite to batch if exists, or create new batch if (found) (*item)->batchNode->addChild(entity->getSprite()); else { BatchManagerItem* mgr = new BatchManagerItem(); CCSpriteBatchNode *node = CCSpriteBatchNode::create(name); mgr->batchNode = node; mgr->sprite = entity; mgr->parallax = parallax; node->addChild(entity->getSprite()); items.push_front(mgr); layer->addChild(mgr->batchNode); } return false; }
void AllianceAreaPopupView::refreshShowNode(bool isDrag/* = true*/){ CCPoint pt = getMovePoint(); if(!pt.equals(ccp(0, 0))){ m_picNode->setPosition(m_picNode->getPosition() + pt); if(isDrag){ float maxMove = 15; auto moveDis = sqrt(pow(pt.x, 2) + pow(pt.y, 2)); if(moveDis > maxMove){ pt = pt * (maxMove * 1.0f / moveDis); } getMapNode()->setPosition(getMapNode()->getPosition() + pt); reDraw(NULL); }else{ auto seque = CCSequence::create(CCMoveTo::create(0.2, getMapNode()->getPosition() + pt) , CCCallFuncN::create(this, callfuncN_selector(AllianceAreaPopupView::reDraw)) , NULL ); getMapNode()->runAction(seque); } }else{ reDraw(NULL); } }
void CAScrollView::deaccelerateScrolling(float delay) { if (m_tInertia.getLength() > maxSpeedCache(delay)) { m_tInertia = ccpMult(m_tInertia, maxSpeedCache(delay) / m_tInertia.getLength()); } CCPoint speed; if (m_tInertia.getLength() > maxSpeed(delay)) { speed = ccpMult(m_tInertia, maxSpeed(delay) / m_tInertia.getLength()); } else { speed = m_tInertia; } m_tInertia = ccpMult(m_tInertia, 1 - decelerationRatio(delay)); CCPoint point = m_pContainer->getCenterOrigin(); if (m_bBounces) { CCSize size = this->getContentSize(); CCSize cSize = m_pContainer->getFrame().size; cSize.width = MAX(cSize.width, size.width); cSize.height = MAX(cSize.height, size.height); CCPoint resilience = CCPointZero; if (point.x - cSize.width / 2 > 0) { resilience.x = (point.x - cSize.width / 2) / size.width; } if (point.y - cSize.height / 2 > 0) { resilience.y = (point.y - cSize.height / 2) / size.height; } if ((point.x + cSize.width / 2 - size.width) < 0) { resilience.x = (point.x + cSize.width / 2 - size.width) / size.width; } if ((point.y + cSize.height / 2 - size.height) < 0) { resilience.y = (point.y + cSize.height / 2 - size.height) / size.height; } resilience = ccpMult(resilience, maxBouncesSpeed(delay)); if (speed.getLength() < resilience.getLength()) { speed = ccpSub(speed, resilience); m_tInertia = CCPointZero; } } point = ccpAdd(point, speed); if (this->isScrollWindowNotMaxOutSide()) { m_tInertia = CCPointZero; } if (m_bBounces == false) { point = this->getScrollWindowNotOutPoint(point); } else { if (m_bBounceHorizontal == false) { point.x = this->getScrollWindowNotOutHorizontal(point.x); } if (m_bBounceVertical == false) { point.y = this->getScrollWindowNotOutVertical(point.y); } } if (m_pScrollViewDelegate && point.equals(m_pContainer->getCenter().origin) == false) { m_pScrollViewDelegate->scrollViewDidScroll(this); } m_bDecelerating = true; if (speed.getLength() <= 0.5f) { point = this->getScrollWindowNotOutPoint(point); m_bDecelerating = false; CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CAScrollView::deaccelerateScrolling), this); } m_pContainer->setCenterOrigin(point); }
bool RigSplit(const CCPoint& startPoint, const CCPoint& endPoint, const Rig& originalRig, std::vector<Rig>& finalRigs) { std::vector<std::pair<Rig, CCPoint> > splitPolygons; std::pair<Rig, CCPoint>* currentRig = NULL; Rig intersections; Rig ignoreIntersections; CCPoint lastVert = originalRig[originalRig.size()-1]; for(int i = 0; i < originalRig.size(); i++) { CCPoint intersectPoint = ccpIntersectPoint(startPoint, endPoint, lastVert, originalRig[i]); if( ccpSegmentIntersect(startPoint, endPoint, lastVert, originalRig[i]) ) { intersections.push_back(intersectPoint); } lastVert = originalRig[i]; } std::sort(intersections.begin(), intersections.end(), PositionSort(startPoint)); //Check if the split line is valid. if(intersections.size() < 2) { return false; } if(RigCheckPoint(originalRig, startPoint)) { ignoreIntersections.push_back(intersections[0]); intersections.erase(intersections.begin()); } if(intersections.size() % 2 != 0) { ignoreIntersections.push_back(intersections[intersections.size()-1]); intersections.pop_back(); } std::vector<std::pair<CCPoint, CCPoint> > intersectionPairs; intersectionPairs.reserve(intersections.size() / 2); for(int i = 0; i < intersections.size(); i += 2) { intersectionPairs.push_back(std::make_pair(intersections[i], intersections[i+1])); } splitPolygons.push_back(std::make_pair(Rig(), CCPoint())); currentRig = &splitPolygons[0]; for(int y = 0; y < originalRig.size(); y++) { CCPoint edgeStart = originalRig[y]; CCPoint edgeEnd = originalRig[ (y+1) % originalRig.size()]; if(!RigContain(currentRig->first, edgeStart)) { currentRig->first.push_back(edgeStart); } //Check for intersections with the split line: bool intersects = ccpSegmentIntersect(startPoint, endPoint, edgeStart, edgeEnd); if(intersects) { CCPoint intersectionPoint = ccpIntersectPoint(startPoint, endPoint, edgeStart, edgeEnd); //These points should be ignored. if(RigContain(ignoreIntersections, intersectionPoint)) { continue; } //Add the intersection point to the current polygon. if(!RigContain(currentRig->first, intersectionPoint)) { currentRig->first.push_back(intersectionPoint); } CCPoint pairs; //Find the intersction point in the intersection pairs. for(int f = 0; f < intersectionPairs.size(); f ++) { if(intersectionPairs[f].first.equals(intersectionPoint)) { pairs = intersectionPairs[f].second; break; } if(intersectionPairs[f].second.equals(intersectionPoint)) { pairs = intersectionPairs[f].first; break; } } assert(!pairs.equals(CCPointZero)); if(!RigContain(currentRig->first, pairs)) { currentRig->first.push_back(pairs); } //Set its partner as the crossback point for the current polygon. currentRig->second = pairs; bool found = false; for(int x = 0; x < splitPolygons.size(); x ++) { //If there is an existing polygon with a crossback for this edge if(&splitPolygons[x] != currentRig && (splitPolygons[x].second.equals(pairs) || splitPolygons[x].second.equals(intersectionPoint)) ) { //Set the current polygon to be that polygon currentRig = &splitPolygons[x]; found = true; break; } } //Else if(!found) { //Append a new polygon and new crossback point to the output lists and make it current splitPolygons.push_back(std::make_pair(Rig(), CCPoint())); currentRig = &splitPolygons[splitPolygons.size()-1]; } if(!RigContain(currentRig->first, edgeEnd)) { currentRig->first.push_back(edgeEnd); } } } for(int i = 0; i < splitPolygons.size(); i++) { Rig rig = splitPolygons[i].first; finalRigs.push_back(rig); } return true; }
void CAButton::setSprite(CAControlState controlState, CAView* var, CCPoint point) { if (!var) return; var->setAnchorPoint(CCPoint(0.5f, 0.5f)); var->setPosition(point); var->retain(); CCPoint cPoint = m_obContentSize/2; if (controlState == CAControlStateNormal) { this->removeSubview(m_pSpriteNormal); CC_SAFE_DELETE(m_pSpriteNormal); if (!cPoint.equals(point)) { m_tSpriteNPoint = point; } else { m_tSpriteNPoint = CCPointZero; } m_pSpriteNormal = var; return; } else if (controlState == CAControlStateHighlighted) { this->removeSubview(m_pSpriteHighlighted); CC_SAFE_DELETE(m_pSpriteHighlighted); if (!cPoint.equals(point)) { m_tSpriteHPoint = point; } else { m_tSpriteHPoint = CCPointZero; } m_pSpriteHighlighted = var; } else if (controlState == CAControlStateDisabled) { this->removeSubview(m_pSpriteDisabled); CC_SAFE_DELETE(m_pSpriteDisabled); if (!cPoint.equals(point)) { m_tSpriteDPoint = point; } else { m_tSpriteDPoint = CCPointZero; } m_pSpriteDisabled = var; } else if (controlState == CAControlStateSelected) { this->removeSubview(m_pSpriteSelected); CC_SAFE_DELETE(m_pSpriteSelected); if (!cPoint.equals(point)) { m_tSpriteSPoint = point; } else { m_tSpriteSPoint = CCPointZero; } m_pSpriteSelected = var; } }
void MapScrollLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { if (pTouches->count() > 1) { CCSetIterator it = pTouches->begin(); CCTouch *touch1 = (CCTouch*)(*(it++));; CCTouch *touch2 = (CCTouch*)(*it); CCPoint curPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getLocationInView()); CCPoint curPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getLocationInView()); CCPoint prevPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getPreviousLocationInView()); CCPoint prevPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getPreviousLocationInView()); CCPoint curPosLayer = ccpMidpoint(curPosTouch1, curPosTouch2); CCPoint prevPosLayer = ccpMidpoint(prevPosTouch1, prevPosTouch2); float prevScale = this->getScale(); float curScale = this->getScale() * (ccpDistance(curPosTouch1, curPosTouch2) / ccpDistance(prevPosTouch1, prevPosTouch2)); if(!zoomStart){ zoomStart = true; curScale = prevScale; } if (curScale < minScale || curScale > maxScale) curScale = prevScale; this->setScale( curScale); if (this->getScale() != prevScale) { CCPoint realCurPosLayer = this->convertToNodeSpace(curPosLayer); float deltaX = (this->getAnchorPoint().x * this->getContentSize().width) * (this->getScale() - prevScale); float deltaY = (this->getAnchorPoint().y * this->getContentSize().height) * (this->getScale() - prevScale); this->setPosition(ccp(this->getPosition().x - deltaX, this->getPosition().y - deltaY)); } if (!prevPosLayer.equals(curPosLayer)) { this->setPosition(ccp(this->getPosition().x + curPosLayer.x - prevPosLayer.x, this->getPosition().y + curPosLayer.y - prevPosLayer.y)); } return; } CCTouch* touch = (CCTouch*)( pTouches->anyObject()); CCPoint location = touch->getLocation(); CCPoint prevLocation = touch->getPreviousLocation(); CCPoint diff = ccpSub(location, prevLocation); CCPoint newPosition = ccpAdd(getPosition(), diff); float scale = this->getScale(); float contentSizeWidth = this->getContentSize().width*this->getScale(); float leftOffset = -(contentSizeWidth - WINSIZE.width); float contentSizeHeight = this->getContentSize().height*this->getScale(); float downOffset = -(contentSizeHeight - WINSIZE.height); if (newPosition.x < leftOffset) { float offsetBorder = leftOffset - newPosition.x; diff.x/=(offsetBorder/7.0f); } else if (newPosition.x > 0.0f) { float offsetBorder = newPosition.x; diff.x/=(offsetBorder/7.0f); } if (newPosition.y < downOffset) { float offsetBorder = downOffset - newPosition.y; diff.y/=(offsetBorder/15.0f); } else if (newPosition.y > 0.0f) { float offsetBorder = newPosition.y; diff.y/=(offsetBorder/7.0f); } newPosition = ccpAdd(getPosition(), diff); this->setPosition(newPosition); lastScrolling.push_back(diff); }