如何在 javascript 上创建一个简单的重力引擎
How to create a simple gravity engine on javascript
我正在尝试使用 P5.js 库在 javascript 中制作一个简单的动画。我想让一个球出现在某个高度,然后让它掉下来,让它弹跳直到停下来。
我不是在寻找外部库,只是 P5。
我的代码是这样的:
function Ball() {
this.diameter = 50;
this.v_speed = 5;
this.ypos = height/2 - 100;
this.xpos = width/2;
this.update = function(){
this.ypos = this.ypos + this.v_speed;
this.ypos = constrain(this.ypos, this.diameter/2, height-this.diameter/2);
}
this.show = function(){
ellipse(this.xpos, this.ypos, this.diameter);
fill(255);
}
}
var ball;
function setup() {
createCanvas(600, 600);
ball = new Ball();
}
function draw() {
background(0);
ball.update();
ball.show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
有人可以帮助我吗?非常感谢
这很简单,这是一种可行的(但粗略的)方法:
var velY = 0;
var yPos = 0;
draw = function() {
velY += 0.1;//Make this value higher for stronger gravity.
yPos += velY;
if (yPos > 400) {
velY = -velY;
yPos = 400;
}//This may need a little tweaking.
ellipse(300, yPos, this.diameter);
};
首先您必须将起始速度设置为 0 并定义重力:
this.v_speed = 0;
this.gravity = 0.2;
一个有效的 update
方法,可以直接应用于您的示例,如下所示:
this.starty = height/2 - 100;
this.endy = height-this.diameter/2;
this.update = function(){
this.v_speed = this.v_speed + this.gravity;
this.ypos = this.ypos + this.v_speed;
if (this.ypos >= this.endy){
this.ypos = this.endy;
this.v_speed *= -1.0; // change direction
this.v_speed = this.v_speed*0.9;
if ( Math.abs(this.v_speed) < 0.5 ) {
this.ypos = this.starty;
}
}
}
关键是球在地上弹跳时要降低速度和改变方向:
this.v_speed *= -1.0;
this.v_speed = this.v_speed*0.9;
另见 Bouncing Balls, struggling with getting more than 1 (processing)。
查看示例,其中我将建议应用于您的原始代码:
function Ball() {
this.diameter = 50;
this.v_speed = 0;
this.gravity = 0.2;
this.starty = height/2 - 100;
this.endy = height-this.diameter/2;
this.ypos = this.starty;
this.xpos = width/2;
this.update = function(){
this.v_speed = this.v_speed + this.gravity;
this.ypos = this.ypos + this.v_speed;
if (this.ypos >= this.endy){
this.ypos = this.endy;
this.v_speed *= -1.0; // change direction
this.v_speed = this.v_speed*0.9;
if ( Math.abs(this.v_speed) < 0.5 ) {
this.ypos = this.starty;
}
}
}
this.show = function(){
ellipse(this.xpos, this.ypos, this.diameter);
fill(255);
}
}
var ball;
function setup() {
createCanvas(600, 600);
ball = new Ball();
}
function draw() {
background(0);
ball.update();
ball.show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>
我正在尝试使用 P5.js 库在 javascript 中制作一个简单的动画。我想让一个球出现在某个高度,然后让它掉下来,让它弹跳直到停下来。
我不是在寻找外部库,只是 P5。
我的代码是这样的:
function Ball() {
this.diameter = 50;
this.v_speed = 5;
this.ypos = height/2 - 100;
this.xpos = width/2;
this.update = function(){
this.ypos = this.ypos + this.v_speed;
this.ypos = constrain(this.ypos, this.diameter/2, height-this.diameter/2);
}
this.show = function(){
ellipse(this.xpos, this.ypos, this.diameter);
fill(255);
}
}
var ball;
function setup() {
createCanvas(600, 600);
ball = new Ball();
}
function draw() {
background(0);
ball.update();
ball.show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>
有人可以帮助我吗?非常感谢
这很简单,这是一种可行的(但粗略的)方法:
var velY = 0;
var yPos = 0;
draw = function() {
velY += 0.1;//Make this value higher for stronger gravity.
yPos += velY;
if (yPos > 400) {
velY = -velY;
yPos = 400;
}//This may need a little tweaking.
ellipse(300, yPos, this.diameter);
};
首先您必须将起始速度设置为 0 并定义重力:
this.v_speed = 0;
this.gravity = 0.2;
一个有效的 update
方法,可以直接应用于您的示例,如下所示:
this.starty = height/2 - 100;
this.endy = height-this.diameter/2;
this.update = function(){
this.v_speed = this.v_speed + this.gravity;
this.ypos = this.ypos + this.v_speed;
if (this.ypos >= this.endy){
this.ypos = this.endy;
this.v_speed *= -1.0; // change direction
this.v_speed = this.v_speed*0.9;
if ( Math.abs(this.v_speed) < 0.5 ) {
this.ypos = this.starty;
}
}
}
关键是球在地上弹跳时要降低速度和改变方向:
this.v_speed *= -1.0;
this.v_speed = this.v_speed*0.9;
另见 Bouncing Balls, struggling with getting more than 1 (processing)。
查看示例,其中我将建议应用于您的原始代码:
function Ball() {
this.diameter = 50;
this.v_speed = 0;
this.gravity = 0.2;
this.starty = height/2 - 100;
this.endy = height-this.diameter/2;
this.ypos = this.starty;
this.xpos = width/2;
this.update = function(){
this.v_speed = this.v_speed + this.gravity;
this.ypos = this.ypos + this.v_speed;
if (this.ypos >= this.endy){
this.ypos = this.endy;
this.v_speed *= -1.0; // change direction
this.v_speed = this.v_speed*0.9;
if ( Math.abs(this.v_speed) < 0.5 ) {
this.ypos = this.starty;
}
}
}
this.show = function(){
ellipse(this.xpos, this.ypos, this.diameter);
fill(255);
}
}
var ball;
function setup() {
createCanvas(600, 600);
ball = new Ball();
}
function draw() {
background(0);
ball.update();
ball.show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>