[英]My implementation of collision detection doesn't behave normally
I've been trying to use some simple force implementations in p5.js, I have the concept in mind but I feel like I overlooked a simple error in my code. 我一直在尝试在p5.js中使用一些简单的强制实现,我有这个概念,但我觉得我忽略了代码中的一个简单错误。 This is my code right now:
这是我现在的代码:
var x = 0;
var y = 0;
var grav = new p5.Vector(0, 13, 0);
var bounce = new p5.Vector(0, -13, 0);
var isbounce = false;
var ymax = 0;
function setup() {
createCanvas(600, 400);
}
function draw() {
console.log(ymax, isbounce);
background(0);
fill(255);
ellipse(200, y, 50, 50);
if (y < 376 && isbounce == false) {
y += grav.y;
}
if (y >= 376) {
isbounce = true;
}
if (isbounce == true) {
y += bounce.y;
if (y == ymax) {
isbounce = false;
ymax++;
}
}
}
As you can see, the object does bounce twice, but then it completely floats off the screen. 正如您所看到的,对象会弹跳两次,但随后它会完全浮出屏幕。 I can't explain this, I looked at the console and it confirmed that it only bounced twice (setting
isbounce
to true, false and true again). 我无法解释这一点,我看着控制台,它确认它只反弹两次(设置
isbounce
为true,false和true再次)。 What I'm expecting is that isbounce
will be set to true everytime the object hits the ground, and to false whenever its y value reaches ymax (which decreases everytime it bounces to stimulate energy loss). 我期待的是,每当物体撞击地面时,
isbounce
将被设置为true,并且当y值达到ymax时(每当它反弹以减少刺激能量损失时,它将减小)。 I thought this was a rather easy implementation of forces, but I'm puzzled as to why it doesn't work. 我认为这是一个相当容易实现的力量,但我很困惑为什么它不起作用。
替换if (y == ymax) {
by if (y <= ymax) {
以确保它在屏幕顶部反弹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.