BugFix: 优化摩檫力演示交互、修复自由落体下落时快速点击开启空气阻力会触发消失物体的BUG

This commit is contained in:
xiaozhou233 committed 2026-08-01 11:03:53 +08:00
1 parent 2e0e2ff934
commit 89ba4931bd
1 file changed
+40 -30
+40 -30
View File
@@ -577,13 +577,17 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
if(running){ if(running){
const elapsed = (t - startTime)/1000; const elapsed = (t - startTime)/1000;
if(!landedBig){ if(!landedBig){
const a = airResistance ? g - 0.5 * balls.big.vy * balls.big.vy / balls.big.mass : g; const a = airResistance ? g - 0.001 * balls.big.vy * balls.big.vy / balls.big.mass : g;
balls.big.vy += a * dt * 50; balls.big.y += balls.big.vy * dt * 2; balls.big.vy += a * dt * 50;
if(balls.big.vy < 0) balls.big.vy = 0;
balls.big.y += balls.big.vy * dt * 2;
if(balls.big.y >= floorY - balls.big.r){ balls.big.y = floorY - balls.big.r; balls.big.vy = 0; landedBig = true; timeBig = elapsed; } if(balls.big.y >= floorY - balls.big.r){ balls.big.y = floorY - balls.big.r; balls.big.vy = 0; landedBig = true; timeBig = elapsed; }
} }
if(!landedSmall){ if(!landedSmall){
const a = airResistance ? g - 0.5 * balls.small.vy * balls.small.vy / balls.small.mass : g; const a = airResistance ? g - 0.001 * balls.small.vy * balls.small.vy / balls.small.mass : g;
balls.small.vy += a * dt * 50; balls.small.y += balls.small.vy * dt * 2; balls.small.vy += a * dt * 50;
if(balls.small.vy < 0) balls.small.vy = 0;
balls.small.y += balls.small.vy * dt * 2;
if(balls.small.y >= floorY - balls.small.r){ balls.small.y = floorY - balls.small.r; balls.small.vy = 0; landedSmall = true; timeSmall = elapsed; } if(balls.small.y >= floorY - balls.small.r){ balls.small.y = floorY - balls.small.r; balls.small.vy = 0; landedSmall = true; timeSmall = elapsed; }
} }
if(landedBig && landedSmall){ running = false; updateGravityUI(); } if(landedBig && landedSmall){ running = false; updateGravityUI(); }
@@ -846,7 +850,7 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
let appF = 0, maxAppF = 300; let appF = 0, maxAppF = 300;
let forceTarget = 0; let forceTarget = 0;
let holding = false; let holding = false;
let dragging = false, dragStartX = 0, dragBoxStartX = 0, hoverBox = false; let dragging = false, dragTargetX = 0, dragOffsetX = 0, hoverBox = false;
let squash = 0; let squash = 0;
const muK = 0.8; const muK = 0.8;
@@ -941,6 +945,26 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
ctx.fillText('→ 拖拽木块施加推力 →', bx + bw / 2, by - 60); ctx.fillText('→ 拖拽木块施加推力 →', bx + bw / 2, by - 60);
ctx.textAlign = 'start'; ctx.textAlign = 'start';
} }
if(dragging){
const boxCenter = box.x + box.w / 2;
const cy = box.y + box.h / 2;
const tx = Math.max(20, Math.min(W - 20, dragTargetX));
ctx.strokeStyle = 'rgba(79,140,255,.6)';
ctx.lineWidth = 2;
ctx.setLineDash([5, 4]);
ctx.beginPath();
ctx.moveTo(tx, cy);
ctx.lineTo(boxCenter, cy);
ctx.stroke();
ctx.setLineDash([]);
ctx.fillStyle = 'rgba(79,140,255,.3)';
ctx.beginPath();
ctx.arc(tx, cy, 7, 0, Math.PI*2);
ctx.fill();
ctx.strokeStyle = '#4f8cff';
ctx.lineWidth = 2;
ctx.stroke();
}
} }
function drawForceBars(){ function drawForceBars(){
@@ -1031,7 +1055,11 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
const sF = slidingF(); const sF = slidingF();
if(holding){ if(holding){
forceTarget = Math.min(forceTarget + 4, maxAppF); forceTarget = Math.min(forceTarget + 4, maxAppF);
} else if(!dragging){ } else if(dragging){
const boxCenter = box.x + box.w / 2;
const pull = dragTargetX - boxCenter;
forceTarget = Math.max(0, Math.min(pull * 2.5, maxAppF));
} else {
forceTarget *= 0.92; forceTarget *= 0.92;
if(forceTarget < 0.5) forceTarget = 0; if(forceTarget < 0.5) forceTarget = 0;
} }
@@ -1090,22 +1118,14 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
const p = toCanvasXY(e.clientX, e.clientY); const p = toCanvasXY(e.clientX, e.clientY);
hoverBox = p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h; hoverBox = p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h;
canvas.style.cursor = hoverBox ? 'grab' : 'default'; canvas.style.cursor = hoverBox ? 'grab' : 'default';
if(dragging){
const dx = p.x - dragStartX;
const newForce = Math.max(0, Math.min(dx * 3, maxAppF));
forceTarget = newForce;
appF = newForce;
dragBoxStartX = box.x;
dragStartX = p.x;
}
}); });
canvas.addEventListener('mousedown', (e) => { canvas.addEventListener('mousedown', (e) => {
const p = toCanvasXY(e.clientX, e.clientY); const p = toCanvasXY(e.clientX, e.clientY);
if(hoverBox){ if(hoverBox){
dragging = true; dragging = true;
dragStartX = p.x; dragTargetX = box.x + box.w / 2;
dragBoxStartX = box.x; dragOffsetX = p.x - (box.x + box.w / 2);
canvas.style.cursor = 'grabbing'; canvas.style.cursor = 'grabbing';
} }
}); });
@@ -1113,12 +1133,7 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
window.addEventListener('mousemove', (e) => { window.addEventListener('mousemove', (e) => {
if(!dragging) return; if(!dragging) return;
const p = toCanvasXY(e.clientX, e.clientY); const p = toCanvasXY(e.clientX, e.clientY);
const dx = p.x - dragStartX; dragTargetX = p.x - dragOffsetX;
const newForce = Math.max(0, Math.min(dx * 3, maxAppF));
forceTarget = newForce;
appF = newForce;
dragBoxStartX = box.x;
dragStartX = p.x;
}); });
window.addEventListener('mouseup', () => { window.addEventListener('mouseup', () => {
@@ -1134,8 +1149,8 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
const p = toCanvasXY(t.clientX, t.clientY); const p = toCanvasXY(t.clientX, t.clientY);
if(p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h){ if(p.x >= box.x && p.x <= box.x + box.w && p.y >= box.y && p.y <= box.y + box.h){
dragging = true; dragging = true;
dragStartX = p.x; dragTargetX = box.x + box.w / 2;
dragBoxStartX = box.x; dragOffsetX = p.x - (box.x + box.w / 2);
} }
}, { passive: false }); }, { passive: false });
@@ -1144,12 +1159,7 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
if(!dragging) return; if(!dragging) return;
const t = e.touches[0]; const t = e.touches[0];
const p = toCanvasXY(t.clientX, t.clientY); const p = toCanvasXY(t.clientX, t.clientY);
const dx = p.x - dragStartX; dragTargetX = p.x - dragOffsetX;
const newForce = Math.max(0, Math.min(dx * 3, maxAppF));
forceTarget = newForce;
appF = newForce;
dragBoxStartX = box.x;
dragStartX = p.x;
}, { passive: false }); }, { passive: false });
window.addEventListener('touchend', () => { window.addEventListener('touchend', () => {