# [XNA] Enemy AI Problems

This topic is 2991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, been working on my game's AI for a while now and I've come across some errors that I'm not sure how to approach.

My game is a 3D space shooter, I currently have enemies moving around but I'm having a few problems

1) Some of my enemies are not being drawn! They detect collsion and explode but are completely invisible on the screen! I really have no clue on this one, I am going to post how the enemies are being drawn and updated below.

2) I currently have each enemy on a timer, when the timer depletes the enemy will change direction or speed (there's a timer for both), the only problem though is that when the enemies change direction they don't stop rotating. What would be the most efficient way to check for when they stop? I'll show my code on how I'm currently doing it below.

3) And finally, there is one more thing I'm wanting to implement to my AI... I'd like to give them a line of sight so when the player is in front of them they will fire a bullet. I've read a lot of articles that explain that a raycast collsion is the way to go for this, but I haven't been able to find any code samples or any type of example on the web about this for XNA. Would you guys say that a raycast is the best way to go? Or is there another alternative?

Thanks so much for your time, here is all of the code that relates to the enemies:

//The sniplet of the update function in my enemy class, there is code below it //that calculates thrust, but all of that is working, this is the problem part. //direction is set to each enemy's current direction. DirectionToChange is //where we want the enemy to be facing, and right, up and forward are set to //Vector3.Right, Vector3.Up and so on...         public void Update(GameTime gameTime, float enemyThrust, int changeDirection, string directionToChange)        {            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;            Vector2 rotationAmount = Vector2.Zero;            //TODO: If enemy should turn, turn until he reaches desired position:             if (changeDirection == 1)            {                if (directionToChange == "right")                {                    if (direction == right)                    {                        changeDirection = 0;                    }                    rotationAmount.X += 1.0f;                }                if (directionToChange == "left")                {                    if (direction == left)                    {                        changeDirection = 0;                    }                    rotationAmount.X -= 1.0f;                }                if (directionToChange == "front")                {                                    }            }

//How each enemy is drawn, this is in my Draw method.... some enemies are invisible, but most are being drawn.                for (int i = 0; i < enemyList.Length; i++)                {                     Matrix enemyWorld = Matrix.CreateWorld(enemyList.position, enemyList.direction, Vector3.Up);                    if (enemyList.isActive)                    {                        DrawModel(cobra, enemyWorld);                    }                }//This is in the update loop for the enemy:                for (int i = 0; i < enemyList.Length; i++)                {                    //Update ship's movement and add AI etc.                    enemyList.Update(gameTime, enemyList.thrust, enemyList.changeDirection, enemyList.directionToChange);                    enemyList.speedTimer -= 1;                    enemyList.directionTimer -= 1;//Below this is just code that randomly updates the speedTimer and directionTimer when they reach 0.

[Edited by - Sandman on July 15, 2010 6:40:04 AM]

##### Share on other sites
Edited OP for readability (broken table breaking monster comment into several lines)

##### Share on other sites
Sorry, meant to reply to this yesterday but got distracted at work.

Quote:
 Original post by -XM-1) Some of my enemies are not being drawn! They detect collsion and explode but are completely invisible on the screen! I really have no clue on this one, I am going to post how the enemies are being drawn and updated below.

Well the obvious thing to check is the bActive flag - you're checking it for doing the renders, but you're not checking it for the update, which might explain the results you're seeing.

Quote:
 2) I currently have each enemy on a timer, when the timer depletes the enemy will change direction or speed (there's a timer for both), the only problem though is that when the enemies change direction they don't stop rotating. What would be the most efficient way to check for when they stop? I'll show my code on how I'm currently doing it below.

My guess is that the "direction == left" type tests are never returning true, most likely due to floating point errors, or perhaps due to the size of your rotation if you're working in radians.

With both of these bugs, it should be fairly easy to figure out what is going on using the debugger.

Quote:
 3) And finally, there is one more thing I'm wanting to implement to my AI... I'd like to give them a line of sight so when the player is in front of them they will fire a bullet. I've read a lot of articles that explain that a raycast collsion is the way to go for this, but I haven't been able to find any code samples or any type of example on the web about this for XNA. Would you guys say that a raycast is the best way to go? Or is there another alternative?

I think that would work provided you don't mind them being fairly bad shots. If a ship only fires when you're directly in front of them, they'll only ever hit under a fairly narrow set of circumstances (extreme short range, player flying directly towards/away from the shooter, or staying still)

##### Share on other sites
Thanks, I just got done fixing all of those things.... I ended up just setting the rotations on another timer, I was trying to avoid that but everything looks fine now!

Now on to the ray cast collision.... thanks for the help!

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 9
• 22
• 9
• 31
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007485
• ### Who's Online (See full list)

There are no registered users currently online

×