-1

So this may be a fairly simple question, however I've been writing this code for over 8 hours so my brain is slightly pooped. Basically, I have a frame that looks like this: (https://i.stack.imgur.com/YkbD1.jpg)

If you'll excuse my crude drawing, I was wondering about the maths. Given the angle theta, what I want to do is, given a specific Point object, that will be the bullet I shoot, how do I adjust the x/y coordinates in parallel to move along that given angle? There is a window size of 700x500 inside the JFrame. I'll post the code in a pastebin if it is quite necessary but I felt that this question was purely arithmetic (sin/cos/tan) related.

Basically I have this:

        if(pinballAngle > 90){
            pinballCoordinate.x+=(pinballAngle/3);
            pinballCoordinate.y-=20;
        }
        else if(pinballAngle < 90){
            pinballCoordinate.x+=(pinballAngle/3);
            pinballCoordinate.y-=20;
        }
        else{
            pinballCoordinate.y-=20;
        }

So pinballCoordinate is the Point object that I am trying to adjust given the angle of theta, that is the 'gun' that shoots the pinball. The 'pinballAngle' variable is the angle of the gun.

  • So you're asking how to work out sines and cosines in Java? – Dawood ibn Kareem Mar 21 '17 at 23:32
  • So, the question could boil down to a linear path following problem. That is, you have a start point, you have an angle and you want to follow a path away from the start point. Here I'm falling back to calculating a point on a circle to generate my target point and then I just need to follow a straight line between those points :P - It's probably a little convoluted, but I fall back on things I know how to solve when presented with issues like this ;) – MadProgrammer Mar 21 '17 at 23:35
  • See the example cited [here](http://stackoverflow.com/a/4550667/230513). – trashgod Mar 22 '17 at 08:12

1 Answers1

0

first, you will need your angle in radian

int speed = 20; // the speed of the bullet

double rad = pinballAngle * 2 * Math.PI / 360;
pinballCoordinate.x += (int) ( Math.cos(rad)*speed );
pinballCoordinate.y += (int) ( Math.sin(rad)*speed );

You will probably need to use the opposite in x or y, replace with some -= where it is needed.

the Math documentation: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

User9123
  • 396
  • 3
  • 8
  • `-=` is not needed, as sine and cosine already produce the correct positive/negative values. – GameGibu Mar 21 '17 at 23:51
  • You'll probably need `-=` for `y` but `+=` for `x`. – Dawood ibn Kareem Mar 21 '17 at 23:58
  • @DavidWallace that's true in the case that they are correcting for the inverted y-axis used by most layout/drawing utilities. The geometries are, in these cases, merely flipped vertically, such that rotations are represented in the opposite direction. – GameGibu Mar 22 '17 at 00:06
  • @GameGibu Yes, and it also depends on what pinballAngle means in the OP source code, aka if 0° is the left or right direction (in the picture, it's the left direction, so `-=` for x) – User9123 Mar 22 '17 at 00:23
  • How do you determine the vertical (y) or horizontal (x) distance when all the parameters (sides) of triangle are unknown? – hashmap Mar 22 '17 at 02:10
  • @NimitPatel what do you mean ? obviously you can't determine a unique triangle when no side lenght is fixed. Here, the hypotenuse is arbitrarily set to 20 – User9123 Mar 22 '17 at 02:16