# Thread: AI basics not working?

1. Member Join Date
Apr 2013
Posts
41
Rep Power
0

## AI basics not working?

Okay so i am just baffled why this isnt 100% working...

Java 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%:
Java 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  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## 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.  Reply With Quote

3. Member Join Date
Apr 2013
Posts
41
Rep Power
0

## Re: AI basics not working? 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!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•