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

• March 22nd, 2012, 05:02 PM
Bentino
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:
Code :

```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;   }```
• March 22nd, 2012, 05:25 PM
KucerakJM
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.
• March 22nd, 2012, 05:34 PM
Bentino
Re: i am having a problem with the distance formula,help needed!
KucerakJM,
Sorry about that, here is the whole program:
Code :

```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.
• March 22nd, 2012, 07:03 PM
Norm
Re: i am having a problem with the distance formula,help needed!
Quote:

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.
• March 22nd, 2012, 07:07 PM
KucerakJM
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:

Code :

```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.

Code :

```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.