我正在制作一个简单的项目,其中一个点绕另一个点旋转。我找不到一个数学公式来根据与重力成比例的运动方向来改变点的速度。我还没有采用物理学,所以这对我来说仍然很难。
var c = document.createElement("canvas");
c.width = window.innerWidth;
c.height = window.innerHeight;
c.style = "position:absolute; top: 0; left: 0; background-color: black;";
document.body.appendChild(c);
var ctx = c.getContext("2d");
var shipX = 50;
var shipY = 0;
var moveDir = 0;
var speed = 8;
var halfX = window.innerWidth/2;
var halfY = window.innerHeight/2;
var gravSpeed = 0.7;
function pixelOn(x,y){
ctx.fillStyle = "white";
ctx.fillRect(x,y,2,2);
}
function draw(){
pixelOn(halfX,halfY);
pixelOn(shipX+halfX,halfY-shipY);
shipX += Math.sin(moveDir*Math.PI/180)*speed;
shipY += Math.cos(moveDir*Math.PI/180)*speed;
var gravDir = Math.atan2(-shipX,-shipY);
var nd = Math.atan2(Math.sin(moveDir*Math.PI/180-gravDir), Math.cos(moveDir*Math.PI/180-gravDir))*180/Math.PI;
moveDir -= nd/(speed/gravSpeed);
//change speed on movement direction here
}
setInterval(function(){
draw();
},50);