Game Development Community

Using castRay to detect other vehicles ahead.

by arda · in Torque Game Engine · 09/21/2005 (12:46 pm) · 1 replies

I need my AI controlled vehicle stop offsett distance from a wall/vehicle.

I cast a ray from getPosition() to getPosition() + getVelocity() * 3_SECONDS. However as the vehicle behind get close to other one
it slow downs as I expected but when it comes too close (bumper to bumper), the feeler does not detect the car ahead and that causes the car behind to speed up again.

Any idea?

Thanks.

void AIWheeledVehicle::avoidVehicles(Move* movePtr)
{
    F32 ONE_SECOND = 1.0;
    F32 TWO_SECONDS = 2.0;
    F32 THREE_SECONDS = 3.0;
    F32 FOUR_SECONDS = 4.0;
    F32 BREAK_DISTANCE = 5.0;

    //
    // Avoid any object 3 seconds away
    //
    Point3F startPos = getPosition();

    Point3F unitVelocity = getVelocity();
    unitVelocity .normalize();

    Point3F endPos = startPos + getVelocity() * THREE_SECONDS;

    // Current Speed
    F32 speed = getMoveSpeed();

    // Throttle to speed up or speed down
    F32 throttle;// = 1.0;

    // Apply handbreak
    bool applyHandbreak = false;


    RayInfo rayInfo;
    //if( getContainer()->castRay( startPos, endPos, VehicleObjectType | InteriorObjectType, &rayInfo ) )
    {    
        //tunca
        // debug info delete
        //Con::printf("avoidVehicles antenna len = [%f]", (endPos - startPos).magnitudeSafe());
        //Con::printf("avoidVehicles speed= [%f]", getVelocity().magnitudeSafe());
        Point3F p = rayInfo.object->getPosition();
        Con::printf("Ray start = [%f, %f, %f]");
        Con::printf("Ray end = [%f, %f, %f]");
        //
        // If object is between start and end positions ??
        //
        if ((startPos.x < p.x && p.x < endPos.x) &&
            (startPos.y < p.y && p.y < endPos.y) &&
            (startPos.z < p.z && p.z < endPos.z))
            Con::printf("Object is inside start-end points ");

        Con::printf("avoidVehicles rayInfo.distance = [%f]", rayInfo.distance);
        Con::printf("avoidVehicles rayInfo.objectPosition = [%f, %f, %f]", p.x, p.y, p.z);
        // Dont apply  throttle
        throttle = 0.0;
        // Apply break to stop
        applyHandbreak = true;
    }
    else
    {
        Con::printf("Throttling");
        throttle = 1.0;
    }

    //else
    //{
    //    // Car has stopped, move the car
    //    if (isZero(getMoveSpeed()))
    //    {
    //        throttle = 1;
    //    }
    //}

    movePtr->y =  throttle;
    movePtr->trigger[2] = applyHandbreak;
}

#1
09/23/2005 (1:13 am)
To solve this problem I used server side container gServerContainer instead of getContainer(), and it works as I expected now.