游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3363|回复: 9

一瑞典游戏公司的招聘题

[复制链接]

2

主题

17

帖子

23

积分

注册会员

Rank: 2

积分
23
发表于 2004-12-13 17:42:00 | 显示全部楼层 |阅读模式
// DQ1: The below code is used to check for which agent is hit by a bullet.
// What is wrong with it and how would you fix it.
// A bullet is only allowed to hit one agent.

bool EXG_UnitContainer::Intersect(const MC_Vector3f& aStart, const MC_Vector3f& anEnd,
    const unsigned short aUnitToExclude, Intersection& anIntersection)
{
    unsigned short u;
    EXG_Unit* unit;

    for (u = 0; u < EX_MAX_UNITS; u++) {
        unit = myUnits;
        if (unit && u != aUnitToExclude) {
            if ( EXM_Intersection:ineVsSphere( aStart, anEnd,unit->GetPosition(), unit->GetType().GetRadius(), anIntersection.myPosition ) ) {
                anIntersection.myDirection = myDamageModel.CalculateHitDirection(aStart, anEnd, unit->GetBodyHeading());
                anIntersection.myUnit = unit;
                return true;
            }
        }
    }

    return false;
}

2

主题

17

帖子

23

积分

注册会员

Rank: 2

积分
23
 楼主| 发表于 2004-12-13 21:28:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

any ideas? ;)

2

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2004-12-13 23:32:00 | 显示全部楼层

Re: 一瑞典游戏公司的招聘题

myUnits也许应该排排序吧?

3

主题

56

帖子

56

积分

注册会员

Rank: 2

积分
56
发表于 2004-12-14 10:10:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

应该找出距离start最近的交点

0

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2004-12-14 12:57:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

if (unit && u != aUnitToExclude)
=>
if (unit && (u != aUnitToExclude) )


这道题不是判断算法是否有问题,否则就应该给出EXM_Intersection:ineVsSphere
的具体代码.

问题是在C++中 && 和 != 运算符的有线级别上.

26

主题

417

帖子

476

积分

中级会员

总版主

Rank: 3Rank: 3

积分
476
发表于 2004-12-14 13:01:00 | 显示全部楼层

Re: Re:一瑞典游戏公司的招聘题

hitit: Re:一瑞典游戏公司的招聘题

if (unit && u != aUnitToExclude)
=>
if (unit && (u != aUnitToExclude) )


这也是个错误啊!必须的()起来!

0

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2004-12-14 13:04:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

怎么错了?

先判断(u != aUnitToExclude),以确定当前的Agent index是否是该被排除在外的aUnitToExclude,然后再和unit 作AND运算:
以确保unit既不为空,也不是改被排除的哪个Agent.

2

主题

17

帖子

23

积分

注册会员

Rank: 2

积分
23
 楼主| 发表于 2004-12-14 18:54:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

aprilsky和chenlee找到了问题所在
hitit: 建议你查阅一下运算符的优先级列表,括号是不需要的
参考代码如下:

bool EXG_UnitContainer::Intersect(
    const MC_Vector3f& aStart,
    const MC_Vector3f& anEnd,
    const unsigned short aUnitToExclude,
    Intersection& anIntersection)
{
    unsigned short u;
    EXG_Unit* unit;

    Intersection ClosestIntersection;
    ClosestIntersection.myUnit = NULL;

    // iterate through contained units
    for(u = 0; u < EX_MAX_UNITS; u++)
    {
        unit = myUnits; // obtain the unit at index u

        // if valid unit and not a unit to exclude
        if (unit && u != aUnitToExclude)
        {
            // do line-sphere intersection test
            if ( EXM_Intersection:ineVsSphere(aStart, anEnd, unit->GetPosition(), unit->GetType().GetRadius(), anIntersection.myPosition) )
            {
                // intersecting! anIntersection.myPosition should have already been updated??? provided that it is a reference parameter

                // update the ClosestIntersection only when 1. first time found an intersection, or 2. new distance is closer to the bullet starting position
                if ( !ClosestIntersection.myUnit ||
                    DISTANCE(anIntersection.myPosition, aStart) < DISTANCE(ClosestIntersection.myPosition, aStart) )
                {
                    // update position
                    ClosestIntersection.myPosition = anIntersection.myPosition;

                    // update direction
                    ClosestIntersection.myDirection =  myDamageModel.CalculateHitDirection(aStart, anEnd, unit->GetBodyHeading());

                    // save intersected unit
                    ClosestIntersection.myUnit = unit;
                }
            }
        }
    }

    // if ClosestIntersection has ever been updated, return it to user
    if (ClosestIntersection.myUnit) {
        anIntersection = ClosestIntersection;
        return true;
    }

    return false;
}

2

主题

81

帖子

91

积分

注册会员

Rank: 2

积分
91
发表于 2004-12-21 01:49:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

都是高手啊.我是什么都看不懂.来凑个热闹.哈。哈。

1+1=??

0

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2004-12-22 19:15:00 | 显示全部楼层

Re:一瑞典游戏公司的招聘题

又学到了不少东西
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-23 19:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表