## Recommended Posts

Hello,

I've been trying to implement some simple physics simulation in pixi, involving a ball (circle) that bounces around and spins. I've managed to aproximate a pretty good model myself but the movement always becomes awkward when the ball begin to slow down so I tried to use the Garwin model I found here. However, something must be wrong with either the way I applied the functions or the MUs i'm using because as the ball settles to the floor it begins to accelerate exponentially.

This is my bounce function:

``````  let alfa = 0.4,
cor = {
x: 0.8,
y: 0.8
}

function enhancedBounce() {
let vx = (((1 - (alfa * cor.x)) * cat.vx) + ((alfa * (1 + cor.x)) * cat.vr * cat.radius)) / (1 + alfa);
let vr = ((1 + cor.x) * cat.vx + alfa * (1 + cor.x) * cat.radius * (cat.vr)) / (cat.radius * (1 + alfa));

cat.vr = vr;
cat.vx = vx;

cat.vy = -cor.y * cat.vy ;
}``````

Where cat.vx and cat.vy are velocity components and cat.vr is the angular velocity of the ball. Now I'm pretty bad at math but it seems that whatever I input into the functions as vx and vr, the output of both functions is greater than those values (vr' > vr and vx' > vr) which seems counterintuitive to me, however, since I'm not good enough at math I can't demonstrate that this is in fact the case.

cat.vx and cat.vr are just added to cat.x and cat.y on every render

``````function render() {
if(cat.x >= sceneHeight - 32) {
enhancedBounce();
}

cat.x += cat.vx;
cat.y += cat.vy;
cat.rotate += cat.vr;
}``````

The function performs well when the ball is simply bouncing off the ground with the rotation and x-velocity looking nice but the moment the ball rests on the ground and enhancedBounce() is called every frame, it accelerates as stated before.

Any idea what i'm doing wrong?

Edited by Wardzr

##### Share on other sites

Its more 2d coding problem. Should I move that thread in https://www.html5gamedevs.com/forum/3-2d/ ?

It doesnt matter if you do it in canvas2d drawImage / drawEllipse  or pixijs sprite/grapics position move Also I see that you dont actually have explanations of how "vx" and "vy" are used. Programming is different than math a bit - you have a function FRAME() that is called every 1/60 second, with a "delta" that is 1 in good case and more in case of lags. Because of discrete model, some functions can become unstable and dont work the same way they do in math.

##### Share on other sites

Yes please, move it. In the mean time I'll add a description for those velocities. And yeah, I realize I used pixi just to draw the scene.

Edited by Wardzr

##### Share on other sites

Aparently there was a typo in the article I linked. In the angular speed function, the tangential speed (Rw1) should be multiplied by "a - ex" instead of "a(1 + ex)".

Just in case someone else has the same issue.

The new code becomes

``````  let alfa = 0.4,
cor = {
x: 0.8,
y: 0.8
}

function enhancedBounce() {
let vx = (((1 - (alfa * cor.x)) * cat.vx) + ((alfa * (1 + cor.x)) * cat.vr * cat.radius)) / (1 + alfa);
let vr = ((1 + cor.x) * cat.vx + (alfa - cor.x) * cat.radius * (cat.vr)) / (cat.radius * (1 + alfa));

cat.vr = vr;
cat.vx = vx;

cat.vy = -cor.y * cat.vy ;
}``````

Edited by Wardzr

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.