BugFix: 木块无法向左推动
This commit is contained in:
1 parent
89ba4931bd
commit
7f1d935733
1 file changed
+68
-40
+68
-40
@@ -436,12 +436,13 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
<h3>🎮 推动木块实验</h3>
|
<h3>🎮 推动木块实验</h3>
|
||||||
<span class="exp-badge">拖拽或按住按钮</span>
|
<span class="exp-badge">拖拽或按住按钮</span>
|
||||||
</div>
|
</div>
|
||||||
<p class="exp-desc">点击并拖拽木块模拟推力,或按住"持续推"按钮。调节压力和粗糙度,观察静摩擦→滑动摩擦的转变。</p>
|
<p class="exp-desc">点击并拖拽木块(左右皆可)模拟推力,或按住"左推/右推"按钮。调节压力和粗糙度,观察静摩擦→滑动摩擦的转变。</p>
|
||||||
<div class="canvas-wrap">
|
<div class="canvas-wrap">
|
||||||
<canvas id="frictionCanvas" width="800" height="340"></canvas>
|
<canvas id="frictionCanvas" width="800" height="340"></canvas>
|
||||||
</div>
|
</div>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
<button class="btn" id="pushBtn">▶ 持续推</button>
|
<button class="btn secondary" id="pushLeftBtn">◀ 左推</button>
|
||||||
|
<button class="btn" id="pushBtn">▶ 右推</button>
|
||||||
<button class="btn secondary" id="resetBtn">↻ 重置木块</button>
|
<button class="btn secondary" id="resetBtn">↻ 重置木块</button>
|
||||||
<label>压力 N:<input type="range" id="normalSlider" min="50" max="300" value="100"><span class="val" id="normalVal">100</span> N</label>
|
<label>压力 N:<input type="range" id="normalSlider" min="50" max="300" value="100"><span class="val" id="normalVal">100</span> N</label>
|
||||||
<label>粗糙度 μ:<input type="range" id="muSlider" min="0.05" max="0.8" value="0.3" step="0.05"><span class="val" id="muVal">0.30</span></label>
|
<label>粗糙度 μ:<input type="range" id="muSlider" min="0.05" max="0.8" value="0.3" step="0.05"><span class="val" id="muVal">0.30</span></label>
|
||||||
@@ -850,6 +851,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 holdingLeft = false;
|
||||||
let dragging = false, dragTargetX = 0, dragOffsetX = 0, hoverBox = false;
|
let dragging = false, dragTargetX = 0, dragOffsetX = 0, hoverBox = false;
|
||||||
let squash = 0;
|
let squash = 0;
|
||||||
|
|
||||||
@@ -916,33 +918,47 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
ctx.textAlign = 'center';
|
ctx.textAlign = 'center';
|
||||||
ctx.fillText('m', bx + bw / 2, by + bh / 2 + 5);
|
ctx.fillText('m', bx + bw / 2, by + bh / 2 + 5);
|
||||||
ctx.textAlign = 'start';
|
ctx.textAlign = 'start';
|
||||||
if(appF > 0){
|
const absAppF = Math.abs(appF);
|
||||||
const aLen = Math.min(appF * 0.55, 150);
|
if(absAppF > 0.5){
|
||||||
drawArrow(bx - aLen - 18, by + bh / 2, bx - 5, by + bh / 2, '#4f8cff', 'F推=' + Math.round(appF) + 'N');
|
const aLen = Math.min(absAppF * 0.55, 150);
|
||||||
|
const label = 'F推=' + Math.round(absAppF) + 'N';
|
||||||
|
if(appF >= 0){
|
||||||
|
drawArrow(bx - aLen - 18, by + bh / 2, bx - 5, by + bh / 2, '#4f8cff', label);
|
||||||
|
} else {
|
||||||
|
drawArrow(bx + bw + aLen + 18, by + bh / 2, bx + bw + 5, by + bh / 2, '#4f8cff', label);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const maxS = maxStaticF();
|
const maxS = maxStaticF();
|
||||||
if(appF > 0){
|
if(absAppF > 0.5 || moving){
|
||||||
if(moving){
|
if(moving){
|
||||||
const fricVal = slidingF();
|
const fricVal = slidingF();
|
||||||
const fLen = Math.min(fricVal * 0.55, 150);
|
const fLen = Math.min(fricVal * 0.55, 150);
|
||||||
drawArrow(bx + bw + 5, by + bh / 2 + 18, bx + bw + fLen + 18, by + bh / 2 + 18, '#ff6b6b', 'f滑=' + fricVal.toFixed(1) + 'N');
|
if(vel > 0){
|
||||||
|
drawArrow(bx + bw + fLen + 18, by + bh / 2 + 18, bx + bw + 5, by + bh / 2 + 18, '#ff6b6b', 'f滑=' + fricVal.toFixed(1) + 'N');
|
||||||
|
} else {
|
||||||
|
drawArrow(bx - fLen - 18, by + bh / 2 + 18, bx - 5, by + bh / 2 + 18, '#ff6b6b', 'f滑=' + fricVal.toFixed(1) + 'N');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const fricVal = Math.min(appF, maxS);
|
const fricVal = Math.min(absAppF, maxS);
|
||||||
const fLen = Math.min(fricVal * 0.55, 150);
|
const fLen = Math.min(fricVal * 0.55, 150);
|
||||||
drawArrow(bx + bw + 5, by + bh / 2 + 18, bx + bw + fLen + 18, by + bh / 2 + 18, '#f76707', 'f静=' + fricVal.toFixed(1) + 'N');
|
if(appF >= 0){
|
||||||
|
drawArrow(bx + bw + fLen + 18, by + bh / 2 + 18, bx + bw + 5, by + bh / 2 + 18, '#f76707', 'f静=' + fricVal.toFixed(1) + 'N');
|
||||||
|
} else {
|
||||||
|
drawArrow(bx - fLen - 18, by + bh / 2 + 18, bx - 5, by + bh / 2 + 18, '#f76707', 'f静=' + fricVal.toFixed(1) + 'N');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drawArrow(bx + bw / 2, by - 5, bx + bw / 2, by - 50, '#5c7cfa', 'G=' + nF + 'N');
|
drawArrow(bx + bw / 2, by - 5, bx + bw / 2, by - 50, '#5c7cfa', 'G=' + nF + 'N');
|
||||||
drawArrow(bx + bw / 2, by + bh + 5, bx + bw / 2, by + bh + 50, '#51cf66', 'N=' + nF + 'N');
|
drawArrow(bx + bw / 2, by + bh + 5, bx + bw / 2, by + bh + 50, '#51cf66', 'N=' + nF + 'N');
|
||||||
ctx.fillStyle = moving ? '#c92a2a' : (appF > maxS ? '#e67700' : '#2b8a3e');
|
ctx.fillStyle = moving ? '#c92a2a' : (absAppF > maxS ? '#e67700' : '#2b8a3e');
|
||||||
ctx.font = 'bold 15px sans-serif';
|
ctx.font = 'bold 15px sans-serif';
|
||||||
const stateTxt = moving ? '▶ 运动中 (v=' + vel.toFixed(1) + ')' : (appF > maxS ? '即将滑动…' : '■ 静止');
|
const stateTxt = moving ? ('运动中 ' + (vel > 0 ? '→' : '←') + ' (v=' + Math.abs(vel).toFixed(1) + ')') : (absAppF > maxS ? '即将滑动…' : '■ 静止');
|
||||||
ctx.fillText(stateTxt, 20, 28);
|
ctx.fillText(stateTxt, 20, 28);
|
||||||
if(hoverBox && !dragging){
|
if(hoverBox && !dragging){
|
||||||
ctx.fillStyle = '#4f8cff';
|
ctx.fillStyle = '#4f8cff';
|
||||||
ctx.font = '12px sans-serif';
|
ctx.font = '12px sans-serif';
|
||||||
ctx.textAlign = 'center';
|
ctx.textAlign = 'center';
|
||||||
ctx.fillText('→ 拖拽木块施加推力 →', bx + bw / 2, by - 60);
|
ctx.fillText('← 拖拽木块施加推力 →', bx + bw / 2, by - 60);
|
||||||
ctx.textAlign = 'start';
|
ctx.textAlign = 'start';
|
||||||
}
|
}
|
||||||
if(dragging){
|
if(dragging){
|
||||||
@@ -979,10 +995,11 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
ctx.fillRect(barX, barY + 44, barW, barH);
|
ctx.fillRect(barX, barY + 44, barW, barH);
|
||||||
const maxS = maxStaticF();
|
const maxS = maxStaticF();
|
||||||
const sF = slidingF();
|
const sF = slidingF();
|
||||||
const appRatio = Math.min(appF / maxVal, 1);
|
const absAppF = Math.abs(appF);
|
||||||
|
const appRatio = Math.min(absAppF / maxVal, 1);
|
||||||
const maxSRatio = Math.min(maxS / maxVal, 1);
|
const maxSRatio = Math.min(maxS / maxVal, 1);
|
||||||
const sFRatio = Math.min(sF / maxVal, 1);
|
const sFRatio = Math.min(sF / maxVal, 1);
|
||||||
const appColor = appF > maxS ? '#ff6b6b' : '#4f8cff';
|
const appColor = absAppF > maxS ? '#ff6b6b' : '#4f8cff';
|
||||||
ctx.fillStyle = appColor;
|
ctx.fillStyle = appColor;
|
||||||
ctx.fillRect(barX, barY, barW * appRatio, barH);
|
ctx.fillRect(barX, barY, barW * appRatio, barH);
|
||||||
ctx.fillStyle = '#fcc419';
|
ctx.fillStyle = '#fcc419';
|
||||||
@@ -1012,7 +1029,7 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
ctx.textAlign = 'start';
|
ctx.textAlign = 'start';
|
||||||
ctx.fillStyle = appColor;
|
ctx.fillStyle = appColor;
|
||||||
ctx.font = 'bold 10px sans-serif';
|
ctx.font = 'bold 10px sans-serif';
|
||||||
ctx.fillText(Math.round(appF) + ' N', barX + barW + 5, barY + 11);
|
ctx.fillText(Math.round(absAppF) + ' N', barX + barW + 5, barY + 11);
|
||||||
ctx.fillStyle = '#b8860b';
|
ctx.fillStyle = '#b8860b';
|
||||||
ctx.fillText(maxS.toFixed(1) + ' N', barX + barW + 5, barY + 33);
|
ctx.fillText(maxS.toFixed(1) + ' N', barX + barW + 5, barY + 33);
|
||||||
ctx.fillStyle = '#c92a2a';
|
ctx.fillStyle = '#c92a2a';
|
||||||
@@ -1053,60 +1070,64 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
function updatePhysics(){
|
function updatePhysics(){
|
||||||
const maxS = maxStaticF();
|
const maxS = maxStaticF();
|
||||||
const sF = slidingF();
|
const sF = slidingF();
|
||||||
|
const mass = nF / 10;
|
||||||
if(holding){
|
if(holding){
|
||||||
forceTarget = Math.min(forceTarget + 4, maxAppF);
|
forceTarget = Math.min(forceTarget + 4, maxAppF);
|
||||||
|
} else if(holdingLeft){
|
||||||
|
forceTarget = Math.max(forceTarget - 4, -maxAppF);
|
||||||
} else if(dragging){
|
} else if(dragging){
|
||||||
const boxCenter = box.x + box.w / 2;
|
const boxCenter = box.x + box.w / 2;
|
||||||
const pull = dragTargetX - boxCenter;
|
const pull = dragTargetX - boxCenter;
|
||||||
forceTarget = Math.max(0, Math.min(pull * 2.5, maxAppF));
|
forceTarget = Math.max(-maxAppF, Math.min(pull * 2.5, maxAppF));
|
||||||
} else {
|
} else {
|
||||||
forceTarget *= 0.92;
|
forceTarget *= 0.92;
|
||||||
if(forceTarget < 0.5) forceTarget = 0;
|
if(Math.abs(forceTarget) < 0.5) forceTarget = 0;
|
||||||
}
|
}
|
||||||
appF += (forceTarget - appF) * 0.25;
|
appF += (forceTarget - appF) * 0.25;
|
||||||
if(appF <= maxS + 0.5){
|
const absAppF = Math.abs(appF);
|
||||||
|
if(Math.abs(vel) < 0.01 && absAppF <= maxS + 0.5){
|
||||||
|
vel = 0;
|
||||||
moving = false;
|
moving = false;
|
||||||
if(vel > 0.01){
|
|
||||||
vel -= sF / (nF / 10) * 0.016;
|
|
||||||
if(vel < 0) vel = 0;
|
|
||||||
} else {
|
|
||||||
vel = 0;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
moving = true;
|
moving = true;
|
||||||
const mass = nF / 10;
|
vel += (appF / mass) * 0.016;
|
||||||
const accel = (appF - sF) / mass;
|
if(Math.abs(vel) > 0.01){
|
||||||
vel += accel * 0.016;
|
const fricStep = (sF / mass) * 0.016;
|
||||||
|
if(vel > 0) vel = Math.max(0, vel - fricStep);
|
||||||
|
else vel = Math.min(0, vel + fricStep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(moving && vel > 0){
|
if(moving){
|
||||||
box.x += vel * 0.016 * 60;
|
box.x += vel * 0.016 * 60;
|
||||||
if(box.x < 20){ box.x = 20; vel = 0; moving = false; }
|
if(box.x < 20){ box.x = 20; vel = 0; moving = false; }
|
||||||
if(box.x + box.w > W - 20){ box.x = W - 20 - box.w; vel = 0; moving = false; }
|
if(box.x + box.w > W - 20){ box.x = W - 20 - box.w; vel = 0; moving = false; }
|
||||||
} else if(!moving){
|
|
||||||
if(box.x < 160){
|
|
||||||
box.x += (160 - box.x) * 0.05;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
squash *= 0.85;
|
squash *= 0.85;
|
||||||
if(moving && vel > 5) squash = Math.min(vel / 40, 0.6);
|
if(moving && Math.abs(vel) > 5) squash = Math.min(Math.abs(vel) / 40, 0.6);
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateUI(){
|
function updateUI(){
|
||||||
document.getElementById('forceVal').textContent = Math.round(appF) + ' N';
|
document.getElementById('forceVal').textContent = Math.round(Math.abs(appF)) + ' N';
|
||||||
document.getElementById('normalVal').textContent = nF;
|
document.getElementById('normalVal').textContent = nF;
|
||||||
document.getElementById('muVal').textContent = mu.toFixed(2);
|
document.getElementById('muVal').textContent = mu.toFixed(2);
|
||||||
const maxS = maxStaticF();
|
const maxS = maxStaticF();
|
||||||
const sF = slidingF();
|
const sF = slidingF();
|
||||||
document.getElementById('maxStatic').textContent = maxS.toFixed(2) + ' N';
|
document.getElementById('maxStatic').textContent = maxS.toFixed(2) + ' N';
|
||||||
document.getElementById('slidingF').textContent = sF.toFixed(2) + ' N';
|
document.getElementById('slidingF').textContent = sF.toFixed(2) + ' N';
|
||||||
if(appF > 0){
|
const absAppF = Math.abs(appF);
|
||||||
if(appF <= maxS){
|
if(moving){
|
||||||
document.getElementById('frictionForce').textContent = '静摩擦 ' + Math.min(appF, maxS).toFixed(2) + ' N';
|
const dir = vel > 0 ? '→' : '←';
|
||||||
document.getElementById('motionState').textContent = '静止 (静摩擦)';
|
document.getElementById('frictionForce').textContent = '滑动摩擦 ' + sF.toFixed(2) + ' N';
|
||||||
|
document.getElementById('motionState').textContent = '运动中 ' + dir + ' (滑动摩擦)';
|
||||||
|
} else if(absAppF > 0.5){
|
||||||
|
const dir = appF > 0 ? '→' : '←';
|
||||||
|
if(absAppF <= maxS){
|
||||||
|
document.getElementById('frictionForce').textContent = '静摩擦 ' + absAppF.toFixed(2) + ' N';
|
||||||
|
document.getElementById('motionState').textContent = '静止 ' + dir + ' (静摩擦)';
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('frictionForce').textContent = '滑动摩擦 ' + sF.toFixed(2) + ' N';
|
document.getElementById('frictionForce').textContent = '静摩擦 ' + maxS.toFixed(2) + ' N';
|
||||||
document.getElementById('motionState').textContent = '运动中 (滑动摩擦)';
|
document.getElementById('motionState').textContent = '即将滑动 ' + dir + '…';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('frictionForce').textContent = '0.00 N';
|
document.getElementById('frictionForce').textContent = '0.00 N';
|
||||||
@@ -1173,6 +1194,13 @@ l <p class="section-sub">力是物体对物体的作用,力不能脱离物体
|
|||||||
pushBtn.addEventListener('touchstart', (e) => { e.preventDefault(); holding = true; });
|
pushBtn.addEventListener('touchstart', (e) => { e.preventDefault(); holding = true; });
|
||||||
pushBtn.addEventListener('touchend', () => { holding = false; });
|
pushBtn.addEventListener('touchend', () => { holding = false; });
|
||||||
|
|
||||||
|
const pushLeftBtn = document.getElementById('pushLeftBtn');
|
||||||
|
pushLeftBtn.addEventListener('mousedown', () => { holdingLeft = true; });
|
||||||
|
pushLeftBtn.addEventListener('mouseup', () => { holdingLeft = false; });
|
||||||
|
pushLeftBtn.addEventListener('mouseleave', () => { holdingLeft = false; });
|
||||||
|
pushLeftBtn.addEventListener('touchstart', (e) => { e.preventDefault(); holdingLeft = true; });
|
||||||
|
pushLeftBtn.addEventListener('touchend', () => { holdingLeft = false; });
|
||||||
|
|
||||||
document.getElementById('resetBtn').addEventListener('click', () => {
|
document.getElementById('resetBtn').addEventListener('click', () => {
|
||||||
box.x = 160;
|
box.x = 160;
|
||||||
vel = 0;
|
vel = 0;
|
||||||
|
|||||||
Reference in new issue
Block a user