Collision with terrain
Posted: Thu Nov 21, 2013 11:28 pm
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.
And for creating the terrain
Finally in my draw method
[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();
}
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));
//======================================================================================
}
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);
}