Sign in to follow this  
MohammadAhmed

chasing

Recommended Posts

Hello , here is an image show what I am gonna to do in my XNA game , I want to check if the player is inside zone range and if yes then attack the player , I tried to do this using checking ray but it's odd and give null results as player must be in front of enemy so he can be detected !

jvcsdkjqg3jd9dj6g.jpg

Share this post


Link to post
Share on other sites

Since you didn't specify, I'm assuming 2D for this. I'm also guessing each enemy has their "zone" or view distance stored with the rest of their information. You can find the distance between the player and the enemy by using the following

float x = player.x - enemy.x; // Distance between x positions of player and enemy
float y = player.y - enemy.y; // Likewise, difference between y values of positions
 
float distance = Math.sqrt((x * x) - (y * y)); // Replace with your math library - this is a Java implementation

 

Then, if the distance calculated is less than than the enemy's view distance or zone, the enemy attacks. For making sure the enemy actually 'sees' the player, you can do a check where, if the enemy is facing left and the player's x position is left of (less than) the enemy's, attack. If the player's x position is right of (greater than) the enemy's, attack. You can adjust this to have the enemy have a view angle as well so he can't see floor to ceiling but has a more realistic feeling field of view.

Share this post


Link to post
Share on other sites

thanks David smile.png , sorry I am assuming 3d of course smile.png  , I have bounding box surrounding my player and also for the enemy , now I can fire a ray to check collision in front of enemy then if I am collide with the ray enemy can attack else enemy can't , but this looks odd and very bad as I must be in front of enemy , so instead I want a zone ( circle ) which is dangerous circle so that when I am inside this circle enemy can attack 

Note : anyone have another strategy pls share with some code if you can smile.png .

David , you said that I can do this by using view angle for each enemy instead , can you post some code if you can taking in mind the ray I can use for each model in the game .
dhk5taecpo6k6mp6g.jpg

Edited by mrmohadnan

Share this post


Link to post
Share on other sites

It doesn't matter if it is 2d or 3d, you could use the same math

 

 

float enemyConeAngle = 0.6f;
float enemyZoneRadius = 10.0f;

vec3 enemyPos = enemy.pos;
vec3 enemyDir = normalize(enemy.LookAt - enemyPos);
vec3 playerPos = player.pos;
vec3 toPlayerDir = normalize(playerPos - enemyPos);
 
float angle = acos(dot(enemyDir, toPlayerDir));
float distance = sqrt(dot(playerPos - enemyPos));

if(angle < enemyConeAngle && distance < enemyZoneRadius)
{
    enemy.state = chase_player;
}
 
...
if(enemy.state == chase_player)
{
    float moveSpeed = 3.0f;
    enemy.pos += toPlayerDir * moveSpeed * deltaTime; // move towards player
}

 

On top of my head, might made mistake somewhere.

Edited by belfegor

Share this post


Link to post
Share on other sites

Oh again brother belfegor thaaaaaaaaaanks  , I tried this code but the enemy move at his place ( no translation is done ! )not move , what  do think belfegor ? 

 

 double distanceX = (double)(cci.CharacterController.Body.Position.X - dwarfChrachterController.Body.Position.X);
            double distanceZ = (double)(cci.CharacterController.Body.Position.Z - dwarfChrachterController.Body.Position.Z);
            if (Math.Sqrt( (distanceX * distanceX) + (distanceZ * distanceZ)) <=500)
            {
                RunController(dwarfAnimatior, dwarfwalk);

                Vector3 direction = cci.CharacterController.Body.Position - dwarfChrachterController.Body.Position;

                Vector3.Normalize(direction);

                dwarfChrachterController.Body.Position += direction * 5 * gameTime.ElapsedGameTime.Seconds;

                if (enemyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 50)
                {
                    sound.playAh();
                }
            }
Edited by mrmohadnan

Share this post


Link to post
Share on other sites

You should learn to debug your code a little.For example put some breakpoints to see what is going on. You could put one in body of if(Math.Sqrt(...)) statement to see if code that is inside is executed.

 

In my engine 500 would be half of kilometer.

Edited by belfegor

Share this post


Link to post
Share on other sites

yeah , thaaaanks belfegor , I got it working and now it's moving toward me , now just orient the enemy when he move toward me so that his body.front direction facing me ! can you refine your code a little bit as you do with steering ? 

usry54ec1lm8a1y6g.jpg

Ray enmyRay;

            enmyRay.Position = dwarfChrachterController.Body.Position;

            enmyRay.Direction = Vector3.Normalize(dwarfChrachterController.Body.OrientationMatrix.Forward);
            double distanceX = (double)(cci.CharacterController.Body.Position.X - dwarfChrachterController.Body.Position.X);
            double distanceZ = (double)(cci.CharacterController.Body.Position.Z - dwarfChrachterController.Body.Position.Z);

            Vector3 velocity =(cci.CharacterController.Body.Position- dwarfChrachterController.Body.Position) ;

            if (Math.Sqrt( (distanceX * distanceX) + (distanceZ * distanceZ)) <=500)
            {
                RunController(dwarfAnimatior, dwarfwalk);
                
                Vector3.Normalize(velocity);
                velocity *= 5.0f;

                dwarfChrachterController.HorizontalMotionConstraint.MovementDirection += new Vector2(velocity.X,velocity.Z);

                if (enmyRay.Intersects(cci.CharacterController.Body.CollisionInformation.BoundingBox) <= 50)
                {
                    RunController(dwarfAnimatior, dwarfattak);
                    sound.playAh();
                }
                velocity = cci.CharacterController.Body.Position - dwarfChrachterController.Body.Position;
            }
Edited by mrmohadnan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this