From 853728624a0b7036361e7b747a7342754e03e84e Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Fri, 23 Jan 2026 18:22:05 -0800 Subject: [PATCH] Refactor --- html/images/space.svg | 76 +++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index 749063b..43e8544 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -131,7 +131,7 @@ - + @@ -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 };