Как добавить физику в свою программу JS?
Я создаю программу Javascript, которая включает в себя физику, но формулу снаряда мне очень трудно понять правильно. Мой код выглядит примерно так
//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; }; };
Что я уже пробовал:
Я пытался изменить формулу и искать другие формулы и изменять переменные вместе с другими вещами.