COCOS2DX-based RPG simple and practical algorithm 2-character following movement

Source: Internet
Author: User

1. The simplest way of thinking:

If you let B follow a and both keep the distance x (what, do you want B and a to overlap?) Then you really don't have to look at it anymore.

Each frame checks the distance between B and a, if the distance > X, then let B want a to move one step. But when B's Speed > A's speed, the problem comes, B is shaking very badly when it follows a.


2. Resolve Jitter

Here's a little bit of water trick to fix jitter: lerp interpolation

M_herofollowlist is a list of roles, with each frame calling Playherofollow () to implement the following

void maptilelayer::p layherofollow ()

{

if (m_herofollowlist. Empty())

return;

Auto prev =m_herofollowlist. At(0);

prev->setposition(m_movelayer-getPosition());

for (auto hero:m_herofollowlist)

{

if (hero->getPosition(). Distance (prev->getPosition()) >)

{

Hero->setposition(hero->getPosition(). Lerp(prev->getPosition(),0.03));

hero->setlocalzorder(getzorderonforeground(hero->getPosition()));

prev = hero;

}

}

}


3. Completely follow the above, Lerp is a water-comparing technique, as we will find that the follower's trajectory is not exactly the same as the follower! Sometimes we need exactly the same moving path, such as "Snake"
(1) Implement a function that records the role position per frame Recordcaptainpath

List<point> Captainpathqueue; 100 points
void Hero::recordcaptainpath (float dt)
{
while (Captainpathqueue.size () >= 100)
{
Captainpathqueue.erase (Captainpathqueue.begin ());
}
Point curpos = Getcenterpoint ();
Captainpathqueue.push_back (CurPos);
}


(2) Provide a function for the follower to obtain a path point

const int queue_max_frame_length = 15; The gap with the followers is 15 frames.

The size of the queue_max_frame_length determines how close the formation is.


Point Hero::getnextpathrecord ()
{
if (Captainpathqueue.size () < queue_max_frame_length)
return Point::zero;
Else
{
Point first = Captainpathqueue.front ();
Captainpathqueue.pop_front ();
return first;
}
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

COCOS2DX-based RPG simple and practical algorithm 2-character following movement

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.