Advertisement

physics engine stacking is not stable

Started by December 16, 2019 12:33 PM
2 comments, last by bzt 5 years, 1 month ago

I am writing a simple 2d physics engine with javascript, but something worng here.

I put 4 boxes vertically, but they always tilt slightly(slight rotation) to a side even though box 9 is on the flat ground. And after few seconds they fall on the ground because of the tilt. I have no idea how to fix it.

A student 

Hi, I'm interested in the problem , what kind of algorithm do you use to handle the resting contact of the blocks ?

Advertisement

The solution is clearly to figure out the reason for the tilt. Could be a simple rounding problem, which is easy to solve by adding a half-pixel coordinate (something like box_coordinate = Math.round(floatvar + 0.5) + "px"). Use your browser's javascript console and debugger. Put a lot of console.log calls in your code to locate where the incorrect value is calculated (or learn how to use breakpoints and step-through and follow the code that calculates the box's position and rotation). As I've said, looks like a simple rounding error to me, you just have to find out where it happens.

Cheers,
bzt

This topic is closed to new replies.

Advertisement