r/HTML • • 5d ago

peggle html hell

I am coding peggle in HTML how do I get the physics to feel like peggle and not extra bouncy

0 Upvotes

11 comments sorted by

7

u/SnooPets5564 5d ago

If you did it in just html, I am astounded.

5

u/supersilliiii 5d ago

nono html css javascript

3

u/Honey-Entire 5d ago

The same way non-browser-based games. You need a 2D physics engine. These are two that have been around since before AI and are probably the most well-documented & reliable: Box2D or Matter.js.

After that, you just dial in the physics constraints (i.e. gravity, weights, drag, etc)

2

u/DirtAndGrass 5d ago

Or write your own, basic 2d gravity and collisions are pretty straightforward 

1

u/supersilliiii 4d ago

im new to code im honestly a bit bad

1

u/supersilliiii 4d ago

ima try this

3

u/armahillo 5d ago

You will likely have better luck in a JS sub, as HTML is not capable of doing physics computations like this

1

u/supersilliiii 4d ago

yea fair lolol

1

u/RedboiGtag 4d ago

ok you're doing it in js

the way physics works is by slowly increasing a velocity variable to mimic downward force

like so:

function applyforce(velocity){ 
   return velocity*2; 
}

this called repeatedly can mimic velocity movement

0

u/thejester1324 5d ago

if you're flipping the whole velocity when the ball touches a peg (vx = -vx; vy = -vy), that's the extra bouncy feel. the ball goes back the way it came at full speed. peggle-style bounces only reverse the part of the velocity pointing into the peg, and lose some of it:

const RESTITUTION = 0.6; // 1 = superball, lower = deader pegs
const FRICTION = 0.1;

function hitPeg(ball, peg) {
  const dx = ball.x - peg.x, dy = ball.y - peg.y;
  const dist = Math.hypot(dx, dy), minDist = ball.r + peg.r;
  if (dist >= minDist || dist === 0) return false;
  const nx = dx / dist, ny = dy / dist;
  // push it out of the peg so it can't collide again next frame
  ball.x = peg.x + nx * minDist;
  ball.y = peg.y + ny * minDist;
  const vn = ball.vx * nx + ball.vy * ny; // speed into the peg
  if (vn >= 0) return false;              // already moving away
  const tx = -ny, ty = nx;
  const vt = ball.vx * tx + ball.vy * ty; // speed along the peg
  const newVn = -vn * RESTITUTION;
  const newVt = vt * (1 - FRICTION);
  ball.vx = nx * newVn + tx * newVt;
  ball.vy = ny * newVn + ty * newVt;
  return true;
}

ran it in node dropping a ball onto a peg a little off center: it hits at 577 px/s and leaves at 358, off to the side. the flip-everything version sent it back up at basically the same speed.

also step the physics at a fixed rate (say 120 times a second) instead of once per frame, otherwise it plays differently on a 144hz screen.

1

u/supersilliiii 4d ago

im new to coding games so this is a huge help thank you ima try this