Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.43 KB | None | 0 0
  1.                     //BALL COLLISION CODE
  2.                     for (int i = 0; i < balls.Count; i++)
  3.                     {
  4.                         for (int j = i + 1; j < balls.Count; j++)
  5.                         {
  6.                             // TODO: Put your collision handling with another ball code here
  7.                             if (balls[i].BoundingSphere.Intersects(balls[j].BoundingSphere))
  8.                             {
  9.                                 //moving objects only if both velocity.length > 0
  10.                                 if (balls[i].Velocity.Length() > 0 && balls[j].Velocity.Length() > 0)
  11.                                 {
  12.                                     Vector3 collisionVector = balls[i].Position - balls[j].Position;
  13.                                     Vector3 collisionVectorj = balls[j].Position - balls[i].Position;
  14.  
  15.                                     Vector3 velocityOutputI = balls[i].Velocity - ((Vector3.Dot((balls[i].Velocity - balls[j].Velocity), collisionVector)
  16.                                         / collisionVector.LengthSquared()) * collisionVector);
  17.  
  18.                                     Vector3 velocityOutputJ = balls[j].Velocity - ((Vector3.Dot((balls[j].Velocity - balls[i].Velocity), collisionVectorj)
  19.                                         / collisionVectorj.LengthSquared()) * collisionVectorj);
  20.  
  21.                                     balls[i].Velocity = velocityOutputI;
  22.                                     balls[j].Velocity = velocityOutputJ;
  23.  
  24.                                     //MOVE OUT OF WAY CODE
  25.                                     balls[i].Position -= ((balls[j].Position - balls[i].Position).Length() - (balls[i].BoundingSphere.Radius) * 2) * Vector3.Normalize(collisionVector);
  26.                                     balls[j].Position += ((balls[j].Position - balls[i].Position).Length() - (balls[i].BoundingSphere.Radius) * 2) * Vector3.Normalize(collisionVector);
  27.                                 }
  28.  
  29.                                 //if one ball is 0
  30.                                 else
  31.                                 {
  32.  
  33.                                     //if ball j is moving
  34.                                     float jSpeed = balls[j].Velocity.Length();
  35.                                     float iSpeed = balls[i].Velocity.Length();
  36.  
  37.                                     if (jSpeed > 0)
  38.                                     {
  39.                                         //create normal to collision vector
  40.                                         Vector3 collisionVector = balls[i].Position - balls[j].Position;
  41.                                         float collLength = collisionVector.Length();
  42.  
  43.                                         //create normal to the collision vector
  44.                                         Vector3 collisionNormal = new Vector3(-collisionVector.Z, collisionVector.Y, collisionVector.X);
  45.  
  46.                                         //theta calculation
  47.                                         float dot = Vector3.Dot(-balls[j].Velocity, collisionVector);
  48.                                         float denom = jSpeed * collLength;
  49.                                         float theta = (float)Math.Acos(dot /
  50.                                             denom);
  51.  
  52.                                         //vector normalization
  53.                                         collisionVector = Vector3.Normalize(collisionVector);
  54.                                         collisionNormal = Vector3.Normalize(collisionNormal);
  55.  
  56.                                         //MAKE SPECIAL CASE FOR THETA = 0;
  57.                                         if (theta == 0)
  58.                                         {
  59.                                             Vector3 tempVelocity = balls[i].Velocity;
  60.                                             balls[i].Velocity = balls[j].Velocity;
  61.                                             balls[j].Velocity = tempVelocity;
  62.                                         }
  63.                                         else
  64.                                         {
  65.                                             float velocityLengthJ = balls[j].Velocity.Length() * ((float)Math.Sqrt(2 + (2 * (float)Math.Cos(theta))) / 2);
  66.                                             float velocityLengthI = balls[j].Velocity.Length() * (float)(Math.Sin(theta / 2));
  67.  
  68.                                             //create the new ball velocities
  69.                                             balls[j].Velocity = velocityLengthJ * Vector3.Normalize(collisionNormal);
  70.                                             balls[i].Velocity = velocityLengthI * Vector3.Normalize(collisionVector);
  71.                                         }
  72.                                     }
  73.  
  74.                                     if (iSpeed > 0)
  75.                                     {
  76.                                         //create normal to collision vector
  77.                                         Vector3 collisionVector = balls[j].Position - balls[i].Position;
  78.                                         float collLength = collisionVector.Length();
  79.  
  80.                                         //create normal to the collision vector
  81.                                         Vector3 collisionNormal = new Vector3(-collisionVector.Z, collisionVector.Y, collisionVector.X);
  82.  
  83.                                         //theta calculation
  84.                                         float dot = Vector3.Dot(-balls[i].Velocity, collisionVector);
  85.                                         float denom = iSpeed * collLength;
  86.                                         float theta = (float)Math.Acos(dot /
  87.                                             denom);
  88.  
  89.                                         //MAKE SPECIAL CASE FOR THETA = 0;
  90.                                         if (theta == 0)
  91.                                         {
  92.                                             Vector3 tempVelocity = balls[i].Velocity;
  93.                                             balls[i].Velocity = balls[j].Velocity;
  94.                                             balls[j].Velocity = tempVelocity;
  95.                                         }
  96.                                         else
  97.                                         {
  98.                                             //vector normalization
  99.                                             collisionVector = Vector3.Normalize(collisionVector);
  100.                                             collisionNormal = Vector3.Normalize(collisionNormal);
  101.  
  102.                                             float velocityLengthI = balls[i].Velocity.Length() * (float)(Math.Sqrt(2 + (2 * (float)Math.Cos(theta))) / 2);
  103.                                             float velocityLengthJ = balls[i].Velocity.Length() * (float)(Math.Sin(theta / 2));
  104.  
  105.                                             Vector3 normalizedCollisionN = Vector3.Normalize(collisionNormal);
  106.                                             Vector3 normalizedCollision = Vector3.Normalize(collisionVector);
  107.  
  108.                                             //create the new ball velocities
  109.                                             balls[i].Velocity = velocityLengthI * normalizedCollisionN;
  110.                                             balls[j].Velocity = velocityLengthJ * normalizedCollision;
  111.                                         }
  112.                                     }
  113.                                 }
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement