Ejemplo n.º 1
0
        //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);
        }
Ejemplo n.º 2
0
        internal void TBuild(int gridSize, int chunkSize, float medianHeight,
                             float variance, int polySize, int tilingIterations, float borderSize,
                             int smoothPasses, int seed, int erosionIterations,
                             float rainFall, float solubility, float evaporation, int threads)
        {
            mFracFact = new FractalFactory(variance, medianHeight, gridSize + 1, gridSize + 1);

            mNumStreamThreads = threads;

            float   [,] fract = mFracFact.CreateFractal(seed);

            for (int i = 0; i < smoothPasses; i++)
            {
                FractalFactory.SmoothPass(fract);
            }

            for (int i = 0; i < tilingIterations; i++)
            {
                float borderSlice = borderSize / tilingIterations;

                FractalFactory.MakeTiled(fract, borderSlice * (i + 1));
            }

            if (erosionIterations > 0)
            {
                int realIterations = FractalFactory.Erode(fract, mRand,
                                                          erosionIterations, rainFall, solubility, evaporation);

                //redo tiling if eroded
                for (int i = 0; i < tilingIterations; i++)
                {
                    float borderSlice = borderSize / tilingIterations;

                    FractalFactory.MakeTiled(fract, borderSlice * (i + 1));
                }
            }

            Vector3 [,] norms = mFracFact.BuildNormals(fract, polySize);

            if (mTModel != null)
            {
                mTModel.FreeAll();
            }
            mTModel = new TerrainModel(fract, polySize, gridSize);

            mCellGridMax = gridSize / chunkSize;

            List <HeightMap.TexData> tdata = new List <HeightMap.TexData>();

            float transHeight = 0f;

            if (mTerrain != null)
            {
                //grab a copy of the old texture data if any
                List <HeightMap.TexData> texOld = mTerrain.GetTextureData(out transHeight);

                //clone it because it is about to all get nuked
                foreach (HeightMap.TexData td in texOld)
                {
                    HeightMap.TexData td2 = new HeightMap.TexData(td);
                    tdata.Add(td2);
                }
                mTerrain.FreeAll();
            }
            mTerrain = new Terrain(fract, norms, polySize, chunkSize, mCellGridMax);

            mTerrain.SetTextureData(tdata, transHeight);

            mBoundary = chunkSize * polySize;

            WrapGridCoordinates();

            mTerrain.SetCellCoord(mGridCoordinate);

            mTerrain.BuildGrid(mGD, mChunkRange, mNumStreamThreads);

            mPos.Y = mTModel.GetHeight(mPos) + 200f;

            mTerrain.UpdatePosition(mPos, mTerMats);

            //clamp box heights
            mTModel.FixBoxHeights();

            //clear existing
            if (mQTreeBoxes != null)
            {
                mQTreeBoxes.Free();
            }

            //turn on to debug quadtree
            //careful about big map sizes
//			List<BoundingBox>	boxes	=mTModel.GetAllBoxes();
//			mQTreeBoxes	=PrimFactory.CreateCubes(mGD.GD, boxes);
        }