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 5 of 5

Thread: i am having a problem with the distance formula,help needed!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i am having a problem with the distance formula,help needed!

    good day all, i am having a problem with a distance formula, i am using it in java greenfoot for a game that i need to program, could someone help me out?
    thanks

    here's what i got so far:
    private double getDistance(double x1, double y1, double x2, double y2)
       {
     
       x1 = t.getX();
       y1 = t.getY();    
       distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
       return distance;
     
       }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: i am having a problem with the distance formula,help needed!

    Hello there Bentino, I am sorry to hear you are having troubles but I will do my best to help out. First off, you never specified what the problem was and this can deter people from wanting to help. So if you can add that in that would help tremendously. Second, there is no way for us to know what 't' is referencing; more specifically we have no idea how you have t.getX() or t.getY() set up which is where the problem could actually be. To surmise, tell us the specific problem (including error messages and what not) and show us a bit more code.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i am having a problem with the distance formula,help needed!

    KucerakJM,
    Sorry about that, here is the whole program:
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
     
    /**
     * Write a description of class Target here.
     * 
     * @author Benjamin Mpungu
     * @version 3.17.2012
     */
    public class Target extends Actor
    {
     
    private int value;
    private int score;
    private double distance;
     
       public Target(int value)
       {
           int hitScore;
           value = hitScore;
           hitScore.setImage("target300.png"); // hitscore amount is set to 300
       }
     
      public void getAirspace()
      {
           return Airspace;     
      }
     
      public void getValue()
      {
           return value;    
      }    
     
       /**
         * Act - do whatever the Target wants to do. This method is called whenever
         * the 'Act' or 'Run' button gets pressed in the environment.
         */
     
       private double getDistance(double x1, double y1, double x2, double y2)
       {
     
       x1 = t.getX();
       y1 = t.getY();    
       distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
       return distance;
     
       }
     
     
       public void act() 
        {
            int myX=aboveThis.getX();
            int myY=aboveThis.getY();
     
            List<Target> targets = inWorld.getObjects(Target.class);
     
            for (Target t : targets)
            {
                if  ( (t.getX() == myX) && (t.getY()<myY) )
                    t.setLocation(t.getX(), t.getY()+25);
                    getWorld().removeObject(Target);
                    // ******************Don't forget to remove target from world after this if statement****************
            if (distance <= 15) // change this to distance formula***************
                hitScore += score;
                hitScore.setImage("explosion.gif");
            }   
     
        }    
     
     
    }

    this program know as "Target" is one subclass out of the actor classes in greenfoot, this basically represents the targets that a plane in my game will be hitting, so this distance formula is needed to find the distance between a Bomb( which is another program/subclass) being dropped by the plane. So i don't necessarily have an error here just that i am not sure how to declare the x1,x2,y1,y2 in the distance formula. The "t" is represented by "for (Target t : targets)" and the t.getX() and t.getY() are supposed to find the x and y variables of the target.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: i am having a problem with the distance formula,help needed!

    how to declare the x1,x2,y1,y2 in the distance formula.
    Take a piece of paper and draw the two points and label the points with their x,y values.
    The changes in the values of x and y are the horizontal and vertical distances between the points that make a right angle. Then use those values in the formula.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: i am having a problem with the distance formula,help needed!

    Well, I must clarify that I have never used Greenfoot before, seems like an interesting tool. However, I think I have something that can help you in your endeavor. I tinkered around with the 'balloons' scenario that came with Greenfoot and found some interesting things. First, some thoughts.

    1. Each Instance of the Target subclass has it's own act() method which takes away the need to create a list of all of them. For instance if you had this instead:

    public void act() 
        {
            int myX=aboveThis.getX();
            int myY=aboveThis.getY();
     
            if((getX() == myX) && (getY()<myY))
                setLocation(getX(), getY()+25);
                getWorld().removeObject(this);
     
            if (distance <= 15) // change this to distance formula***************
                hitScore += score;
                hitScore.setImage("explosion.gif");
            }

    it would work all the same.


    2. As for this distance formula, unless you absolutely need to create one, I see no reason to do so. As I said before I tinkered with the 'balloons' scenario and came across the getOneIntersectingObject() method in the documentation, which seems to take care of the distance formula, so long as the two objects need to touch.

    3. With this in mind and this is largely based off the the 'balloons' scenario, I would suggest only coding in movement in the act() method for the Target subclass and have the Bomb subclass figure out if there is a hit.

    So, there are a few built-in methods that can handle intersections in Greenfoot and I would suggest using one of those. Here is the basic idea of how that would be done.


    I would probably put something like this within the Bomb subclass.

    public void act()
    {
      Target target = (Target) getOnIntersectingObject(Target.class);  //Looks to see if a Target object is intersecting the current bomb object
      if(target != null) {  //If an object was found then blow it up.
        target.destroy();
      }
    }

    This is by no means the complete code and as I said before this is only good if you don't have to write your own Distance method. One final note, I don't know what aboveThis refers to. Yeah that's about it, hope this can help in some way.

Similar Threads

  1. Java client development for distance learning.
    By bosozoku in forum Java Theory & Questions
    Replies: 0
    Last Post: March 4th, 2012, 10:01 AM
  2. [SOLVED] Putting Trajectory of a Projectile formula in Java form.
    By mwebb in forum Java Theory & Questions
    Replies: 2
    Last Post: March 2nd, 2012, 07:23 PM
  3. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  4. lengthen distance between JMenuItem title and shortcut(accelerator)
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 1
    Last Post: October 28th, 2011, 07:27 PM
  5. Method is not initiating formula
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2011, 08:19 PM