Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 XnaCommons.Core;
- using XnaCommons.Rendering;
- using XnaGUI.Controls;
- using XnaGUI.Controls.Decorators;
- using eyecm.PhysX;
- namespace PhysXTest
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class MainGame : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- Camera camera;
- SpriteFont consolas;
- Model rails, wheel;
- Model boxModel;
- float x = 0;
- VertexPositionColorNormal[] vpc;
- BasicEffect effect;
- public MainGame()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferMultiSampling = true;
- Content.RootDirectory = "Content";
- Components.Add(camera = new Camera(this) { FarPlane = 50000 });
- Components.Add(new ResourceManager(this));
- }
- Physics physics;
- Scene scene;
- Actor groundPlane, box;
- void CreateActorFromMesh(Model model)
- {
- Dictionary<string, object> modelDict = model.Tag as Dictionary<string, object>;
- Vector3[] verts = modelDict["Vertices"] as Vector3[];
- ConvexMeshDesc convexDesk = new ConvexMeshDesc();
- convexDesk.PinPoints<Vector3>(verts, 0, 0);
- }
- Actor CreateGroundPlane()
- {
- ActorDesc actorDesc = new ActorDesc();
- actorDesc.Shapes.Add(new PlaneShapeDesc());
- return scene.CreateActor(actorDesc);
- }
- Actor CreateBox()
- {
- ActorDesc actorDesc = new ActorDesc();
- BoxShapeDesc boxDesc = new BoxShapeDesc();
- float radius = boxModel.Meshes[0].BoundingSphere.Radius / 2;
- boxDesc.Dimensions = new Vector3(radius, radius, radius);
- actorDesc.Shapes.Add(boxDesc);
- actorDesc.Body = new BodyDesc();
- actorDesc.Density = 10;
- actorDesc.GlobalPose = Matrix.CreateFromYawPitchRoll(0.25f, 0.35f, 0.7f) * Matrix.CreateTranslation(10, 40.5f, 0);
- return scene.CreateActor(actorDesc);
- }
- private void InitPhysics()
- {
- physics = Physics.Create();
- SceneDesc sceneDesc = new SceneDesc() { Gravity = new Vector3(0, -9.8f, 0) };
- scene = physics.CreateScene(sceneDesc);
- physics.Parameters.SkinWidth = 0.01f;
- physics.Parameters.VisualizationScale = 1;
- physics.Parameters.VisualizeCollisionShapes = 1;
- Material defaultMaterial = scene.Materials[0];
- defaultMaterial.Restitution = 0.5f;
- defaultMaterial.StaticFriction = 0.5f;
- defaultMaterial.DynamicFriction = 0.4f;
- box = CreateBox();
- groundPlane = CreateGroundPlane();
- ProcessPhysics(0.01f);
- }
- private void ProcessPhysics(float dt)
- {
- scene.Simulate(dt);
- scene.FlushStream();
- }
- private void GetPhysicsResults()
- {
- while (!scene.FetchResults(SimulationStatuses.AllFinished, false)) ;
- }
- protected override void Initialize()
- {
- base.Initialize();
- Window.Title = "PhysX Test";
- }
- VertexBuffer vBuffer;
- protected override void LoadContent()
- {
- consolas = Content.Load<SpriteFont>("Fonts\\Calibri_12");
- rails = Content.Load<Model>("Models\\Rails\\railway");
- wheel = Content.Load<Model>("Models\\Wheel\\wheelpair");
- boxModel = Content.Load<Model>("Models\\Basic\\box");
- InitPhysics();
- vpc = new VertexPositionColorNormal[6];
- vpc[0] = new VertexPositionColorNormal(new Vector3(0.5f, 0, 0.5f), Color.LightGray);
- vpc[1] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, 0.5f), Color.Gray);
- vpc[2] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, -0.5f), Color.DarkGray);
- vpc[3] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, -0.5f), Color.DarkGray);
- vpc[4] = new VertexPositionColorNormal(new Vector3(0.5f, 0, -0.5f), Color.LightGray);
- vpc[5] = new VertexPositionColorNormal(new Vector3(0.5f, 0, 0.5f), Color.LightGray);
- vBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorNormal.vdecl, 6, BufferUsage.WriteOnly);
- vBuffer.SetData<VertexPositionColorNormal>(vpc);
- effect = new BasicEffect(GraphicsDevice);
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- base.Update(gameTime);
- if (Keyboard.GetState().IsKeyDown(Keys.Up))
- box.AddForce(new Vector3(0, 40.0f, 0), ForceModes.Acceleration, true);
- if (Keyboard.GetState().IsKeyDown(Keys.Down))
- box.AddForce(new Vector3(0, -40, 0), ForceModes.Acceleration, true);
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- box.AddForce(new Vector3(-40, 0, 0), ForceModes.Acceleration, true);
- if (Keyboard.GetState().IsKeyDown(Keys.Right))
- box.AddForce(new Vector3(40, 0, 0), ForceModes.Acceleration, true);
- GetPhysicsResults();
- ProcessPhysics((float)gameTime.ElapsedGameTime.TotalSeconds);
- }
- private void DrawPlane()
- {
- effect.World = Matrix.CreateScale(5000);
- effect.View = camera.View;
- effect.Projection = camera.Projection;
- effect.EnableDefaultLighting();
- effect.VertexColorEnabled = true;
- effect.PreferPerPixelLighting = true;
- GraphicsDevice.SetVertexBuffer(vBuffer);
- foreach (EffectPass pass in effect.CurrentTechnique.Passes)
- {
- pass.Apply();
- GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
- }
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.DepthStencilState = new DepthStencilState();
- GraphicsDevice.Clear(Color.CornflowerBlue);
- BasicEffectRenderer.DrawModel(rails, Matrix.Identity, camera);
- BasicEffectRenderer.DrawModel(wheel, Matrix.CreateRotationX(x / Microsoft.Xna.Framework.MathHelper.Pi) * Matrix.CreateTranslation(0, 4.8f, x), camera);
- BasicEffectRenderer.DrawModel(boxModel, box.GlobalPose, camera);
- DrawPlane();
- //rails.Draw(Matrix.Identity, camera.View, camera.Projection);
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement