如何在JS程序中添加物理? [英] How do I add physics to my JS program?

查看:68
本文介绍了如何在JS程序中添加物理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含物理学的Javascript程序,但是弹丸配方很难让我做对。我的代码看起来像这样

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.

推荐答案

重读代码!

reread your code !
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.



出了问题,永远不要改变随机的东西,希望能解决问题,除非你有时间放松。

相反,使用集成的调试器你的浏览器或类似FireBug的东西。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是 debugger

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


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.


这篇关于如何在JS程序中添加物理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆