XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

AS - Artificial Stupidity. Enemy isnt responding correctly...

Last post 04-20-2008 1:48 PM by SomeRhino. 1 replies.
  • 04-20-2008 11:49 AM

    AS - Artificial Stupidity. Enemy isnt responding correctly...

    Hello everyone,

    My enemies are not exactly very responsive. It changes states and moves toward you along the z axis (all that is needed) but when it comes within range, it is meant to rotate to face the player and then shoot 2 shots one from either side of it. (its only a box till the model is finished). It doesnt do this. Here is the code from AI.cs. Please tell me if any more is needed...

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
            private void shootAtPlayer(Player player, Bullet[] enemyBullet, int difficulty)
    {
    rotation = turnToFaceO(toRotationVector2(position), toRotationVector2(player.position), rotation, 0.025f);
    elevation = turnToFaceE(toElevationVector2(position), toElevationVector2(player.position), elevation, 0.025f);
    if (!(rotation == playerOrientation && elevation == playerElevation))
    {
    rotation = turnToFaceO(toRotationVector2(position), toRotationVector2(player.position), rotation, 0.025f);
    elevation = turnToFaceE(toElevationVector2(position), toElevationVector2(player.position), elevation, 0.025f);
    }
    else
    {
    for (int i = 0; i < GameConstants.enemyClip; i++)
    if (!enemyBulletIdea.isActive)
    {
    Matrix orientationMatrix = Matrix.CreateRotationY(rotation);
    Matrix elevationMatrix = Matrix.CreateRotationX(elevation);
    Matrix trans = Matrix.CreateTranslation(player.position) * elevationMatrix * orientationMatrix;
    enemyBulletIdea.direction = trans.Translation;
    enemyBulletIdea.position = position + (trans.Right * 2f) + (trans.Forward * 2f);
    enemyBulletIdea.isActive = true;
    break;
    }
    for (int i = 0; i < GameConstants.enemyClip; i++)
    if (!enemyBulletIdea.isActive)
    {
    Matrix orientationMatrix = Matrix.CreateRotationY(rotation);
    Matrix elevationMatrix = Matrix.CreateRotationX(elevation);
    Matrix trans = Matrix.CreateTranslation(position) * elevationMatrix * orientationMatrix;
    enemyBulletIdea.direction = trans.Translation;
    enemyBulletIdea.position = position + (trans.Left * 2f) + (trans.Forward * 2f);
    enemyBulletIdea.isActive = true;
    break;
    }
    //soundBank.PlayCue("EnemyShoot");
    }
    }

    private Vector2 toRotationVector2(Vector3 v) { return new Vector2(v.X, v.Z); }
    private Vector2 toElevationVector2(Vector3 v) { return new Vector2((float)Math.Sqrt(Math.Pow(v.X, 2) + Math.Pow(v.Z, 2)), v.Y); }

    private float turnToFaceO(Vector2 position, Vector2 faceThis, float currentAngle, float turnSpeed)
    {
    float x = faceThis.X - position.X;
    float y = faceThis.Y - position.Y;
    playerOrientation = (float)Math.Atan2(y, x);
    float difference = wrapAngle(playerOrientation - currentAngle);
    difference = MathHelper.Clamp(difference, -turnSpeed, turnSpeed);
    return wrapAngle(currentAngle + difference);
    }

    private float turnToFaceE(Vector2 position, Vector2 faceThis, float currentAngle, float turnSpeed)
    {
    float x = faceThis.X - position.X;
    float y = faceThis.Y - position.Y;
    playerElevation = (float)Math.Atan2(y, x);
    float difference = wrapAngle(playerElevation - currentAngle);
    difference = MathHelper.Clamp(difference, -turnSpeed, turnSpeed);
    return wrapAngle(currentAngle + difference);
    }

    private float wrapAngle(float radians)
    {
    while (radians < -MathHelper.Pi)
    radians += MathHelper.TwoPi;
    while (radians > MathHelper.Pi)
    radians -= MathHelper.TwoPi;
    return radians;
    }
  • 04-20-2008 1:48 PM In reply to

    Re: AS - Artificial Stupidity. Enemy isnt responding correctly...

     One thing that may be causing problems is that you are doing a == test on two floats in the main if clause, and the chances of two floats being exactly equal is quite improbable. I doubt this test would ever pass. Try using a range like
    if (Math.Abs(float1 - float2) < 0.1f)
    { ... }

    Please elaborate on what the AI does once it gets in range.
    __________________________________________________
    Check out my XNA project here:
    http://people.cis.ksu.edu/~ccaywood/astroblast/
Page 1 of 1 (2 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG