Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BALL COLLISION CODE
- for (int i = 0; i < balls.Count; i++)
- {
- for (int j = i + 1; j < balls.Count; j++)
- {
- // TODO: Put your collision handling with another ball code here
- if (balls[i].BoundingSphere.Intersects(balls[j].BoundingSphere))
- {
- //moving objects only if both velocity.length > 0
- if (balls[i].Velocity.Length() > 0 && balls[j].Velocity.Length() > 0)
- {
- Vector3 collisionVector = balls[i].Position - balls[j].Position;
- Vector3 collisionVectorj = balls[j].Position - balls[i].Position;
- Vector3 velocityOutputI = balls[i].Velocity - ((Vector3.Dot((balls[i].Velocity - balls[j].Velocity), collisionVector)
- / collisionVector.LengthSquared()) * collisionVector);
- Vector3 velocityOutputJ = balls[j].Velocity - ((Vector3.Dot((balls[j].Velocity - balls[i].Velocity), collisionVectorj)
- / collisionVectorj.LengthSquared()) * collisionVectorj);
- balls[i].Velocity = velocityOutputI;
- balls[j].Velocity = velocityOutputJ;
- //MOVE OUT OF WAY CODE
- balls[i].Position -= ((balls[j].Position - balls[i].Position).Length() - (balls[i].BoundingSphere.Radius) * 2) * Vector3.Normalize(collisionVector);
- balls[j].Position += ((balls[j].Position - balls[i].Position).Length() - (balls[i].BoundingSphere.Radius) * 2) * Vector3.Normalize(collisionVector);
- }
- //if one ball is 0
- else
- {
- //if ball j is moving
- float jSpeed = balls[j].Velocity.Length();
- float iSpeed = balls[i].Velocity.Length();
- if (jSpeed > 0)
- {
- //create normal to collision vector
- Vector3 collisionVector = balls[i].Position - balls[j].Position;
- float collLength = collisionVector.Length();
- //create normal to the collision vector
- Vector3 collisionNormal = new Vector3(-collisionVector.Z, collisionVector.Y, collisionVector.X);
- //theta calculation
- float dot = Vector3.Dot(-balls[j].Velocity, collisionVector);
- float denom = jSpeed * collLength;
- float theta = (float)Math.Acos(dot /
- denom);
- //vector normalization
- collisionVector = Vector3.Normalize(collisionVector);
- collisionNormal = Vector3.Normalize(collisionNormal);
- //MAKE SPECIAL CASE FOR THETA = 0;
- if (theta == 0)
- {
- Vector3 tempVelocity = balls[i].Velocity;
- balls[i].Velocity = balls[j].Velocity;
- balls[j].Velocity = tempVelocity;
- }
- else
- {
- float velocityLengthJ = balls[j].Velocity.Length() * ((float)Math.Sqrt(2 + (2 * (float)Math.Cos(theta))) / 2);
- float velocityLengthI = balls[j].Velocity.Length() * (float)(Math.Sin(theta / 2));
- //create the new ball velocities
- balls[j].Velocity = velocityLengthJ * Vector3.Normalize(collisionNormal);
- balls[i].Velocity = velocityLengthI * Vector3.Normalize(collisionVector);
- }
- }
- if (iSpeed > 0)
- {
- //create normal to collision vector
- Vector3 collisionVector = balls[j].Position - balls[i].Position;
- float collLength = collisionVector.Length();
- //create normal to the collision vector
- Vector3 collisionNormal = new Vector3(-collisionVector.Z, collisionVector.Y, collisionVector.X);
- //theta calculation
- float dot = Vector3.Dot(-balls[i].Velocity, collisionVector);
- float denom = iSpeed * collLength;
- float theta = (float)Math.Acos(dot /
- denom);
- //MAKE SPECIAL CASE FOR THETA = 0;
- if (theta == 0)
- {
- Vector3 tempVelocity = balls[i].Velocity;
- balls[i].Velocity = balls[j].Velocity;
- balls[j].Velocity = tempVelocity;
- }
- else
- {
- //vector normalization
- collisionVector = Vector3.Normalize(collisionVector);
- collisionNormal = Vector3.Normalize(collisionNormal);
- float velocityLengthI = balls[i].Velocity.Length() * (float)(Math.Sqrt(2 + (2 * (float)Math.Cos(theta))) / 2);
- float velocityLengthJ = balls[i].Velocity.Length() * (float)(Math.Sin(theta / 2));
- Vector3 normalizedCollisionN = Vector3.Normalize(collisionNormal);
- Vector3 normalizedCollision = Vector3.Normalize(collisionVector);
- //create the new ball velocities
- balls[i].Velocity = velocityLengthI * normalizedCollisionN;
- balls[j].Velocity = velocityLengthJ * normalizedCollision;
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement