This commit is contained in:
2026-01-23 18:22:05 -08:00
parent 435eb0b128
commit 853728624a

View File

@@ -131,7 +131,7 @@
<!-- <polygon class="wall" points="-10,10 10,30 -10,40 -20,20" /> -->
<!-- <polygon class="wall" points="-20,-10 0,10 -20,20 -30,0" /> -->
<!-- <polygon class="wall" points="-20,-10 20,10 -20,20 -30,0" /> -->
<polygon class="wall" points="-20,-10 20,10 -20,20 -30,0" />
<!-- <polygon class="wall" points="-20,-50 -10,-50 -10,-60 -20,-60" /> -->
<!-- <polygon class="wall" points="20,50 10,50 10,60 20,60" /> -->
@@ -536,46 +536,58 @@ const Move = (() => {
return vector;
}
const vec1 = vector(v.x, v.y);
const vec2 = vector(run, rise);
// From https://stackoverflow.com/a/14886099
function bounceVector(v, run, rise) {
const vec1 = vector(v.x, v.y);
const vec2 = vector(run, rise);
// 1. Find the dot product of vec1 and vec2
// Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
// var dpA:Number = vec1.vx * vec2.dx + vec1.vy * vec2.dy;
const dpA = vec1.x * vec2.dx + vec1.y * vec2.dy;
// From https://stackoverflow.com/a/14886099
// 2. Project vec1 over vec2
// var prA_vx:Number = dpA * vec2.dx;
const prAvx = dpA * vec2.dx;
// var prA_vy:Number = dpA * vec2.dy;
const prAvy = dpA * vec2.dy;
// 1. Find the dot product of vec1 and vec2
// Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
// var dpA:Number = vec1.vx * vec2.dx + vec1.vy * vec2.dy;
const dpA = vec1.x * vec2.dx + vec1.y * vec2.dy;
// 3. Find the dot product of vec1 and vec2's normal
// (left or right normal depending on line's direction, let's say left)
// vec.leftNormal --> vx = vec.vy; vy = -vec.vx;
// vec.rightNormal --> vx = -vec.vy; vy = vec.vx;
// var dpB:Number = vec1.vx * vec2.leftNormal.dx + vec1.vy * vec2.leftNormal.dy;
const dpB = vec1.x * vec2.rightNormal.dx + vec1.y * vec2.rightNormal.dy;
// 2. Project vec1 over vec2
// var prA_vx:Number = dpA * vec2.dx;
const prAvx = dpA * vec2.dx;
// var prA_vy:Number = dpA * vec2.dy;
const prAvy = dpA * vec2.dy;
// 4. Project vec1 over vec2's left normal
// var prB_vx:Number = dpB * vec2.leftNormal.dx;
const prBvx = dpB * vec2.rightNormal.dx;
// var prB_vy:Number = dpB * vec2.leftNormal.dy;
const prBvy = dpB * vec2.rightNormal.dy;
// 3. Find the dot product of vec1 and vec2's normal
// (left or right normal depending on line's direction, let's say left)
// vec.leftNormal --> vx = vec.vy; vy = -vec.vx;
// vec.rightNormal --> vx = -vec.vy; vy = vec.vx;
// var dpB:Number = vec1.vx * vec2.leftNormal.dx + vec1.vy * vec2.leftNormal.dy;
const dpB = vec1.x * vec2.rightNormal.dx + vec1.y * vec2.rightNormal.dy;
// 5. Add the first projection prA to the reverse of the second -prB
// var new_vx:Number = prA_vx - prB_vx;
// var new_vy:Number = prA_vy - prB_vy;
newX = prAvx - prBvx;
newY = prAvy - prBvy;
// 4. Project vec1 over vec2's left normal
// var prB_vx:Number = dpB * vec2.leftNormal.dx;
const prBvx = dpB * vec2.rightNormal.dx;
// var prB_vy:Number = dpB * vec2.leftNormal.dy;
const prBvy = dpB * vec2.rightNormal.dy;
drawLine(p.x, p.y, p.x + vec2.rightNormal.x , p.y + vec2.rightNormal.y, "black"); // edge normal vector
drawLine(p.x, p.y, p.x + newX, p.y + newY, "blue"); // bounced velocity vector
// 5. Add the first projection prA to the reverse of the second -prB
// var new_vx:Number = prA_vx - prB_vx;
// var new_vy:Number = prA_vy - prB_vy;
return {
x: prAvx - prBvx,
y: prAvy - prBvy,
vec1,
vec2
};
}
// contact.velocity = { x: prAvx - prBvx, y: prAvy - prBvy };
contact.velocity = bounceVector(v, run, rise);
// edge normal vector
drawLine(p.x, p.y, p.x + contact.velocity.vec2.rightNormal.x , p.y + contact.velocity.vec2.rightNormal.y, "black");
// bounced velocity vector
drawLine(p.x, p.y, p.x + contact.velocity.x, p.y + contact.velocity.y, "blue");
if (det < 0) {
Velocity[entity_id] = { x: newX, y: newY };
Velocity[entity_id] = contact.velocity;
Position[entity_id] = contact.position;
} else {
Velocity[entity_id] = { x: v.x, y: v.y };

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 52 KiB