Ejemplo n.º 1
0
 public void Calculate()
 {
     try
     {//parse strings into floating point numbers
         float pos1_x = Single.Parse(pos1_inputs[0].text);
         float pos1_y = Single.Parse(pos1_inputs[1].text);
         float pos1_z = Single.Parse(pos1_inputs[2].text);
         float pos2_x = Single.Parse(pos2_inputs[0].text);
         float pos2_y = Single.Parse(pos2_inputs[1].text);
         float pos2_z = Single.Parse(pos2_inputs[2].text);
         float num    = Single.Parse(radius_input.text);
         if (num < 0)
         {
             throw new Exception("Only positive radii allowed!");
         }
         pos1   = new MyVector3(pos1_x, pos1_y, pos1_z);
         pos2   = new MyVector3(pos2_x, pos2_y, pos2_z);
         radius = num;
     }
     catch (Exception e)
     {//parsing failed
         Debug.LogError(e.Message);
         pos1_inputs[0].text = "";
         pos1_inputs[1].text = "";
         pos1_inputs[2].text = "";
         pos2_inputs[0].text = "";
         pos2_inputs[1].text = "";
         pos2_inputs[2].text = "";
         radius_input.text   = "";
         return;
     }
     //printe vectors to the scene
     directionalUnitVect.text = MyVector3.DirectionalUnitVector(pos1, pos2).Print();
     closerThanRadius.text    = MyVector3.CloserThanRadius(pos1, pos2, radius).ToString();
 }
Ejemplo n.º 2
0
 private void FixedUpdate()
 {
     //check collision between any pair of balls
     for (int i = 0; i < balls.Length - 1; i++)
     {
         for (int j = i + 1; j < balls.Length; j++)
         {
             if (MyVector3.CloserThanRadius(balls[i].GetPosition(), balls[j].GetPosition(), 2 * SnookerBall.radius))
             {//the distance between the centers of the two balls is less than twice the radius
              //this means there is a collision
                 HandleCollision(balls[i], balls[j]);
             }
         }
     }
 }
Ejemplo n.º 3
0
 public void FixedUpdate()
 {
     if (rotating)
     {//the ball is rotating around a waypoint
         //angle ball->current waypoint->next waypoint at the end of the previous update (clockwise winding)
         float prevAngle = MyVector3_Cylindrical.DeltaAngleRadiants(rotationPosition, directionConcesutiveWaypoints);
         int   sign      = clockwise ? -1 : 1;
         float dAngle    = angularVelocity * Time.fixedDeltaTime * sign;
         rotationPosition.RotateByAngle(dAngle);
         //angle ball->current waypoint->next waypoint at the current update
         float angle = MyVector3_Cylindrical.DeltaAngleRadiants(rotationPosition, directionConcesutiveWaypoints);
         if ((!clockwise && prevAngle > Mathf.PI && angle < Mathf.PI) ||
             (clockwise && prevAngle < Mathf.PI && angle > Mathf.PI) ||
             dAngle > Mathf.PI)
         {//the ball has moved past the next movement direction on this frame
             SetLinearMovementVariables();
         }
         else
         {//the ball has NOT crossed the next movement direction on this frame
             position = MyVector3.Add(rotationPosition.ToCartesian(), waypoints_position[target]);
         }
     }
     else
     {
         if (MyVector3.CloserThanRadius(position, waypoints_position[target], rotationRadius))
         {//the ball is closer to the waypoint than the specified radius
             SetRotationVariables();
         }
         else
         {
             MyVector3 deltaPos             = movementDirection.Scale(Time.fixedDeltaTime * velocity);
             float     distanceFromWaypoint = MyVector3.Subtract(waypoints_position[target], position).Magnitude();
             if (distanceFromWaypoint - deltaPos.Magnitude() <= rotationRadius)
             {//the ball has moved closer than the specified radius on this frame
                 position = MyVector3.Subtract(waypoints_position[target], movementDirection.Scale(rotationRadius));
                 SetRotationVariables();
             }
             else
             {//the ball has NOT moved closer than the specified radius on this frame
                 position = MyVector3.Add(position, deltaPos);
             }
         }
     }
     //place object in the scene
     MoveGameObject();
 }