private void UpdateBallDirAndSpeed(float ticksToTravel) { if (mNumTicksLeft <= 0) // beginning of a new road { mNumTicksLeft = ticksToTravel; } // use num ticks to determine how far we have travelled float percentTravelled = (ticksToTravel - mNumTicksLeft) / ticksToTravel; // ball's center position should be percentTravelled along the road's entire length float roadLength = mRoad.RoadSegmentLength(mCurrentRoadSegment); Vector2 v = mRoad.RoadSegmentDir(mCurrentRoadSegment); Vector2 startPos = mRoad.RoadSegmentStartPos(mCurrentRoadSegment); Center = startPos + percentTravelled * roadLength * v; VelocityDirection = v; // speed will be a function of what is remaining length and remaining ticks left. float totalLength = mRoad.RoadSegmentLength(mCurrentRoadSegment); float travelled = (Center - startPos).Length(); float remainLength = totalLength - travelled; Speed = remainLength / mNumTicksLeft; }
public void Update(float turnRate) { // continuously adjust the car's forward direction and speed mTurnRate = turnRate; /// Two distinct states to be concerned with /// /// 1. There are next road segment to move towards /// 2. Within the bounds of current road segment /// if (mCurrentRoadSegment < mRoad.NumDefinedSegments()) { Vector2 travelled = Center - mRoad.RoadSegmentStartPos(mCurrentRoadSegment); if (travelled.Length() >= mRoad.RoadSegmentLength(mCurrentRoadSegment)) { mCurrentRoadSegment++; if (mCurrentRoadSegment >= mRoad.NumDefinedSegments()) { ResetCarPosition(); } } } else { ResetCarPosition(); } UpdateCarDirAndSpeed(); }
public bool Update(float ticksToTravel) { // continuously adjust the ball's forward direction and speed bool newState = false; /// distinct states to be concerned with /// /// 1. Has a valide next state (road segment) /// 1a. continues to be valid (within the bounds of road segment) /// 1b. if not, trasition to next state /// 2. No more valid next state: reset /// if (mCurrentRoadSegment < mRoad.NumDefinedSegments()) { Vector2 travelled = Center - mRoad.RoadSegmentStartPos(mCurrentRoadSegment); if (travelled.Length() >= mRoad.RoadSegmentLength(mCurrentRoadSegment)) { mCurrentRoadSegment++; newState = true; if (mCurrentRoadSegment >= mRoad.NumDefinedSegments()) { ResetBallPosition(); } } } else { ResetBallPosition(); newState = true; } // **** set dir and velocity only during state trasition *** // CONSEQUENCE?: Last state/setment (under user control) may be // wrong if user changes the state (Road Size/Dir) // while we are in the segment if (newState || (mCurrentRoadSegment == (mRoad.NumDefinedSegments() - 1)) ) { UpdateBallDirAndSpeed(ticksToTravel); } // SOLUTION? Call UpdateBallDirAndSpeed() during _EVERY_ Update // for LAST SEGMENT only. return(newState); }