ignore linear impulse if bodies are separating
This commit is contained in:
22
rigidbody.py
22
rigidbody.py
@@ -5,7 +5,7 @@ import pygame as pg
|
||||
|
||||
from tools import debug
|
||||
from collider.system import intersect
|
||||
from collider.types import *
|
||||
from collider.types import BaseCollider, ColliderContact
|
||||
from transform import Transform
|
||||
|
||||
@dataclass
|
||||
@@ -55,21 +55,22 @@ class PhysicsSystem:
|
||||
|
||||
for a, b in combinations(self.bodies, 2):
|
||||
if collision := intersect(a.collider, b.collider, a.transform, b.transform):
|
||||
self.resolve_collision(a, b, collision)
|
||||
self.resolve_collision(a, b, collision, dt)
|
||||
|
||||
def resolve_collision(self, a: RigidBody, b: RigidBody, collision: ColliderContact) -> None:
|
||||
def resolve_collision(self, a: RigidBody, b: RigidBody, collision: ColliderContact, dt: float) -> None:
|
||||
v_rel_linear = pg.Vector2(a.velocity - b.velocity)
|
||||
if a.mass == 0.0 and b.mass == 0.0: return
|
||||
SLACK=0.2
|
||||
correction = collision.penetration / (a.inv_mass + b.inv_mass) * SLACK * collision.normal
|
||||
correction = collision.penetration * SLACK * collision.normal
|
||||
|
||||
if a.mass != 0.0:
|
||||
a.transform.position += correction
|
||||
if b.mass != 0.0:
|
||||
b.transform.position -= correction
|
||||
|
||||
debug.draw_contact(collision)
|
||||
restitution = a.restitution * b.restitution
|
||||
friction = a.coef_friction * b.coef_friction
|
||||
tangent = collision.normal.rotate(90)
|
||||
v_rel_linear = pg.Vector2(a.velocity - b.velocity)
|
||||
a_w = a.angular_velocity
|
||||
b_w = b.angular_velocity
|
||||
|
||||
@@ -81,7 +82,7 @@ class PhysicsSystem:
|
||||
w_cross_r_b = pg.Vector2(-r_b.y * b_w, r_b.x * b_w)
|
||||
v_rel = v_rel_linear + w_cross_r_a - w_cross_r_b
|
||||
v_rel_tangent = v_rel.dot(tangent)
|
||||
|
||||
|
||||
collision_impulse = -(1+restitution)*(v_rel.dot(collision.normal))\
|
||||
/ (a.inv_mass + b.inv_mass + (r_a.cross(collision.normal)**2 * a.inv_moment_of_inertia)\
|
||||
+ (r_b.cross(collision.normal)**2 * b.inv_moment_of_inertia)) / len(collision.points)
|
||||
@@ -89,7 +90,10 @@ class PhysicsSystem:
|
||||
(r_a.cross(tangent))**2 * a.inv_moment_of_inertia +\
|
||||
(r_b.cross(tangent)**2 * b.inv_moment_of_inertia))) / len(collision.points)
|
||||
friction_impulse = pg.math.clamp(friction_impulse, -abs(collision_impulse)*friction, abs(collision_impulse)*friction)
|
||||
a.apply_impulse(collision.normal*collision_impulse + friction_impulse * tangent, point)
|
||||
b.apply_impulse(-1 * collision.normal*collision_impulse - friction_impulse * tangent, point)
|
||||
a.apply_impulse( friction_impulse * tangent, point)
|
||||
b.apply_impulse( -1 * friction_impulse * tangent, point)
|
||||
if v_rel_linear.dot(collision.normal) < 0.0:
|
||||
a.apply_impulse(collision_impulse * collision.normal, point)
|
||||
b.apply_impulse(-1 * collision_impulse * collision.normal, point)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user