简体   繁体   中英

Bouncing Ball Border Glitch

I am making a bouncing ball simulation, where it uses 0.98 as the gravity (which works properly), but sometimes the ball gets stuck in a wall (top, bottom, left, or right side) and it will have 0 xspeed and just normal yspeed (or vice versa). I've been trying to adjust this by limiting the velocity, but that doesn't seem to work as well as I had hoped. I currently have it set up to reset after every time the ball is on the floor for 100 continuous frames. Here is a link to my code on p5.js: https://editor.p5js.org/Mavhawk64/sketches/5ks6M4wD0

it's because sometimes the particle get so far out of the border, that after you reverse it, on the next tick it's still out of the border, and then you reverse it again and this thing will go on endlessly. OPTION 1:

you need to be able to check and set some flag that you will only reverse the direction of the speed only once when you are out of the boundaries of the box. once you are again inside the box, you should reset this flag for the next time this happens.

OPTION 2:

you can differentiate between the borders and based on what border is hit, set the velocity accordingly. meaning that if you hit the top border, you set the ySpeed to (-1) * abs(ySpeed) or if you hit the bottom border you set the ySepeed to abs(ySpeed). same thing with the vertical borders. that way the speed wont get reversed each tick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM