Reputation: 73
ive been having trouble with my mob AI
it works to a point, except it does the opposite of what i want, meaning it turns 180Degrees in the wrong direction
Target = playerST.Posistion;
Vector2 trivial;
trivial.X = Posistion.X - Target.X;
trivial.Y = Posistion.Y - Target.Y;
instant = ((float)Math.Atan2(trivial.Y, trivial.X)) + 3.141592f;
this tells me where my target is and calculates the numbers i need to rotate to
its in radians the plus 3.1etc is like 180 degrees, as calculating it this way gives me either a minumum of -3.141 or a maximum of 3.141 but the rotations of the enemy is done in 0 to 6.28 adding the 3.141 makes instant = in the range of the enemies rotation instead of under it by 3.141
anyway this is the part i get stuck at... the actual rotation
// irrelevant
if (attack == true)
{
Vector2 modelVelocityAdd = Vector2.Zero;
modelVelocityAdd.Y = -(float)Math.Sin(rotation);
modelVelocityAdd.X = -(float)Math.Cos(rotation);
modelVelocityAdd *= (0.00002f * speed);
if ((((rotation) + 0.2f)) < instant && (rotation - 0.2f) > instant)
{
modelVelocity += modelVelocityAdd;
}
// not so irrelvant and needs fixing!
if (instant < rotation )
{
rotation -= rotationspeed / 2000;
}
else if (rotation < instant)
{
rotation += rotationspeed / 2000;
}
so my problem is how do i stop it from rotating 180 degrees in the wrong direction and actually get it to face the player instead of facing the exact opposite
i cant simply,do what is below because the ship gets trapped and goes back and forth between say 5 degrees and minus 5 degrees
if (instant < rotation )
{
rotation += rotationspeed / 2000;
}
else if (rotation < instant)
{
rotation -= rotationspeed / 2000;
}
thanks for any help
Upvotes: 0
Views: 1184
Reputation: 73
i figured it out,
trivial.X = Posistion.X - Target.X;
trivial.Y = Posistion.Y - Target.Y;
are wrong it need to be
trivial.X = Target.X -Posistion.X ;
trivial.Y = Target.Y - Posistion.Y ;
thank you without your help i never would of noticed :D
Upvotes: 0
Reputation: 7488
Instead of
if (instant < rotation )
{
rotation += rotationspeed / 2000;
}
else if (rotation < instant)
{
rotation -= rotationspeed / 2000;
}
try
rotation = Math.Lerp(rotation, instant, 0.1);
The 0.1 controls the rotation "speed". Using lerp makes the movement more natrual and will fix the problem of the angle oscillating between positive and negative.
Upvotes: 1