Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

USARBot.SoccerBall


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
/*
    This script allows to move the ball around the map. SetLocation function
    is not enough becouse it updates only the mesh position, note the rigid
    body state. That's why we need to update manually rigid body state. Here's
    another problem: if we move the ball near the previous position (100~200uu)
    than movement will not be precise. It seems that Karma applies an impulse to
    the ball. Only if we move the ball far away the position is correctly updated.
    I don't know the reason of this behavior, for now I'll hack movement code
    making a 2 step movement: 1) I'll move the ball far away both from current and
    destination position. 2) I'll move the ball to destination position.

    Commands to control the ball>

      SETBALL {Location x,y,z}

    Moves the ball in the position (meters) x, y, z.

      SETBALL {Kick <Y/B>} [{Speed speed}]

    Kicks the ball in the Yellow or Blue goal, with an optional Speed value.
    Default speed is 2, 1 is slow while 3 is fast.

    This ball doesn't work in client/server.

     by Marco Zaratti - marco.zaratti@gmail.com
*/
class SoccerBall extends KRobot config(USARBot);

//Move variables
var KRigidBodyState BallRB;
var int BallMState; //0: normal, 1: ready to move, 2: in "space"
var vector newPos, tempPos;

//Kick variables
var vector kickDirection;
var config float kickForce;

//Auto reposition variables
var enum EBallState
{
    BALL_IN,
    BALL_OUT
} BallKState;

var vector stopLocation;
var float triggerTimer;
var config float outRepositionTime;
var config float inRepositionTime;
var config float movementTolerance;
var config int frontierX;
var config int frontierY;
var config vector targetPos;
var config bool enableReposition;

simulated function PostNetBeginPlay()
{
    Super.PostNetBeginPlay();

    BallKState = BALL_IN;
    stopLocation = location;
}

function ProcessCarInput()
{
    Super.ProcessCarInput();

    switch(USARRemoteBot(Controller).BC.cmd)
    {
    case 1: // Move ball
        MoveBall(USARRemoteBot(Controller).BC.Loc);
        USARRemoteBot(Controller).BC.cmd = 0;
        break;
    case 2: // Kick ball
        KickBall(USARRemoteBot(Controller).BC.Target, USARRemoteBot(Controller).BC.Speed);
        USARRemoteBot(Controller).BC.cmd = 0;
       break;
    }

}

simulated event bool KUpdateState(out KRigidBodyState newState)
{
    switch(BallMState)
    {
        case 0:
            return super.KUpdateState(newState);
        case 1:
            newState = BallRB;
            BallRB.Position = KRBVecFromVector(newPos);
            BallRB.AngVel = KRBVecFromVector(vect(0,0,0));
            BallRB.LinVel = KRBVecFromVector(vect(0,0,0));
            BallMState++;
            break;
        case 2:
            newState = BallRB;
            BallMState = 0;
            break;
    }

    return true;
}

simulated function MoveBall(vector pos)
{
    newPos = pos;

    KGetRigidBodyState(BallRB);
    BallRB.Position = KRBVecFromVector(tempPos);
    BallRB.AngVel = KRBVecFromVector(vect(0,0,0));
    BallRB.LinVel = KRBVecFromVector(vect(0,0,0));
    BallMState++;
}

simulated function KickBall(String direction, float force)
{
    local vector dest;

    if(direction ~= "Y")
        dest.X = -670;
    else if(direction ~= "B")
        dest.X = 670;
    //other else if....
    else return;

    //force and kickForce are exponents
    if(force ~=0)
        force = kickForce;

    force = 150 * exp(force/1.44);    //force ~= 150* 2^force
    dest.Y = RandRange(-100,100);
    kickDirection = Normal(dest - location) * force;

    KGetRigidBodyState(BallRB);
    BallRB.LinVel = KRBVecFromVector(kickDirection);
    BallMState = 2;
}

// Checks how far the ball has moved since last stopLocation.
// Every time the ball moves farther then repositionTolerance
// the stopLocation is updated and a new timing cycle is
// initiated.
simulated function bool IsStationary()
{
    if(VSize(location - stopLocation) < movementTolerance)
        return true;

    stopLocation = location;
    triggerTimer = 0;
    return false;
}

// Updates the ball state and reset counters if the ball
// changed state.
simulated function CheckBallState()
{
    local bool inTheField;

    if(location.X < frontierX  &&
       location.X > -frontierX &&
       location.Y < frontierY  &&
       location.Y > -frontierY)
       inTheField = true;

    switch(BallKState)
    {
        case BALL_IN:
            //The ball has moved from IN to OUT
            if(!inTheField)
            {
                triggerTimer =0;
                stopLocation = location;
                BallKState = BALL_OUT;
            }
            break;
        case BALL_OUT:
            //The ball has moved from OUT to IN
            if(inTheField)
            {
                triggerTimer =0;
                stopLocation = location;
                BallKState = BALL_IN;
            }
            break;
    }
}

simulated function Tick(float Delta)
{
    super.Tick(Delta);

    if(!enableReposition || BallMState > 0)
        return;

    // Updates the ball state and reset counters if the ball
    // changed state.
    CheckBallState();

    switch(BallKState)
    {
        case BALL_IN:
            if(IsStationary())
            {
                triggerTimer += Delta;
                if(triggerTimer >= inRepositionTime)
                {
                    triggerTimer = 0;
                    // Do not move the ball if it's near the target position
                    if(VSize(location - targetPos) > movementTolerance)
                        MoveBall(targetPos);
                }
            }
            break;
        case BALL_OUT:
            triggerTimer += Delta;
            if(triggerTimer >= outRepositionTime)
                MoveBall(targetPos);
            break;
    }
}

defaultproperties
{
    bNoDelete=false
    DrawScale=1.0
    kickForce=2
    tempPos=(X=0,Y=0,Z=900) //Temporary position for ball movement
    enableReposition=false
    outRepositionTime=1     //Repositioning time when the ball is out of the field
    inRepositionTime=10     //Repositioning time when the ball is in the field
    movementTolerance=5     //Moevement tolerance of the ball in uu.
    frontierX=700           //frontierX & Y defines the filed extension.
    frontierY=500           //(-frontierX/Y <-> frontierX/Y)
    targetPos=(X=0,Y=0,Z=10)//Taget position when reposisitoning.
    StaticMesh=StaticMesh'USARSim_Objects_Meshes.AiboSoccer.Ball_8cm'
    Begin Object Class=KarmaParams Name=KBall
        KStartEnabled=True
        bKDoubleTickRate=True
        bHighDetailOnly=False
        bClientOnly=False
        KMass=0.04
        KActorGravScale=1
        KLinearDamping=0.15
        KAngularDamping=0.1
        KMaxAngularSpeed=100
        KMaxSpeed=25000
        KFriction=0.05
        KRestitution=0.9
        Name="KBall"
    End Object
    KParams=KarmaParams'USARBot.SoccerBall.KBall'
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Di 16.1.2007 21:48:16.000 - Creation time: Mo 16.4.2007 11:20:53.453 - Created with UnCodeX