]> git.localhorst.tv Git - sdl-test8.git/blobdiff - src/game/Entity.cpp
extracted some parts of the collision response for better readability
[sdl-test8.git] / src / game / Entity.cpp
index 1c3f78527296e859b81e2c597b57285ab1de2245..ebc812e65af4bd06bbe5691f1df558dfd58a4c54 100644 (file)
@@ -71,6 +71,8 @@ void Entity::CollisionResponse(Entity &a, const Ray &na, Entity &b) {
 
        const Scalar e(1);
 
+       const Vector normal(na.Direction());
+
        const Vector va(a.LinearVelocity());
        const Vector vb(b.LinearVelocity());
        const Scalar wa(a.AngularVelocity());
@@ -80,25 +82,33 @@ void Entity::CollisionResponse(Entity &a, const Ray &na, Entity &b) {
        const Vector vbp(b.VelocityAt(na.Origin()));
        const Vector vab = vap - vbp;
 
+       const Vector restitutionVAB((-(1 + e)) * vab);
+
        const Vector rap90((na.Origin() - a.Origin()).Rotate90());
        const Vector rbp90((na.Origin() - b.Origin()).Rotate90());
 
-       const Scalar ia(a.Mass());
+       const Scalar ia(a.Mass()); // inertia equals mass? probably not
        const Scalar ib(b.Mass());
 
+       const Scalar aInverseMass(1.0 / a.Mass());
+       const Scalar bInverseMass(1.0 / b.Mass());
+
        const Scalar rap90dotN(rap90.Dot(na.Direction()));
        const Scalar rbp90dotN(rbp90.Dot(na.Direction()));
 
+       const Scalar aRotationalPart((rap90dotN * rap90dotN) / ia);
+       const Scalar bRotationalPart((rbp90dotN * rbp90dotN) / ib);
+
        const Scalar j(
-                       (((-(1 + e)) * vab).Dot(na.Direction()))
-                       / (na.Direction().Dot(na.Direction() * ((1.0 / a.Mass()) + (1.0 / b.Mass())))
-                                       + ((rap90dotN * rap90dotN) / ia)
-                                       + ((rbp90dotN * rbp90dotN) / ib) ));
+                       restitutionVAB.Dot(normal)
+                       / (normal.Dot(normal * (aInverseMass + bInverseMass))
+                                       + aRotationalPart
+                                       + bRotationalPart ));
 
-       const Vector va2(va + ((j / a.Mass()) * na.Direction()));
-       const Vector vb2(vb - ((j / b.Mass()) * na.Direction()));
-       const Scalar wa2(wa + ((rap90.Dot(j * na.Direction())) / ia));
-       const Scalar wb2(wb - ((rbp90.Dot(j * na.Direction())) / ib));
+       const Vector va2(va + (j / a.Mass()) * normal);
+       const Vector vb2(vb - (j / b.Mass()) * normal);
+       const Scalar wa2(wa + rap90.Dot(j * normal) / ia);
+       const Scalar wb2(wb - rbp90.Dot(j * normal) / ib);
 
        a.linearVelocity = va2;
        a.angularVelocity = wa2;
@@ -109,6 +119,7 @@ void Entity::CollisionResponse(Entity &a, const Ray &na, Entity &b) {
 void Entity::InfInfCollisionResponse(Entity &a, const Ray &na, Entity &b) {
        // If both elements are standing still or both are moving, move them away
        // from each other. Otherwise, move the moving one.
+       // This will keep the paddle from bouncing off the wall at infinite velocity.
        if ((a.linearVelocity == Vector() && b.linearVelocity == Vector())
                        || (a.linearVelocity != Vector() && b.linearVelocity != Vector())) {
                a.Translate(na.Direction());