//if running on a fixed timestep, this might be called //more often with a smaller delta time than RenderUpdate() internal void Update(UpdateTimer time, List <Input.InputAction> actions, PlayerSteering ps) { //Thread.Sleep(30); float secDelta = time.GetUpdateDeltaSeconds(); // mZone.UpdateModels(secDelta); float yawAmount = 0f; float pitchAmount = 0f; bool bGravity = false; float friction = GroundFriction; if (!mbOnGround) { //gravity if (!mbFly) { bGravity = true; } if (mbBadFooting) { friction = GroundFriction; } else { friction = AirFriction; } } else { if (!mbFly) { friction = GroundFriction; } else { friction = AirFriction; } } bool bCamJumped = false; foreach (Input.InputAction act in actions) { if (act.mAction.Equals(Program.MyActions.Jump)) { if (mbOnGround && !mbFly) { friction = AirFriction; bCamJumped = true; } } else if (act.mAction.Equals(Program.MyActions.ToggleFly)) { mbFly = !mbFly; ps.Method = (mbFly)? PlayerSteering.SteeringMethod.Fly : PlayerSteering.SteeringMethod.FirstPerson; } else if (act.mAction.Equals(Program.MyActions.Turn)) { yawAmount = act.mMultiplier; } else if (act.mAction.Equals(Program.MyActions.Pitch)) { pitchAmount = act.mMultiplier; } else if (act.mAction.Equals(Program.MyActions.RayStart)) { mRayStart = GetModelPos(); } else if (act.mAction.Equals(Program.MyActions.RayEnd)) { mRayEnd = GetModelPos(); mbRayHit = mTModel.Trace(mRayStart, mRayEnd, out mRayHit); mColRays.Add(mRayStart); mColRays.Add(mRayEnd); if (mbRayHit) { //scale up to view space Vector3 viewHit = mRayHit; viewHit.X *= 16f; viewHit.Z *= 16f; mColHits.Add(viewHit); } mDrawRays.BuildRayDrawInfo(mColRays, mColHits, 16f); } else if (act.mAction.Equals(Program.MyActions.RayCrazy)) { Stopwatch sw = new Stopwatch(); sw.Start(); mColRays.Clear(); mColHits.Clear(); Vector3 randSpace = Vector3.One * 1000f; randSpace.Y *= 5f; for (int i = 0; i < 1000; i++) { Vector3 start = Mathery.RandomPosition(mRand, randSpace); Vector3 end = Mathery.RandomPosition(mRand, randSpace); //wrap xz if (start.X < 0) { start.X += 1000f; } if (start.Z < 0) { start.Z += 1000f; } if (end.X < 0) { end.X += 1000f; } if (end.Z < 0) { end.Z += 1000f; } Vector3 hit; if (mTModel.Trace(start, end, out hit)) { //scale up to view space Vector3 viewHit = hit; viewHit.X *= 16f; viewHit.Z *= 16f; mColHits.Add(viewHit); } mColRays.Add(start); mColRays.Add(end); } sw.Stop(); mDrawRays.BuildRayDrawInfo(mColRays, mColHits, 16f); } } // UpdateDynamicLights(actions); Vector3 startPos = mGroundPos; Vector3 moveVec = ps.Update(startPos, mGD.GCam.Forward, mGD.GCam.Left, mGD.GCam.Up, actions); if (mbOnGround || mbFly) { moveVec *= JogMoveForce; } else if (mbBadFooting) { moveVec *= StumbleForce; } else { moveVec *= MidAirMoveForce; } mVelocity += moveVec * 0.5f; mVelocity -= (friction * mVelocity * secDelta * 0.5f); Vector3 pos = startPos; if (bGravity) { mVelocity += Vector3.Down * GravityForce * (secDelta * 0.5f); } if (bCamJumped) { mVelocity += Vector3.Up * JumpForce * 0.5f; pos += mVelocity * (1f / 60f); } else { pos += mVelocity * secDelta; } mVelocity += moveVec * 0.5f; mVelocity -= (friction * mVelocity * secDelta * 0.5f); if (bGravity) { mVelocity += Vector3.Down * GravityForce * (secDelta * 0.5f); } if (bCamJumped) { mVelocity += Vector3.Up * JumpForce * 0.5f; } Vector3 camPos = Vector3.Zero; Vector3 endPos = pos; // Move(endPos, time.GetUpdateDeltaMilliSeconds(), false, // mbFly, !bCamJumped, true, true, out endPos, out camPos); mGroundPos += moveVec; bool bWrapped = WrapPosition(ref mGroundPos); WrapGridCoordinates(); if (bWrapped && mTerrain != null) { mTerrain.BuildGrid(mGD, mChunkRange, mNumStreamThreads); } if (mTerrain != null) { mTerrain.SetCellCoord(mGridCoordinate); mTerrain.UpdatePosition(mGroundPos, mTerMats); } mGD.GCam.Update(-mGroundPos, ps.Pitch, ps.Yaw, ps.Roll); mOtherCam.Update(-GetViewPos(), ps.Pitch, ps.Yaw, ps.Roll); if (!mbFly) { if (mbOnGround) { //kill downward velocity so previous //falling momentum doesn't contribute to //a new jump if (mVelocity.Y < 0f) { mVelocity.Y = 0f; } } if (mbBadFooting) { //reduce downward velocity to avoid //getting stuck in V shaped floors if (mVelocity.Y < 0f) { mVelocity.Y -= (StumbleFriction * mVelocity.Y * secDelta); } } } //get ground pos at current location float groundHeight = mTModel.GetHeight(GetModelPos()); mAudio.Update(mGD.GCam); mST.ModifyStringText(mFonts[0], "Grid: " + mGridCoordinate.ToString() + ", LocalPos: " + mGroundPos.IntStr() + ", ModelPos: " + GetModelPos(), "PosStatus"); mST.ModifyStringText(mFonts[0], "Height: " + groundHeight + ", Hit: " + mbRayHit + ", HitPos: " + mRayHit, "ColStatus"); if (mTerrain != null) { mST.ModifyStringText(mFonts[0], "Threads Active: " + mTerrain.GetThreadsActive() + ", Thread Counter: " + mTerrain.GetThreadCounter(), "ThreadStatus"); } mST.Update(mGD.DC); }