private void SetRotationVariables() { directionConcesutiveWaypoints = MyVector3.Subtract(waypoints_position[NextTarget()], waypoints_position[target]).ToCylindrical(); rotationPosition = MyVector3.Subtract(position, waypoints_position[target]).ToCylindrical(); if (rotationPosition.getRadius() > 0) { angularVelocity = velocity / rotationPosition.getRadius(); rotating = true; float angle = MyVector3_Cylindrical.DeltaAngleRadiants(rotationPosition, directionConcesutiveWaypoints); //choose the rotation direction so that the bll always travels along the longest arc if (angle >= Mathf.PI) { clockwise = true; } else { clockwise = false; } } else {//the ball is exactly on top of the waypoint. //No rotation occurs in this case and the ball moves towards the next waypoint MakeRed(target); target = NextTarget(); MakeGreen(target); MakeYellow(NextTarget()); movementDirection = MyVector3.DirectionalUnitVector(position, waypoints_position[target]); } }
public void ConvertFromSherical() { try {//parse strings into floating point numbers float r = Single.Parse(spherical_inputs[0].text); if (r < 0) { throw new Exception("Only positive radii allowed!"); } float azim = Single.Parse(spherical_inputs[1].text) * (float)Math.PI / 180; float polar = Single.Parse(spherical_inputs[2].text) * (float)Math.PI / 180; vectSpherical = new MyVector3_Spherical(r, azim, polar); } catch (Exception e) {//parsing failed Debug.LogError(e.Message); spherical_inputs[0].text = ""; spherical_inputs[1].text = ""; spherical_inputs[2].text = ""; return; } //print vectors to the scene vectCartesian = vectSpherical.ToCartesian(); vectCylindrical = vectSpherical.ToCylindrical(); PrintVectors(); }
//difference in the azimuthal angles of the two given vectors static public float DeltaAngleRadiants(MyVector3_Cylindrical v1, MyVector3_Cylindrical v2) { float dAngle = v1.azimuthal - v2.azimuthal; if (dAngle < 0) { dAngle = dAngle + (float)Math.PI * 2; } return(dAngle); }
private void SetLinearMovementVariables() { rotationPosition = new MyVector3_Cylindrical( rotationPosition.getRadius(), directionConcesutiveWaypoints.getAzmuthalRad(), //place ball exctly between the current and the next waypoint rotationPosition.getY()); rotating = false; position = MyVector3.Add(rotationPosition.ToCartesian(), waypoints_position[target]); MakeRed(target); target = NextTarget(); MakeGreen(target); MakeYellow(NextTarget()); movementDirection = MyVector3.DirectionalUnitVector(position, waypoints_position[target]); }
// Start is called before the first frame update void Start() { position = ToMyVector3(gameObject.transform.position); waypoints_position = new MyVector3[waypoints.Length]; for (int i = 0; i < waypoints.Length; i++) { waypoints_position[i] = ToMyVector3(waypoints[i].position); } target = 0; MakeGreen(target); MakeYellow(NextTarget()); rotationPosition = MyVector3.Subtract(position, waypoints_position[target]).ToCylindrical(); movementDirection = MyVector3.DirectionalUnitVector(position, waypoints_position[target]); rotating = false; }
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(); }
public void ConvertFromCartesian() { try {//parse strings into floating point numbers float x = Single.Parse(cartesian_inputs[0].text); float y = Single.Parse(cartesian_inputs[1].text); float z = Single.Parse(cartesian_inputs[2].text); vectCartesian = new MyVector3(x, y, z); } catch (Exception e) {//parsing failed Debug.LogError(e.Message); cartesian_inputs[0].text = ""; cartesian_inputs[1].text = ""; cartesian_inputs[2].text = ""; return; } //print vectors to the scene vectCylindrical = vectCartesian.ToCylindrical(); vectSpherical = vectCartesian.ToSpherical(); PrintVectors(); }