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