Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. using XnaCommons.Core;
  14. using XnaCommons.Rendering;
  15.  
  16. using XnaGUI.Controls;
  17. using XnaGUI.Controls.Decorators;
  18.  
  19. using eyecm.PhysX;
  20.  
  21. namespace PhysXTest
  22. {
  23.     /// <summary>
  24.     /// This is the main type for your game
  25.     /// </summary>
  26.     public class MainGame : Microsoft.Xna.Framework.Game
  27.     {
  28.         GraphicsDeviceManager graphics;
  29.         Camera camera;
  30.         SpriteFont consolas;
  31.         Model rails, wheel;
  32.  
  33.         Model boxModel;
  34.  
  35.         float x = 0;
  36.  
  37.         VertexPositionColorNormal[] vpc;
  38.         BasicEffect effect;
  39.  
  40.  
  41.         public MainGame()
  42.         {
  43.             graphics = new GraphicsDeviceManager(this);
  44.             graphics.PreferMultiSampling = true;
  45.             Content.RootDirectory = "Content";
  46.             Components.Add(camera = new Camera(this) { FarPlane = 50000 });
  47.             Components.Add(new ResourceManager(this));
  48.         }
  49.  
  50.  
  51.         Physics physics;
  52.         Scene scene;
  53.  
  54.         Actor groundPlane, box;
  55.  
  56.         void CreateActorFromMesh(Model model)
  57.         {
  58.             Dictionary<string, object> modelDict = model.Tag as Dictionary<string, object>;
  59.             Vector3[] verts = modelDict["Vertices"] as Vector3[];
  60.  
  61.             ConvexMeshDesc convexDesk = new ConvexMeshDesc();
  62.             convexDesk.PinPoints<Vector3>(verts, 0, 0);
  63.         }
  64.  
  65.         Actor CreateGroundPlane()
  66.         {
  67.             ActorDesc actorDesc = new ActorDesc();
  68.             actorDesc.Shapes.Add(new PlaneShapeDesc());
  69.             return scene.CreateActor(actorDesc);
  70.         }
  71.  
  72.         Actor CreateBox()
  73.         {
  74.             ActorDesc actorDesc = new ActorDesc();
  75.             BoxShapeDesc boxDesc = new BoxShapeDesc();
  76.             float radius = boxModel.Meshes[0].BoundingSphere.Radius / 2;
  77.             boxDesc.Dimensions = new Vector3(radius, radius, radius);
  78.             actorDesc.Shapes.Add(boxDesc);
  79.  
  80.             actorDesc.Body = new BodyDesc();
  81.             actorDesc.Density = 10;
  82.             actorDesc.GlobalPose = Matrix.CreateFromYawPitchRoll(0.25f, 0.35f, 0.7f) * Matrix.CreateTranslation(10, 40.5f, 0);
  83.             return scene.CreateActor(actorDesc);
  84.         }
  85.  
  86.         private void InitPhysics()
  87.         {
  88.             physics = Physics.Create();
  89.             SceneDesc sceneDesc = new SceneDesc() { Gravity = new Vector3(0, -9.8f, 0) };
  90.             scene = physics.CreateScene(sceneDesc);
  91.  
  92.             physics.Parameters.SkinWidth = 0.01f;
  93.             physics.Parameters.VisualizationScale = 1;
  94.             physics.Parameters.VisualizeCollisionShapes = 1;
  95.  
  96.             Material defaultMaterial = scene.Materials[0];
  97.             defaultMaterial.Restitution = 0.5f;
  98.             defaultMaterial.StaticFriction = 0.5f;
  99.             defaultMaterial.DynamicFriction = 0.4f;
  100.  
  101.             box = CreateBox();
  102.             groundPlane = CreateGroundPlane();
  103.  
  104.             ProcessPhysics(0.01f);
  105.         }
  106.  
  107.         private void ProcessPhysics(float dt)
  108.         {
  109.             scene.Simulate(dt);
  110.             scene.FlushStream();
  111.         }
  112.  
  113.         private void GetPhysicsResults()
  114.         {
  115.             while (!scene.FetchResults(SimulationStatuses.AllFinished, false)) ;
  116.         }
  117.  
  118.         protected override void Initialize()
  119.         {
  120.             base.Initialize();
  121.             Window.Title = "PhysX Test";
  122.         }
  123.  
  124.         VertexBuffer vBuffer;
  125.  
  126.         protected override void LoadContent()
  127.         {
  128.             consolas = Content.Load<SpriteFont>("Fonts\\Calibri_12");
  129.             rails = Content.Load<Model>("Models\\Rails\\railway");
  130.             wheel = Content.Load<Model>("Models\\Wheel\\wheelpair");
  131.             boxModel = Content.Load<Model>("Models\\Basic\\box");
  132.             InitPhysics();
  133.  
  134.             vpc = new VertexPositionColorNormal[6];
  135.             vpc[0] = new VertexPositionColorNormal(new Vector3(0.5f, 0, 0.5f), Color.LightGray);
  136.             vpc[1] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, 0.5f), Color.Gray);
  137.             vpc[2] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, -0.5f), Color.DarkGray);
  138.  
  139.             vpc[3] = new VertexPositionColorNormal(new Vector3(-0.5f, 0, -0.5f), Color.DarkGray);
  140.             vpc[4] = new VertexPositionColorNormal(new Vector3(0.5f, 0, -0.5f), Color.LightGray);
  141.             vpc[5] = new VertexPositionColorNormal(new Vector3(0.5f, 0, 0.5f), Color.LightGray);
  142.  
  143.             vBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorNormal.vdecl, 6, BufferUsage.WriteOnly);
  144.             vBuffer.SetData<VertexPositionColorNormal>(vpc);
  145.             effect = new BasicEffect(GraphicsDevice);
  146.         }
  147.  
  148.         protected override void UnloadContent()
  149.         {
  150.         }
  151.  
  152.         protected override void Update(GameTime gameTime)
  153.         {
  154.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  155.                 this.Exit();
  156.             base.Update(gameTime);
  157.  
  158.             if (Keyboard.GetState().IsKeyDown(Keys.Up))
  159.                 box.AddForce(new Vector3(0, 40.0f, 0), ForceModes.Acceleration, true);
  160.             if (Keyboard.GetState().IsKeyDown(Keys.Down))
  161.                 box.AddForce(new Vector3(0, -40, 0), ForceModes.Acceleration, true);
  162.             if (Keyboard.GetState().IsKeyDown(Keys.Left))
  163.                 box.AddForce(new Vector3(-40, 0, 0), ForceModes.Acceleration, true);
  164.             if (Keyboard.GetState().IsKeyDown(Keys.Right))
  165.                 box.AddForce(new Vector3(40, 0, 0), ForceModes.Acceleration, true);
  166.  
  167.  
  168.             GetPhysicsResults();
  169.             ProcessPhysics((float)gameTime.ElapsedGameTime.TotalSeconds);
  170.  
  171.         }
  172.  
  173.  
  174.         private void DrawPlane()
  175.         {
  176.             effect.World = Matrix.CreateScale(5000);
  177.             effect.View = camera.View;
  178.             effect.Projection = camera.Projection;
  179.             effect.EnableDefaultLighting();
  180.             effect.VertexColorEnabled = true;
  181.             effect.PreferPerPixelLighting = true;
  182.             GraphicsDevice.SetVertexBuffer(vBuffer);
  183.             foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  184.             {
  185.                 pass.Apply();
  186.                 GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
  187.             }
  188.         }
  189.  
  190.         protected override void Draw(GameTime gameTime)
  191.         {
  192.             GraphicsDevice.DepthStencilState = new DepthStencilState();
  193.             GraphicsDevice.Clear(Color.CornflowerBlue);
  194.  
  195.  
  196.             BasicEffectRenderer.DrawModel(rails, Matrix.Identity, camera);
  197.             BasicEffectRenderer.DrawModel(wheel, Matrix.CreateRotationX(x / Microsoft.Xna.Framework.MathHelper.Pi) * Matrix.CreateTranslation(0, 4.8f, x), camera);
  198.             BasicEffectRenderer.DrawModel(boxModel, box.GlobalPose, camera);
  199.  
  200.             DrawPlane();
  201.             //rails.Draw(Matrix.Identity, camera.View, camera.Projection);
  202.  
  203.             base.Draw(gameTime);
  204.         }
  205.  
  206.  
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement