Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: A question about an object detecting another.

  1. #1
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default A question about an object detecting another.

    Hello everyone!
    In my absence from this site I have been experimenting with swing and graphics. My problem is this, I have two cows on the screen that move along a grid(each square is 16x16) and I have them print an arrow in the direction of the other cow AS LONG AS IT IS 3 OR LESS SQUARES away. When they print their arrows it's usually facing the opposite direction of the cow they are detecting. The object that is the cow is Testai. If you need more of my code posted in order to help I will! (After all it is just cows moving)
    This code is the Testai "detection" code
    for(Testai c : testArray)
    		{
    			//looking right
    			if ((int)(c.x - super.x)/16 <= 3 && (c.x - super.x)/16 > 0)
    			{
    				if ((int)c.y - super.y == 0)
    				{
    					//shoots right
    					PrintScreen.right = true;
    				}
    				else if((int)c.y - super.y > 0)
    				{
    					//shoots south east
    					PrintScreen.southeast = true;
    				}
    				else if((int)c.y - super.y < 0)
    				{
    					//shoots north east
    					PrintScreen.northeast = true;
    				}
    			}
    			//looking left
    			else if ((int)(c.x - super.x)/16 >= -3 && (c.x - super.x)/16 < 0)
    			{
    				if ((int)c.y - super.y == 0)
    				{
    					//shoots left
    					PrintScreen.left = true;
    				}
    				else if((int)c.y - super.y > 0)
    				{
    					//shoots south west
    					PrintScreen.southwest = true;
    				}
    				else if((int)c.y - super.y < 0)
    				{
    					//shoots north west
    					PrintScreen.northwest = true;
    				}
    			}
    			else if ((int)(c.x - super.x)/16 == 0)
    			{
    				if ((int)c.y - super.y < 0)
    				{
    					//shoots down
    					PrintScreen.down = true;
    				}
    				else if ((int)c.y - super.y > 0)
    				{
    					//shoots up
    					PrintScreen.up = true;
    				}
    			}
    		}
    This is where the arrows are printed.
    for(int s = 0; s < testArray.size(); s++)
    		{
    			t = testArray.get(s);
    			if(up)
    			{
    				arrow = new ImageIcon("up.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX(),(int)t.getY()-16,this);
    				up = false;
    			}
    			else if(down)
    			{
    				arrow = new ImageIcon("down.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX(),(int)t.getY()+16,this);
    				down = false;
    			}
    			else if(left)
    			{
    				arrow = new ImageIcon("left.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()-16,(int)t.getY(),this);
    				left = false;
    			}
    			else if (right)
    			{
    				arrow = new ImageIcon("right.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()+16,(int)t.getY(),this);
    				right = false;
    			}
    			else if(southwest)
    			{
    				arrow = new ImageIcon("sw.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()-16,(int)t.getY()+16,this);
    				southwest = false;
    			}
    			else if(southeast)
    			{
    				arrow = new ImageIcon("se.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()+16,(int)t.getY()+16,this);
    				southeast = false;
    			}
    			else if(northwest)
    			{
    				arrow = new ImageIcon("nw.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()-16,(int)t.getY()-16,this);
    				northwest = false;
    			}
    			else if(northeast)
    			{
    				arrow = new ImageIcon("ne.png");
    				arrowprint = arrow.getImage();
    				g2.drawImage(arrowprint,(int)t.getX()+16,(int)t.getY()-16,this);
    				northeast = false;
    			}
    			g2.drawImage(cow,(int)t.getX(),(int)t.getY(),this);
    		}
    Thanks to all who reply! I don't know if I put this in the right section though.
    lil_misfit


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A question about an object detecting another.

    What is the object? If AI is involved, what is supposed to possess the intelligence? Is each cow supposed to be independently intelligent, somehow aware of other cows when they are within a 3-square radius, or is there some other overseeing intelligence?

    The easy solution is to use a coordinate system. Get each graphic's position in the grid, do the math to determine how far apart they are and in which direction, and if they are in sensory distance, show the arrows. I'm not sure if that solution meets your objective.

  3. #3
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: A question about an object detecting another.

    The object is my object called Testai and each cow is supposed to have its own intelligence (so far that is moving around and detecting). Would you like me to post the Testai code? I have it so it prints the arrows but the arrows are pointing in the opposite direction. I don't know if it is a printing problem or something wrong with my detect code. I've "traced" my code and it seems fine but the arrows still print in the opposite direction! That is my problem!
    Thank you for responding though!
    lil_misfit

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A question about an object detecting another.

    The "arrows usually facing in the wrong direction," and the arrows ALWAYS pointing in the opposite direction direction are two very different outcomes. If the program ALWAYS calculates the opposite direction, then it's !alwaysRight. It's just a sign (or logic) error. Find the sign error, if you'd like, or just accept the result you're getting and change the sign.

Similar Threads

  1. Basic java object question
    By BimmyJim in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2013, 08:56 AM
  2. Efficient Object Detecting?
    By Gravity Games in forum Object Oriented Programming
    Replies: 6
    Last Post: October 4th, 2012, 11:01 AM
  3. Basic Question Regarding Object/Method
    By aandcmedia in forum Object Oriented Programming
    Replies: 3
    Last Post: February 26th, 2012, 02:42 PM
  4. [SOLVED] Question About Using Scanner Object Twice
    By derekeverett in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: July 16th, 2011, 10:36 AM
  5. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 08:17 PM