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 !
chasing
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.
thanks David , sorry I am assuming 3d of course , 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 .
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 .
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.
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();
}
}
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.
belfegor , even when I am in the range he move at his position not translated toward me ? ok I will brothers thanks again :)
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 ?
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;
}
I'm going to have to agree that you could/should be able to figure this out simply by stepping through your code. Debugging 101, sir.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"