WIP: bounce off walls

This commit is contained in:
2026-01-22 13:26:34 -08:00
parent b509ba12ea
commit 55f6328d3f

View File

@@ -126,7 +126,9 @@
<!-- <polygon class="wall" points="20,-50 10,-50 10,-60 20,-60" /> --> <!-- <polygon class="wall" points="20,-50 10,-50 10,-60 20,-60" /> -->
<!-- <polygon class="wall" points="-10,10 10,10 10,40 -10,40" /> --> <!-- <polygon class="wall" points="-10,10 10,10 10,40 -10,40" /> -->
<polygon class="wall" points="-10,10 10,30 -10,40 -20,20" />
<!-- <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,-50 -10,-50 -10,-60 -20,-60" /> --> <!-- <polygon class="wall" points="-20,-50 -10,-50 -10,-60 -20,-60" /> -->
<!-- <polygon class="wall" points="20,50 10,50 10,60 20,60" /> --> <!-- <polygon class="wall" points="20,50 10,50 10,60 20,60" /> -->
@@ -563,6 +565,9 @@ const Move = (() => {
const velocityVectorMagnitude = Math.sqrt(v.y**2+v.x**2); const velocityVectorMagnitude = Math.sqrt(v.y**2+v.x**2);
const dotProduct = edgeVector.y*v.y + edgeVector.x*v.x; const dotProduct = edgeVector.y*v.y + edgeVector.x*v.x;
const normalVectorDotProduct = normalVector.y*v.y + normalVector.x*v.x; const normalVectorDotProduct = normalVector.y*v.y + normalVector.x*v.x;
// const velocityVectorCrossNormalVector = v.x * normalVector.y - v.y * normalVector.x;
const velocityVectorCrossNormalVector = v.y * normalVector.x - v.x * normalVector.y;
console.log("dotProduct", dotProduct, "normalVectorDotProduct", normalVectorDotProduct); console.log("dotProduct", dotProduct, "normalVectorDotProduct", normalVectorDotProduct);
const edgeTheta = Math.acos(dotProduct / (edgeVectorMagnitude * velocityVectorMagnitude)); const edgeTheta = Math.acos(dotProduct / (edgeVectorMagnitude * velocityVectorMagnitude));
const normalTheta = Math.acos(normalVectorDotProduct / (edgeVectorMagnitude * velocityVectorMagnitude)); const normalTheta = Math.acos(normalVectorDotProduct / (edgeVectorMagnitude * velocityVectorMagnitude));
@@ -570,17 +575,28 @@ const Move = (() => {
console.log("VELOCITY", v); console.log("VELOCITY", v);
console.log("theta between edge vector and velocity vector", edgeTheta, "degrees", edgeTheta * 180 / Math.PI); console.log("theta between edge vector and velocity vector", edgeTheta, "degrees", edgeTheta * 180 / Math.PI);
console.log("theta between normal vector and velocity vector", normalTheta, "degrees", normalTheta * 180 / Math.PI); console.log("theta between normal vector and velocity vector", normalTheta, "degrees", normalTheta * 180 / Math.PI);
console.log("cross product normal and velocity", velocityVectorCrossNormalVector);
const newTheta = (Math.PI - normalTheta) * 2; const newTheta = (Math.PI - normalTheta) * 2;
const newY = -Math.cos(newTheta) * velocityVectorMagnitude; // const newY = -Math.cos(newTheta) * velocityVectorMagnitude;
const newX = Math.sin(newTheta) * velocityVectorMagnitude; // const newX = Math.sin(newTheta) * velocityVectorMagnitude;
let newY;
let newX;
if (velocityVectorCrossNormalVector < 0) {
newX = Math.cos(newTheta) * velocityVectorMagnitude;
newY = Math.sin(newTheta) * velocityVectorMagnitude;
} else {
newX = Math.sin(newTheta) * velocityVectorMagnitude;
newY = Math.cos(newTheta) * velocityVectorMagnitude;
}
Velocity[entity_id] = { Velocity[entity_id] = {
// x: v.x * Math.sin(thetaX) + v.y * Math.cos(thetaY), // x: v.x * Math.sin(thetaX) + v.y * Math.cos(thetaY),
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX) // y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
x: newX, x: newX,
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX) // y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
y: newY y: -newY
}; };
console.log("VELOCITY AFTER", Velocity[entity_id]); console.log("VELOCITY AFTER", Velocity[entity_id]);
@@ -683,7 +699,7 @@ function init() {
s.position = { x: 0, y: 0 }; s.position = { x: 0, y: 0 };
// s.velocity = { x: 0, y: -10 }; // s.velocity = { x: 0, y: -10 };
s.velocity = { x: 0, y: 10 }; s.velocity = { x: -10, y: 0 };
s.angularVelocity = 0; s.angularVelocity = 0;
// s. velocity = { x: -5*mult, y: 7*mult }; // s. velocity = { x: -5*mult, y: 7*mult };

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 53 KiB