Ok, i got it kinda working. (I haven't implemented the camera yet though). So far i have this:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Threading;
using BEPUphysics;
using System.Diagnostics;
namespace AG_DMT
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Space space;
Box box;
Model boxModel;
Model map;
Texture2D mapTexture;
Vector3 mapPosition = Vector3.Zero;
Player p1;
GamePadState oldGamePadState = GamePad.GetState(PlayerIndex.One);
EntityModel boxEntity;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected void StartPhysicsEng()
{
space = new Space(new PersistentUniformGrid(10));
//general settings (grav, etc) OMITTED see demo_source/current location for all settings default assigns
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
p1 = new Player(1, new Vector3(0, 2, -10), new Vector3(0, 0, 1), graphics, Content.Load<Model>("Models\\Characters\\player_bounding"));
StartPhysicsEng(); //Init physics engine space
#region LoadMap
map = Content.Load<Model>("Models\\Maps\\map13ds");
mapTexture = Content.Load<Texture2D>("Models\\Textures\\blank");
StaticTriangleGroup mapGroup = new StaticTriangleGroup(.5f, 1, 0, 0, .4f, 0);
mapGroup.initializeData(map);
mapGroup.friction = .6f;
space.add(mapGroup);
#endregion
#region boxtest
boxModel = Content.Load<Model>("Models\\Characters\\player_bounding");
Box box = new Box(new Vector3(0, 50, 0), 20, 20, 20);
space.add(box);
EntityModel boxEntity = new EntityModel(box, boxModel, Matrix.CreateScale(box.width, box.height, box.length), this);
Components.Add(boxEntity);
#endregion
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//update player cameras
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
p1.UpdateMovement(gamePadState, oldGamePadState);
p1.UpdateCamera(graphics);
physicsLoop(gameTime);
base.Update(gameTime);
}
void physicsLoop(GameTime gameTime)
{
space.update(gameTime);
}
void DrawModel(Model model, Matrix world, Texture2D texture)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect be in mesh.Effects)
{
be.EnableDefaultLighting();
be.PreferPerPixelLighting = true;
be.Projection = p1.cProj;
be.View = p1.cView;
be.World = world;
be.Texture = texture;
be.TextureEnabled = true;
}
mesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawModel(map, Matrix.Identity, mapTexture);
boxEntity.Draw(gameTime, p1.cView, p1.cProj);
base.Draw(gameTime);
}
}
}
I'm also using a the EntityModel class from the example you sent me. I modified the Draw method though:
Code: Select all
public void Draw(GameTime gameTime, Matrix view, Matrix proj)
{
Matrix worldMatrix = transform * entity.orientationMatrix * Matrix.CreateTranslation(entity.centerPosition);
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
effect.View = view;
effect.Projection = proj;
}
mesh.Draw();
}
base.Draw(gameTime);
}
Now, whenever I compile, I get a message saying boxEntity not set to instance of object. Am I doing something wrong here? Maybe something to do with the drawablegamecomponent thing?
Sry if i'm missing something obvious. This is just my first time working with physics wrappers.