]> git.localhorst.tv Git - sdl-test8.git/commitdiff
extracted some parts of the collision response for better readability
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 9 May 2012 18:43:46 +0000 (18:43 +0000)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 9 May 2012 18:43:46 +0000 (18:43 +0000)
src/game/Entity.cpp
src/pong/Match.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());
index 5754b36c51c5b51564202d31619eddc3b920ff79..523c2fd6af6442220486a6bfb89827bc80e846f3 100644 (file)
@@ -34,8 +34,8 @@ Match::Match(void)
 , worldWidth(800)
 , worldHeight(480)
 , ball(10)
-, secondBall(7)
-, thirdBall(5)
+, secondBall(8)
+, thirdBall(7)
 , leftPaddle(10, 100)
 , rightPaddle(10, 100)
 , topWall(800, 10)
@@ -51,15 +51,15 @@ Match::Match(void)
        }
 
        ball.Translate(Entity::Vector(400, 240));
-       ball.Accelerate(Entity::Vector(180, 180));
+       ball.Accelerate(Entity::Vector(180, 180), 0);
 //     ball.SetMaxVelocity(500.0f);
 
        secondBall.Translate(Entity::Vector(300, 240));
-       secondBall.Accelerate(Entity::Vector(-50, -50), 1);
+       secondBall.Accelerate(Entity::Vector(-50, -50), -3);
 //     secondBall.SetMaxVelocity(600.0f);
 
        thirdBall.Translate(Entity::Vector(200, 440));
-       thirdBall.Accelerate(Entity::Vector(60, 40));
+       thirdBall.Accelerate(Entity::Vector(60, 40), 3);
 
        leftPaddle.Translate(Entity::Vector(5, 200));
        rightPaddle.Translate(Entity::Vector(795, 280));