Collision with terrain

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Jack Burton
Posts: 12
Joined: Sun Nov 03, 2013 9:29 pm

Collision with terrain

Post by Jack Burton »

I have a problem with my player colliding with my terrain I've made a short YouTube video to show the collision in action

[youtube]

https://www.youtube.com/watch?v=iAyWFoqH_vI

[/youtube]

as you can see the collision happens way above the terrain, I've went over and over the code and everything seems to be right, a point to note also is the bounding box I have made to show the size of the shipColBox is also not being drawn.
I've tried to pinpoint it buy changing the size of the shipColBox but it has the same result. Has anyone seen this problem before?

This my code for creating the player ship.

Code: Select all

public Ship(GraphicsDevice device, Game1 game)
        {

            graphicsDevice = device;
            this.game = game;
            shipColBox = new Box(shipPos, 0.9f, 0.9f, 0.9f);
            //shipColBox = new Box(shipPos, 10f, 10f, 10f);
            game.space.Add(shipColBox);
            shipColBox.Mass = 1.0f;
            shipColBox.IsAffectedByGravity = true;
            //shipColBox.BecomeDynamic(1);          
            //shipColBox.LinearVelocity = new Vector3(0,-100f,0f);
            shipModel = new EntityModel(shipColBox, game.Content.Load<Model>("Models/Ship"), Matrix.Identity * Matrix.CreateScale(0.5f), game);
            game.Components.Add(shipModel);

            shipColBox.Tag = shipModel;            

            Reset();
      }
And for creating the terrain

Code: Select all

public void DrawTerrain()
        {
            //===============================TERRAIN================================================
            //Create a physical environment from a triangle mesh.
            //First, collect the the mesh data from the model using a helper function.
            //This special kind of vertex inherits from the TriangleMeshVertex and optionally includes
            //friction/bounciness data.
            //The StaticTriangleGroup requires that this special vertex type is used in lieu of a normal TriangleMeshVertex array.
            Vector3[] vertices;
            int[] indices;
            TriangleMesh.GetVerticesAndIndicesFromModel(terrain, out vertices, out indices);
            //Give the mesh information to a new StaticMesh.  
            //Give it a transformation which scoots it down below the kinematic box entity we created earlier.
            var mesh = new StaticMesh(vertices, indices, new AffineTransform(new Vector3(0, -30, 0)));

            //Add it to the space!
            space.Add(mesh);
            //Make it visible too.
            Components.Add(new StaticModel(terrain, mesh.WorldTransform.Matrix * Matrix.CreateScale(1000f), this));
            //======================================================================================         

        }

Finally in my draw method

Code: Select all

protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice device = graphics.GraphicsDevice;
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            spriteBatch.DrawString(font, "Ship Pos: " + ship.shipColBox.Position, new Vector2(10, 10), Color.Black);

            spriteBatch.End();  

            RasterizerState rasterizerState = new RasterizerState();
            rasterizerState.FillMode = FillMode.Solid;
            GraphicsDevice.RasterizerState = rasterizerState;

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;            

            debugDrawer.LightingEnabled = false;
            debugDrawer.VertexColorEnabled = true;
            debugDrawer.World = Matrix.Identity;
            debugDrawer.View = camera.View;
            debugDrawer.Projection = camera.Projection;

            debugBoundingBoxDrawer.Draw(debugDrawer, space);

            //DrawModel(shipModel, ship.World);
            //DrawModel(groundModel, Matrix.CreateScale(1000f) * Matrix.CreateTranslation(0,-29000,0));     
            
            base.Draw(gameTime);
        }

Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Collision with terrain

Post by Norbo »

The graphics appear to be enormous relative to the simulation. It's acceptable to have the graphics be on a different scale than the physics, but it has to be done consistently. Right now, it looks like the scale of the graphics is something like x1000, but the positions aren't scaled by that. The result is that movement seems very insignificant relative to the size, and the relative positions of objects don't look right.

I would recommend just scaling the graphics down to match the simulation.
Jack Burton
Posts: 12
Joined: Sun Nov 03, 2013 9:29 pm

Re: Collision with terrain

Post by Jack Burton »

Amazing Norbo thanks again problem solved :) now on to my next task adding enemys :)
Post Reply