## NOTE: You may want to read the calculations here (on the Physics Stack Exchange website) because I can insert LaTeX equations inline there.

I'm doing a physics simulator right now, but I'm having some trouble bouncing a polygon off the ground.

**What I'm trying to do:**

When the polygon touches the ground, I want to apply a linear pulse at the point of contact, so that the energy E & # 39; of the polygon after the rebound is a certain elasticity constant (!) Enter image description here (2)) (2) times the energy E of the polygon before the rebound.

$$ E & # 39; = epsilon E $$

**My calculations:**

Imagine a Cartesian coordinate system in which the ground is y = 0. Then (! (Insert image description here) (3)) (3) where we expect p to be positive.

m, I, $ overrightarrow {v} $, $ overrightarrow { omega} $ and $ overrightarrow {r} $ are given, and we want to calculate the size p of the impulse $ overrightarrow {P} $.

$$ frac {1} {2} m cdot overrightarrow {v & # 39;} ^ 2 + frac {1} {2} cdot overrightarrow { omega & # 39;} ^ 2 = E & # 39; = epsilon E = epsilon cdot left ( frac {1} {2} m cdot overrightarrow {v} ^ 2 + frac {1} {2} I cdot overrightarrow { omega } ^ 2 right) $$

$$ Leftrightarrow m cdot left ( overrightarrow {v} + frac { overrightarrow {P}} {m} right) ^ 2 + I cdot left ( overrightarrow { omega} + frac { overrightarrow {r} times overrightarrow {P}} {I} right) ^ 2 = epsilon cdot left (m cdot overrightarrow {v} ^ 2 + I cdot overrightarrow { omega} ^ 2 right) $$

$$ Leftrightarrow m cdot overrightarrow {v} ^ 2 + 2 overrightarrow {v} cdot overrightarrow {P} + frac { overrightarrow {P} ^ 2} {m} + I cdot overrightarrow { omega} ^ 2 + 2 overrightarrow { omega} cdot ( overrightarrow {r} times overrightarrow {P}) + frac {( overrightarrow {r} times overrightarrow {P}) ^ 2} {I} = epsilon cdot left (m cdot overrightarrow {v} ^ 2 + I cdot overrightarrow { omega} ^ 2 right) $$

And now we can use the fact that $ overrightarrow {P} (0, p) $, to calculate the point and cross products:

$$ 0 = (1 epsilon) cdot left (m cdot overrightarrow {v} ^ 2 + I cdot overrightarrow { omega} ^ 2 right) + 2p cdot v (1) + frac {p ^ 2} {m} + 2p cdot r (0) cdot | overrightarrow { omega} | + frac {(p cdot r (0)) ^ 2} {I} $$

$$ Leftrightarrow left ( frac {1} {m} + frac {r (0) ^ 2} {I} right) cdot p ^ 2 + left (2v (1) + 2r (0) cdot | overrightarrow { omega} | right) cdot p + (1- epsilon) cdot left (m cdot overrightarrow {v} ^ 2 + I cdot overrightarrow { omega} ^ 2 right) $$

This is a quadratic equation in $ p $.

If we hire:

$$ A = frac {1} {m} + frac {r (0) ^ 2} {I} $$
$$ B = 2v (1) + 2r (0) cdot | overrightarrow { omega} | $$
$$ C = (1 epsilon) cdot left (m cdot overrightarrow {v} ^ 2 + I cdot overrightarrow { omega} ^ 2 right) $$
We can calculate the determinant $$ D = B ^ 2-4AC $$,

Now it is clear that $$ p = frac {-B + sqrt {D}} {2A} $$

When I do this in a simulation, it looks very natural.

However, there are always problems when D <0.

I've tried to solve this problem in countless ways ^{(*)}but it never worked out well.

Can someone check if my method and calculations are correct?

And if not, how can I model this collision instead?

Here's a snippet of my code (in Python):

```
class POLYGON:
def bounce(self):
#Checks whether the polygon touches the ground and makes it rebound if necessary
low_nodes = () #makes a list of the nodes with y<0
for node in self.nodes:
if node(1) <= 0:
low_nodes.append(node)
if len(low_nodes) >= 1:
speed = self.speed
self.translate((0, -2*lowest_node(1)) #lifts the lowest node above the ground
for node in low_nodes:
r = node - self.pos #calculates r
A = 1/self.mass + r(0)**2/self.rot_inertia
B = 2*self.speed(1) + 2*self.rot_speed*r(0)
C = (1-restituence_constant) * (self.mass*np.linalg.norm(self.speed)**2 + self.rot_inertia*self.rot_speed**2)
D = B**2 - 4*A*C
if D<0:
impulse = (0, -B/(2*A)
else:
impulse = (0, (-B-np.sqrt(D)) /(2*A) /len(low_nodes))
self.applyLinearImpulse(impulse, node)
```

^{(*)}

For example:

using the absolute value of D

Consider $ p $ as a complex number and apply the impulse $ (Im (p), Re (p)) $

Set $ p = frac {-B} {2A} $ if $ D <0 $

...