Freely jumping and moving in cocos2d-x + tiledmap

Source: Internet
Author: User

Recently I was learning cocos2d-x and encountered a problem: In tiledmap, how to implement algorithms like super Mary jumping and moving?

Assuming that the game's main character is a tile size, how can we achieve the free movement of the game's leading role in the following scenarios?

1. Algorithm Description 1.
Heroposition_nextframe = heroposition_currentframe + CCP (speedx, 0) + CCP (0, speedy) 2.
Determinepoint [4] = ccpoint (heroposition_nextframe.x, heroposition_nextframe.y) ccpoint (heroposition_nextframe.x + herosize. Width, heroposition_nextframe.y)
Ccpoint (heroposition_nextframe.x + herosize. Width, heroposition_nextframe.y + herosize. Height)
Ccpoint (heroposition_nextframe.x, heroposition_nextframe.y + herosize. Height) 3.
Collisionposition getpointcollisionposition (cocos2d: ccpoint heroposition_core, cocos2d: ccpoint determinedpoint, cocos2d: ccpoint &) 4.
Update m_fmaxdisup, m_fmaxdisdown, m_fmaxdisleft, m_fmaxdisright5.
Update heroposition: the first step is to obtain the position of the next frame of the hero based on the current position of the hero + current horizontal/vertical movement speed. Step 2, obtain the positions of the four vertices of the hero Based on the hero's Position + the size of the hero. As the third step of the monitoring point, the hero must be in the following nine squares at any time, obtain the coordinates of the collision type (leftup, up, rightup, right, rightdown, down, leftdown, left, nocollision) and the collision Block Based on the hero's Position + the position of the monitoring point. Step 4, update m_fmaxdisup, m_fmaxdisdown, m_fmaxdisleft, and m_fmaxdisright Based on the collision and the relative position of the hero. These four values represent the fifth step of the maximum distance that the hero can move up, down, left, and right, update the hero location based on the current location of the hero + horizontal/vertical speed + m_fmaxdisup, m_fmaxdisdown, m_fmaxdisleft, and m_fmaxdisright. 2. note: 2.1 every time the hero location of the next frame is predicted, m_fmaxdisup, m_fmaxdisdown, m_fmaxdisleft, and m_fmaxdisright are modified during initialization. Therefore, m_fmaxdisup, m_fmaxdisdown must be initialized before getpointcollisionposition is executed in, m_fmaxdisleft,
M_fmaxdisright indicates the map size:
m_pHero->InitializeMoveScope(this->m_pMap->getMapSize());void GameobjHero::InitializeMoveScope(cocos2d::CCSize mapSize){m_fMaxDisUp = mapSize.height;m_fMaxDisDown = 0;m_fMaxDisLeft = 0;m_fMaxDisRight = mapSize.width;}
The 2.2 vertex collision uses the collision in the upper right corner as an example. The current algorithm does not distinguish the following three collisions. Obviously, m_fmaxdisright must be updated in the first collision, m_fmaxdisup must be updated in the second collision, and the third collision must be updated in the second collision.
//hero is at the right side of the collision tiledif(IS_FLOAT_POSITIVE(m_pHero->getPositionX() - (collisionTiledPosition.x + 1) * m_pMap->getTiledSize().width)&& IS_FLOAT_POSITIVE((m_pHero->getPositionY() + m_pHero->getContentSize().height) - collisionTiledPosition.y * m_pMap->getTiledSize().height)){m_pHero->setMaxDisLeft(max(m_pHero->getMaxDisLeft(), (collisionTiledPosition.x + 1) * m_pMap->getTiledSize().width));m_pHero->setLeftCollided(true);}//hero is at the down side of the collision tiledelse if(IS_FLOAT_POSITIVE((collisionTiledPosition.x + 1) * m_pMap->getTiledSize().width - m_pHero->getPositionX())&& IS_FLOAT_POSITIVE(collisionTiledPosition.y * m_pMap->getTiledSize().height - (m_pHero->getPositionY() + m_pHero->getContentSize().height))){m_pHero->setMaxDisUp(min(m_pHero->getMaxDisUp(), (collisionTiledPosition.y - 1) * m_pMap->getTiledSize().height));m_pHero->setUpCollided(true);}else{m_pHero->setMaxDisLeft(max(m_pHero->getMaxDisLeft(), (collisionTiledPosition.x + 1) * m_pMap->getTiledSize().width));m_pHero->setMaxDisUp(min(m_pHero->getMaxDisUp(), (collisionTiledPosition.y - 1) * m_pMap->getTiledSize().height));m_pHero->setLeftCollided(true);m_pHero->setUpCollided(true);}

However, the third collision method in the lower-right and lower-left corner is different from the preceding method. m_fmaxdisright and m_fmaxdisleft do not need to be set in the lower-right and lower-left corner, because during the moving process, you need to set the vertical speed of each frame to a negative number to achieve the falling effect when no tile is available. Therefore, each time a hero moves a complete tile, there will be a third collision in the lower-right or lower-left corner. If m_fmaxdisright or m_fmaxdisleft is set, the hero will no longer be able to move.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.