• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當(dāng)自強而不息

            Controlling Players and Characters(12)

             

            Following Another Character
             

            Although following another character might seem complicated at first, don’t worry
            too much. Remember that keeping it simple is the solution. Following a character
            is as easy as walking a route. Because a character already knows its own coordinates
            and the coordinates of the character it is following, you can use previously seen
            functions to move the character toward another one.
             

            The only difference at this point is that you might want a character to get within a
            specific distance from the followed character, as illustrated in Figure 16.5.

            Knowing each character’s coordinates (the character being followed and the character
            doing the following), you can construct a single function that determines
            which direction the “following” character should move:

            void CalculateFollowMovement(
              float CharXPos, float CharZPos, // Coordinate of character
              float WalkSpeed, // Walking speed of char.
              float FollowXPos, float FollowZPos, // Coords of followed char.
              float FollowDistance, // Distance to follow at
              float *MoveX, float *MoveZ) // Variables for movement
            {
              // Fix for faster distance checking
              FollowDistance *= FollowDistance;

              // Get distance between characters
              float XDiff = (float)fabs(FollowXPos - CharXPos);
              float ZDiff = (float)fabs(FollowZPos - CharZPos);
              float Length = XDiff*XDiff + ZDiff*ZDiff;

              // If distance between characters is less than allowed,
              // then just stand still.
              if(Length < FollowDistance) {
                *MoveX = *MoveZ = 0.0f;
                return;
              }

              // Calculate rate to move based on character walking speed
              Length = sqrt(Length);
              *MoveX = (CharXPos - FollowXPos) / Length * WalkSpeed;
              *MoveZ = (CharZPos - FollowZPos) / Length * WalkSpeed;
            }

            Whenever you want to update a character that is following another one, you merely
            pass along the required data and move the character using the returned movement
            variables.

             

            Evading Another Character
             

            Evading means to move a character away from another one. If the character to be
            avoided is closer than the minimum set distance, the evading character moves in
            the opposite direction through the use of the CalculateEvadeMovement function:

            void CalculateEvadeMovement(
              float CharXPos, float CharZPos, // Coordinate of character
              float WalkSpeed, // Walking speed of char.
              float FollowXPos, float FollowZPos, // Coords of evaded char.
              float EvadeDistance, // Distance to evade
              float *MoveX, float *MoveZ) // Variables for movement
            {
              // Fix for faster distance checking
              FollowDistance *= FollowDistance;

              // Get distance between characters
              float XDiff = (float)fabs(FollowXPos - CharXPos);
              float ZDiff = (float)fabs(FollowZPos - CharZPos);
              float Length = XDiff*XDiff + ZDiff*ZDiff;

              // If distance between characters is more than allowed,
              // then just stand still.
              if(Length > EvadeDistance) {
                *MoveX = *MoveZ = 0.0f;
                return;
              }

              // Calculate rate to move based on character walking speed
              Length = sqrt(Length);
              *MoveX = -((CharXPos - FollowXPos) / Length * WalkSpeed);
              *MoveZ = -((CharZPos - FollowZPos) / Length * WalkSpeed);
            }


            posted on 2007-11-14 15:56 lovedday 閱讀(187) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            亚洲AV无码一区东京热久久| 99久久人妻无码精品系列| 久久精品国产亚洲网站| 精品综合久久久久久97超人| 2021少妇久久久久久久久久| 久久国产免费直播| 一日本道伊人久久综合影| 久久久久久精品免费看SSS| 性高湖久久久久久久久| 7国产欧美日韩综合天堂中文久久久久| 久久不见久久见免费影院www日本| 久久发布国产伦子伦精品| AAA级久久久精品无码区| 99久久婷婷国产综合亚洲| 日产精品久久久一区二区| 国产69精品久久久久久人妻精品| 亚洲国产香蕉人人爽成AV片久久| 久久久久久久久久久久中文字幕 | 久久人妻无码中文字幕| av国内精品久久久久影院| 伊人久久大香线蕉无码麻豆| 久久本道伊人久久| 亚洲色大成网站www久久九| 久久精品国产亚洲7777| 国产欧美久久一区二区| 久久夜色精品国产噜噜亚洲AV| 久久婷婷五月综合色99啪ak| 国产巨作麻豆欧美亚洲综合久久 | 久久99国产精品二区不卡| 少妇久久久久久被弄到高潮| 久久精品无码av| 99久久国产热无码精品免费 | 精品免费tv久久久久久久| 久久精品国产乱子伦| 久久无码AV一区二区三区| 久久久久久午夜精品| 久久国产V一级毛多内射| 久久99精品久久久久久水蜜桃| 狠狠色丁香久久婷婷综合五月| 久久国产精品无码一区二区三区| 亚洲国产精品无码久久久蜜芽|