【发布时间】:2019-04-25 17:16:22
【问题描述】:
我用 P5.js 创建的简单游戏包含一个受重力影响下落并在地面上弹跳的球。我想在球触地时为球添加“压缩”动画,使其看起来更逼真。
我怎样才能不让它看起来很奇怪?
代码是这样的:
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.8.0/p5.js"></script>
【问题讨论】:
-
球运动得越快,压缩得越厉害吗?
-
在现实世界中应该是这样的,但是一旦设置了机制,就很容易调整。我只需要基本的动画代码,稍后我会调整它。
标签: javascript canvas processing p5.js