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

Thread: Mistake in formula?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Mistake in formula?

    Hello everyone!

    I have to make a tank-game in Java for school
    Obviously the tank should be able to shoot, in a parabola
    Our teacher provided us with the formula to calculate the y-coordinate for by looping through a given amount of x-coordinates
    Printscreen of the formula: formule.png

    G = 9.81 m/s^2 (gravitation constant)
    alpha = the angle under which the shot is fired
    v0 is the power of the shot
    w = the wind
    x = the x-value of the map
    y0 = y-value of the tank
    y = path of bullet

    Now, I *tried* to convert it to Java and I came up with this:
    coord.setY(
    (int)
    (((-G/2) / Math.pow(power * Math.cos(angle) - wind, 2) * Math.pow(currentX, 2)) + ((currentX * Math.sin(angle)) / (Math.cos(angle) - (wind / power))) + tankPosition.getY())
    );
    I've put it in a loop and add the coord object to an arraylist so I get the path of the bullet

    If I for example enter an angle of 20 and power of 30, this would be the content of the arraylist:
    X - Y
    160 - -13778
    161 - -13950
    162 - -14123
    163 - -14296
    164 - -14471
    165 - -14647
    166 - -14824
    167 - -15002
    168 - -15181
    169 - -15362
    170 - -15543
    171 - -15725
    172 - -15908
    173 - -16092
    174 - -16277
    175 - -16464
    176 - -16651
    177 - -16839
    178 - -17029
    179 - -17219
    180 - -17411
    181 - -17603
    182 - -17796
    183 - -17991
    184 - -18187
    185 - -18383
    186 - -18581
    187 - -18779
    188 - -18979
    189 - -19180
    190 - -19381
    191 - -19584
    192 - -19788
    193 - -19993
    194 - -20199
    195 - -20406
    196 - -20613
    197 - -20822
    198 - -21032
    199 - -21243
    200 - -21455
    201 - -21669
    202 - -21883
    203 - -22098
    204 - -22314
    205 - -22531
    206 - -22749
    207 - -22969
    208 - -23189
    209 - -23410
    Did I do something wrong in converting the formula to java or something? :/

    Thanks for your time!



    Actual code:
    public ArrayList<Coord> createPath(int power, int angle, int wind, Coord tankPosition) {
        final double G = 9.81;
        Coord coord;
        boolean shouldStop = false;
        ArrayList<Coord> list = new ArrayList<>();
        int currentX = tankPosition.getX();
        while (!shouldStop) {
            coord = new Coord();
            coord.setX(currentX);
            coord.setY((int) (((-G / 2) / Math.pow(power * Math.cos(angle) - wind, 2) * Math.pow(currentX, 2)) + ((currentX * Math.sin(angle)) / (Math.cos(angle) - (wind / power))) + tankPosition.getY()));  
            list.add(coord);
            currentX++;
            if (isOutOfBounds(coord) || isCollision(coord))
                shouldStop = true;
        }
        return list;
    }

    Thanks for your time!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Mistake in formula?

    Have you run some numbers through the formula manually? Did you get good results? If so, then work on seeing why the program doesn't work.

    Break the huge formula up into simple expressions and print out the results of each expression.
    Manually compute the results for each expression
    compare the program generated results with the manual results.
    Resolve any differences. If there is an expression that you need help with, copy it and the results it generates along with the manual results and paste it here.


    One problem might be integer arithmetic: 3/2 = 1 NOT 1.5
    Use floating point numbers: 3/2.0 = 1.5
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Can't find mistake in my code
    By Daler in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 12:19 AM
  2. A simple mistake I can't fix
    By xdx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 5th, 2012, 05:50 AM
  3. Please, find my mistake
    By Daler in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2012, 11:17 PM
  4. where located my mistake
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2012, 10:31 AM
  5. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM