# AI basics not working?

• 04-30-2013, 08:08 PM
leeroijenkiins
AI basics not working?
Okay so i am just baffled why this isnt 100% working...

Code:

```                        if(fx < ipallyx)fx += delta * .05;                         if(fx > ipallyx)fx -= delta * .05;                         if(fy > ipallyy)fy -= delta * .05;                         if(fy < ipallyy)fy += delta * .05;                                                 if(fx2 < iknightx)fx2 += delta * .05;                         if(fx2 > iknightx)fx2 -= delta * .05;                         if(fy2 > iknighty)fy2 -= delta * .05;                         if(fy2 < iknighty)fy2 += delta * .05;```
on both of the occasions where it is fx -=, fy -=, fx2 -= and fy2 -= ; they work.
so easy to say the AI will go only negative x and y, but it wont go positive?

I tried putting a system.out.print in the += ones and what do you know it printed; so i know that if statement is working, but for some reason theyre not moving +x or y..
now another weird thing is that this is simply a copied movement i already made but with different variables; cause this works 100%:
Code:

```                        if(ibirdx < ipallyx)birdx += delta * seekerspeed;                         if(ibirdx > ipallyx)birdx -= delta * seekerspeed;                         if(ibirdy > ipallyy)birdy -= delta * seekerspeed;                         if(ibirdy < ipallyy)birdy += delta * seekerspeed;```
does anyone have any ideas? cause im just confused..
and the seekerspeed is just a variable which is .06
• 05-01-2013, 09:46 AM
Tolls
Re: AI basics not working?
OK, going to hazard a guess here as you haven't told us what type 'fx' etc are, but I'm assuming they're ints.

Since the right hand side of the calculation is a double, I'm also guessing that 'delta * 0.05' is less than 1 each time, this is being turned into an int.
When that happens the fraction element is remove.
So for the subtraction we have, eg, 10 - 0.1 = 9.9...remove the fraction gives us 9.
For addition we have 10 + 0.1 = 10.1...remove the fraction takes us back to 10.

So if you want to work with fractions than make all the variables double/float, then convert them when you use them, assuming they need to be ints at some point.
• 05-04-2013, 08:44 PM
leeroijenkiins
Re: AI basics not working?
Quote:

Originally Posted by Tolls
OK, going to hazard a guess here as you haven't told us what type 'fx' etc are, but I'm assuming they're ints.

Since the right hand side of the calculation is a double, I'm also guessing that 'delta * 0.05' is less than 1 each time, this is being turned into an int.
When that happens the fraction element is remove.
So for the subtraction we have, eg, 10 - 0.1 = 9.9...remove the fraction gives us 9.
For addition we have 10 + 0.1 = 10.1...remove the fraction takes us back to 10.

So if you want to work with fractions than make all the variables double/float, then convert them when you use them, assuming they need to be ints at some point.

ahh thank you very much; converted them to floats and it worked!