# Thread: AI in java

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

## AI in java

Hi so im working on some basic AI in java.. just a pixel moving around really, but i want it to follow a certian path. heres the code:

Java Code:
```//moving the monster towards the barrier1
if(x -1 > barrierX1)x -= delta * .05;
if(x -1 < barrierX1){
if(y + 1< barrierY1)y += delta * .05;
}
}
//destroying first barrier
if((x + 1 > barrierX1) && (y + 1 > barrierY1) && (barrier[5] != 0)){
barrier[5] = barrier[5] - 50;
monsterdied = true;
draw = false;
}
//getting in position to destroy second barrier
if((x + 5 > barrierX1) && (y + 2 > barrierY1) && (barrier[5] == 0)){
died = true;
if (go == true){
if(y < spoty )y += delta * .05;
if(y + 1 > spoty){
if(x < spotx)x += delta * .05;
}
}
if(x+2 >spotx)go = false;
//getting to barrier
if(go == false){
if(y < barrierY5)y += delta * .05;
}

//destroying second barrier
if((x-1<spotx) && (y+1>barrierY5) && (barrier[3] != 0)){
barrier[3] = barrier[3] - 50;
monsterdied = true;
draw = false;
died = false;
go = true;
}
}
//going to generator
if((x-1<spotx) && (y+1>barrierY5) && (barrier[3] == 0)){
if(x > generator)x -= delta * .05;
}```
so it all works fine until it gets to this part:

Java Code:
```//going to generator
if((x-1<spotx) && (y+1>barrierY5) && (barrier[3] == 0)){
if(x > generator)x -= delta * .05;
}```
now what that is trying to do is making it move left and for some reason it wont... not sure if its cause it was previously moving right and is still trying to do that or not? i mean i used a boolean to stop it from doing that .. help please. THANKS

2. ## Re: AI in java

There are quite some variables in that condition: x, y, spotx, barriesY5, barrier[3], generator, delta ... print them out before that test and see if their values make sense to you.

kind regards,

Jos

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

## Re: AI in java

Thank you for your reply, i did what you said and the variables all make sense but its just not working like it should be. I am very stumped by this.. i turned the variables from floats to ints just so they didnt have decimal points.. easier to work with.. heres the System.out:

Java Code:
`X: 680, Y: 292, spotx: 680, generator: 508, barrierY5: 292, barrier[3]: 0`
and my if statement is:
Java Code:
```if((ix-10<ispotx) && (iy+1>ibarrierY5) && (barrier[3] == 0)){
if(ix > generator)x -= delta * .05;
}```
all the instances are met and it should be moving -x because X is greater then generator but it isnt moving atall.. any ideas?

4. Moderator
Join Date
Apr 2009
Posts
13,062
Rep Power
23

## Re: AI in java

What is 'ix', 'iy'?
I don't see those in your output.
Or are they the 'X' and 'Y'?
What is the value of 'delta'?

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

## Re: AI in java

Originally Posted by Tolls
What is 'ix', 'iy'?
I don't see those in your output.
Or are they the 'X' and 'Y'?
What is the value of 'delta'?
ix and iy is just me convering the X and Y into ints because they were floats before.
As for delta i couldnt really tell you its exact value as it changes all the time and im not exactly sure why..
you see i am using librarys from Slick2D(im guessing delta is defined in one of those librarys, but it shouldnt be affecting this)

6. Moderator
Join Date
Apr 2009
Posts
13,062
Rep Power
23

## Re: AI in java

I'd print it out.
And also see whether the value of 'x' has actually changed in the last if statement...you need lots of logging, essentially.

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

## Re: AI in java

I added delta to the print statement, and this is the last 4:
Java Code:
```X: 680, Y: 292, spotx: 680, generator 508, barrierY5: 292, barrier[3] 0, Delta: 16
X: 680, Y: 292, spotx: 680, generator 508, barrierY5: 292, barrier[3] 0, Delta: 16
X: 680, Y: 292, spotx: 680, generator 508, barrierY5: 292, barrier[3] 0, Delta: 15
X: 680, Y: 292, spotx: 680, generator 508, barrierY5: 292, barrier[3] 0, Delta: 16```
delta seems to stay around 15/16.
As for the x it doesnt change once it hits 680 because it was going +x till 680, then it went -Y to 292.
Now i want it to go -X to 508 but it just wont... do you think it has something to do with it is getting confused with still trying to go +X and now also trying to go -X? the code is back in my first post. I Separated them with a boolean so they wouldnt conflict but im not so sure..

EDIT: Fixed problem, used your advice of using a whole bunch of logging.
Thank you! can close thread.
Last edited by leeroijenkiins; 04-26-2013 at 07:05 PM.

8. ## Re: AI in java

So 'delta' was too small ...

kind regards,

Jos

#### Posting Permissions

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