Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am making a Javascript program that includes physics but the projectile formula is very difficult for me to get right. My code looks something like this
//VARIABLES

//speed
var speed = 0;

//angle
var angle = 0;

//previous coord log
var pballx = 0;
var pbally = 0;

var ppballx = 0;
var ppbally = 0;

//ball variables
var ballx = 200;
var bally = 200;
var ballsize = 20;

//height of grass
var grassheight = 380;

//COUNTERS

//ball fall counter
var cn1 = 1;
//ball fall counters
var bffasttime = 0;
var bftime = bffasttime/60;
//log counter
var lftime = 0;
var ltime = 0;
//bally counter
var ballcn = 0;

mouseDragged = function(){
        lftime += 1;
        ltime = lftime/60;
        if(ltime >= 0.3){
            ppballx = pballx;
            ppbally = pbally;
            pballx = ballx;
            pbally = bally;
            lftime = 0;
        }
        if(ballx+ballsize/2 >= mouseX){
            if(mouseX >= ballx-ballsize/2){
               if(mouseY >= bally-ballsize/2){
                    if(mouseY >= bally-ballsize/2){
                        ballx = mouseX;
                        bally = mouseY;
                
                    }
                } 
            }
        }
    };
    
    //collision box for grass
    if(bally >= grassheight - ballsize/2){
        bally = grassheight - ballsize/2;
    }
    
    //collision box for side of wall left
    if(ballx <= 0 + ballsize/2){
        ballx = 0 + ballsize/2;
    }
    
    //collision box for side of wall right
    if(ballx >= 400 - ballsize/2){
        ballx = 400 - ballsize/2;
    }
    
    //intiates fall
    mouseReleased = function(){
        cn1 += 1;
        
    };
    
    //makes ball fall
    if(cn1 >= 1){
        bffasttime += 1;
        bftime = bffasttime/60;
        if(ballx === pballx){
            if(bally === pbally){
                ballcn = 1;
                speed = sqrt(sq(abs(ballx-pballx))+(sq(abs(bally-pbally))));
                angle = atan((bally - ppbally)/(ballx - ppballx))*(180/Math.Pi);
                if(bally < grassheight - ballsize/2){
                    bally += (tan(angle))*ballx-(9.8/sq(bftime))/(2*sq(speed)*sq(cos(angle)))*sq(ballx);
                }
            }
            }else{
                speed = sqrt(sq(abs(ballx-pballx))+(sq(abs(bally-pbally))));
                angle = atan((bally - pbally)/(ballx - pballx))*(180/Math.Pi);
                if(bally < grassheight - ballsize/2){
                    bally += (tan(angle))*ballx-(9.8/sq(bftime))/(2*sq(speed)*sq(cos(angle)))*sq(ballx);
                }
            }
        if(ballcn === 0){
            if(bally === pbally){
                if(ballx === pballx){
                    speed = sqrt(sq(abs(ballx-pballx))+(sq(abs(bally-pbally))));
                    angle = atan((bally - ppbally)/(ballx - ppballx))*(180/Math.Pi);
                    if(bally < grassheight - ballsize/2){
                        bally += ((tan(angle))*ballx)-((9.8/sq(bftime))/(2*sq(speed)*sq(cos(angle)))*sq(ballx));
                    }
                }
                }else{
                    speed = sqrt(sq(abs(ballx-pballx))+(sq(abs(bally-pbally))));
                    angle = atan((bally - pbally)/(ballx - pballx))*(180/Math.Pi);
                    if(bally < grassheight - ballsize/2){
                    bally += ((tan(angle))*ballx)-((9.8/sq(bftime))/(2*sq(speed)*sq(cos(angle)))*sq(ballx));
                    }
                }
        }
    }
    //resets counter for fall
    if(bally >=  grassheight - ballsize/2){
        cn1 = 0;
        bffasttime = 0;
        bftime = 0;
    }
    mouseOut = function(){
        cn1 = 1;
    };
};


What I have tried:

I have tried changing up the formula and looking for other formulas and changing variables along with other things.
Posted
Updated 25-Feb-17 17:51pm

1 solution

reread your code !
JavaScript
if(ballx+ballsize/2 >= mouseX){
    if(mouseX >= ballx-ballsize/2){
       if(mouseY >= bally-ballsize/2){ // this line is the same as
           if(mouseY >= bally-ballsize/2){ // this one
                ballx = mouseX;
                bally = mouseY;

            }
        }
    }
}


Quote:
I have tried changing up the formula and looking for other formulas and changing variables along with other things.

Something go wrong, never change random things in hope to solve the problem, unless you have some time to loose.
Instead, use the debugger integrated in your browser or something like FireBug.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900