Hi there, I'm new to these forums.
I was hoping to get some help with writing a method to solve a geometric problem in Java.
The problem goes something like this:
A tank stands in a random location of a field surrounded by a fence. It fires a projectile at a random bearing. Find where it hits the fence.
We already know these values:
fieldWidth
fieldHeight
tankX
tankY
bearing
I hoped the structure could be something like this:
I'm not so good at maths or geometry so I was hoping someone could help me write the method to solve this problem.Code:class CoOrdinate { private double x; private double y; public coOrdinate(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } } class Projectile { private double fieldWidth = 500; private double fieldHeight = 1000; private double tankLocation = new CoOrdinate(50, 810); private double bearing = 165; //Between 0 and 359 public CoOrdinate getHitCoOrdinate() { //Find the co-ordinate it hits at. return hitCoOrdinate; } }
Thanks a lot for your time.
Are you familiar with trigonometry? Your answer to that will determine how much detail I go into.
Yes, I've studied maths and further maths at A level. I'm familiar with it, but I may be a little rusty.![]()
I had a think and I thought that a simpler way of thinking of the problem is in the scenario of a pool table where you're trying to find where it hits the side on the first bounce (assuming there's no pockets and the balls has a radius of 1).
If you convert your angle into radians, and take the tangent, then you're looking for the intersection of the line: y - posY = tan(angle)*(x-posX) with the walls. You should have an intersection with two walls, the correct wall will be determined by the angle (firing up/down or left/right)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks