• <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)自強(qiáng)而不息

            Working with Maps and Levels(7)

            cTrigger::get_trigger

            get_trigger is the function in the trigger class that you call every time the player’s character
            moves. get_trigger will take the coordinates of the character you are checking and
            return the identification number of the first trigger found at that location (if any). If
            no triggers are found at the specified location, get_trigger returns a value of zero.

            A great deal is going on in get_trigger, but things are not too complicated. As
            the linked list of triggers is scanned, each trigger in question is checked to
            see whether it and the specified coordinates share the same map space.

            If so, the trigger’s identification number is returned.

            CAUTION
            Never assign a value of zero to a trigger because the trigger class uses zero
            to signify that no trigger is found when the get_trigger function is called.

            long cTrigger::get_trigger(float x_pos, float y_pos, float z_pos)
            {
                
            for(sTrigger* trigger = m_root_trigger; trigger != NULL; trigger = trigger->next)
                {
                    
            if(! trigger->enabled)      // only bother if enabled
                        continue;

                    
            float x_diff, y_diff, z_diff, dist;

                    
            switch(trigger->type)
                    {
                    
            case TRIGGER_SPHERE:
                        
            // check distance from sphere (using radius)
                        x_diff = fabs(trigger->x1 - x_pos);
                        y_diff = fabs(trigger->y1 - y_pos);
                        z_diff = fabs(trigger->z1 - z_pos);

                        dist = x_diff * x_diff + y_diff * y_diff + z_diff * z_diff;

                        
            if(dist <= trigger->radius)
                            
            return trigger->id;

                        
            break;

                    
            case TRIGGER_BOX:
                        
            // check if inside box
                        if((x_pos >= trigger->x1 && x_pos <= trigger->x2) &&
                           (y_pos >= trigger->y1 && y_pos <= trigger->y2) &&
                           (z_pos >= trigger->z1 && z_pos <= trigger->z2))
                        {
                            
            return trigger->id;
                        }

                        
            break;

                    
            case TRIGGER_CYLINDER:
                        
            // first make sure within height bounds
                        if(y_pos >= trigger->y1 && y_pos <= trigger->y1 + trigger->y2)
                        {
                            
            // check distance from cylinder
                            x_diff = abs(trigger->x1 - x_pos);
                            y_diff = abs(trigger->y1 - y_pos);
                            z_diff = abs(trigger->z1 - z_pos);
                        
                            dist = x_diff * x_diff + y_diff * y_diff + z_diff * z_diff;

                            
            if(dist <= trigger->radius)
                                
            return trigger->id;
                        }

                        
            break;

                    
            case TRIGGER_TRIANGLE:
                        
            // first make sure within height bounds
                        if(y_pos >= trigger->y1 && y_pos <= trigger->y1 + trigger->y2)
                        {
                            
            // check if point in front of all lines

                            D3DXVECTOR2 norm_vec;
                            
                            
            // x1,z1 to x2,z2
                            D3DXVec2Normalize(&norm_vec, &D3DXVECTOR2(trigger->z2 - trigger->z1, trigger->x1 - trigger->x2));

                            
            if(D3DXVec2Dot(&D3DXVECTOR2(x_pos - trigger->x1, z_pos - trigger->z1), &norm_vec) < 0)
                                
            break;

                            
            // x2,z2 to x3,z3
                            D3DXVec2Normalize(&norm_vec, &D3DXVECTOR2(trigger->z3 - trigger->z2, trigger->x2 - trigger->x3));

                            
            if(D3DXVec2Dot(&D3DXVECTOR2(x_pos - trigger->x2, z_pos - trigger->z2), &norm_vec) < 0)
                                
            break;

                            
            // x3,z3 to xz,zz
                            D3DXVec2Normalize(&norm_vec, &D3DXVECTOR2(trigger->z1 - trigger->z3, trigger->x3 - trigger->x1));

                            
            if(D3DXVec2Dot(&D3DXVECTOR2(x_pos - trigger->x3, z_pos - trigger->z3), &norm_vec) < 0)
                                
            break;

                            
            return trigger->id;
                        }

                        
            break;
                    }
                }

                
            return 0;   // means no trigger found
            }

            At this point, you check a trigger that is enabled to see whether it intersects with
            the coordinates passed in the x_pos, y_pos, and z_pos arguments of the get_trigger function.
            Each trigger has a special way of determining whether the specified coordinates
            are within the trigger space, and by using a switch statement, the following
            code can determine how to process that intersection check:

            For spheres, you use a distance check. If the coordinates have a distance equal to
            or less than the radius of the sphere, the trigger is touched.

            Box triggers use typical bounding boxes to compare the coordinates of the opposing
            corners to the coordinates being checked to see whether they intersect.

            Cylinder triggers use a mixture of spheres and bounding boxes.

            The triangle trigger code shown here checks whether the coordinate in question
            is in front of all three edges of the triangle by using what’s called a dot-product. For
            each edge of the triangle, the dot-product is calculated and checked to see whether
            the coordinates in question are on the inside or the outside of the triangle.

            You can think of the dot-product as the distance of the coordinates in question
            from a triangle edge. A negative distance means that the coordinates in question
            are on the outside of the triangle, whereas a positive distance means that the coordinates
            in question are inside the triangle.

            If all three dot-product checks come up with positive values, the coordinates in question
            must be inside the triangle. You use one last test to determine whether the coordinates
            in question fall within the height range defined in the sTrigger structure.

            posted on 2007-12-10 11:15 lovedday 閱讀(265) 評論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

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

            搜索

            最新評論

            久久国产精品久久| 久久精品国产精品亚洲人人| AV无码久久久久不卡蜜桃| 欧美久久一级内射wwwwww.| 久久久久久久综合狠狠综合| 中文字幕久久波多野结衣av| 无码人妻精品一区二区三区久久 | 日产久久强奸免费的看| 欧美一级久久久久久久大片| 亚洲狠狠婷婷综合久久蜜芽| Xx性欧美肥妇精品久久久久久| 久久久久亚洲AV无码去区首| 亚洲国产精品无码久久一线| 品成人欧美大片久久国产欧美...| 精品久久久久久久久免费影院| 亚洲成人精品久久| 久久精品一本到99热免费| 国内精品久久久久久野外| 偷偷做久久久久网站| 四虎国产精品免费久久5151 | 国产L精品国产亚洲区久久| 久久这里都是精品| 91秦先生久久久久久久| 久久亚洲私人国产精品vA | 日本免费久久久久久久网站| 亚洲精品无码专区久久同性男| 久久99热狠狠色精品一区| 亚洲伊人久久精品影院| 亚洲午夜无码AV毛片久久| 久久人人爽人人爽人人片AV麻豆| 久久婷婷五月综合国产尤物app| 怡红院日本一道日本久久| 亚洲精品午夜国产VA久久成人 | 无码国产69精品久久久久网站| 久久久精品波多野结衣| 99久久国产免费福利| 久久亚洲精品视频| 国产精品美女久久久网AV| 99久久精品国产免看国产一区| 日韩人妻无码精品久久免费一| 久久精品国产免费观看三人同眠|