Results 1 to 8 of 8

Thread: AI in java

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

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default 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'?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: AI in java

    Quote Originally Posted by Tolls View Post
    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. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default 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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default Re: AI in java

    So 'delta' was too small ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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