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.

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.

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.

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.

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:

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.