Advertisement
Guest User

IrrlichtLime with BulletSharp

a guest
Jul 4th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using IrrlichtLime;
  7. using IrrlichtLime.Video;
  8. using IrrlichtLime.Scene;
  9.  
  10. using BulletSharp;
  11.  
  12. namespace lime_bulletsharpTry
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             IrrlichtDevice device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new IrrlichtLime.Core.Dimension2Di(1024, 768));
  19.  
  20.             CameraSceneNode camera = device.SceneManager.AddCameraSceneNodeFPS(null, 180, 0.1f);
  21.             camera.Position = new IrrlichtLime.Core.Vector3Df(0, 50, -50);
  22.             camera.Target = new IrrlichtLime.Core.Vector3Df(0);
  23.  
  24.             DefaultCollisionConfiguration bulletCollisionConfiguration = new DefaultCollisionConfiguration();
  25.             CollisionDispatcher bulletCollisionDispatcher = new CollisionDispatcher(bulletCollisionConfiguration);
  26.             BroadphaseInterface bulletBroadphase = new DbvtBroadphase();
  27.             DiscreteDynamicsWorld bulletWorld = new DiscreteDynamicsWorld(bulletCollisionDispatcher, bulletBroadphase, null, bulletCollisionConfiguration);
  28.             bulletWorld.Gravity = new Vector3(0, -100, 0);
  29.  
  30.             bulletCreateRigidBody(
  31.                 bulletWorld,
  32.                 0,
  33.                 Matrix.Identity,
  34.                 new BoxShape(50, 1, 50));
  35.  
  36.             for (int i = 0; i < 16; i++)
  37.             {
  38.                 for (int j = 0; j < 16; j++)
  39.                 {
  40.                     bulletCreateRigidBody(
  41.                         bulletWorld,
  42.                         1.0f,
  43.                         Matrix.Translation(- 16 + j * 2, 2 + i * 2, 0),
  44.                         new BoxShape(1));
  45.                 }
  46.             }
  47.  
  48.             uint curTime = 0, lastTime = 0;
  49.             while (device.Run())
  50.             {
  51.                 // calc bullet world {{{
  52.                 lastTime = curTime;
  53.                 curTime = device.Timer.Time;
  54.                 float deltaTime = (curTime - lastTime) / 1000.0f;
  55.                 bulletWorld.StepSimulation(deltaTime);
  56.                 // }}}
  57.  
  58.                 device.VideoDriver.BeginScene(true, true, new Color(0x112244));
  59.  
  60.                 device.SceneManager.DrawAll();
  61.  
  62.                 Material material = new Material();
  63.                 material.Lighting = false;
  64.                 device.VideoDriver.SetMaterial(material);
  65.  
  66.                 // draw bullet world {{{
  67.                 foreach (CollisionObject collObj in bulletWorld.CollisionObjectArray)
  68.                 {
  69.                     IrrlichtLime.Core.Matrix m = new IrrlichtLime.Core.Matrix();
  70.                     m.SetElementArray(collObj.WorldTransform.ToArray());
  71.                     device.VideoDriver.SetTransform(TransformationState.World, m);
  72.  
  73.                     if (collObj.CollisionShape is BoxShape)
  74.                     {
  75.                         BoxShape boxShape = collObj.CollisionShape as BoxShape;
  76.  
  77.                         Vertex3D[] vertices = new Vertex3D[8];
  78.                         for (int i = 0; i < boxShape.VertexCount; i++)
  79.                         {
  80.                             Vector3 v;
  81.                             boxShape.GetVertex(i, out v);
  82.                             vertices[i] = new Vertex3D(v.X, v.Y, v.Z);
  83.                             vertices[i].Color = new Color(80 + i * 20, 80 + i * 20, 80 + i * 20);
  84.                         }
  85.  
  86.                         ushort[] indices = new ushort[] {
  87.                             0, 1, 2, 1, 3, 2, // back
  88.                             4, 6, 5, 5, 6, 7, // front
  89.                             0, 4, 1, 1, 4, 5, // top
  90.                             1, 5, 7, 1, 7, 3, // left
  91.                             0, 2, 4, 2, 6, 4, // right
  92.                             6, 2, 3, 6, 3, 7 // bottom
  93.                         };
  94.  
  95.                         device.VideoDriver.DrawVertexPrimitiveList(vertices, indices);
  96.                     }
  97.                     else
  98.                     {
  99.                         device.Logger.Log("Unable visualize " + collObj.CollisionShape.GetType().ToString(), LogLevel.Error);
  100.                     }
  101.                 }
  102.                 // }}}
  103.  
  104.                 device.GUIEnvironment.BuiltInFont.Draw(
  105.                     "FPS: " + (int)(1.0f / deltaTime),
  106.                     new IrrlichtLime.Core.Vector2Di(10),
  107.                     Color.OpaqueCyan);
  108.  
  109.                 device.VideoDriver.EndScene();
  110.             }
  111.  
  112.             bulletWorld.Dispose();
  113.             bulletBroadphase.Dispose();
  114.             bulletCollisionDispatcher.Dispose();
  115.             bulletCollisionConfiguration.Dispose();
  116.  
  117.             device.Drop();
  118.         }
  119.  
  120.         public static RigidBody bulletCreateRigidBody(DiscreteDynamicsWorld world, float mass, Matrix startTransform, CollisionShape shape)
  121.         {
  122.             bool isDynamic = (mass != 0.0f);
  123.  
  124.             Vector3 localInertia = Vector3.Zero;
  125.             if (isDynamic)
  126.                 shape.CalculateLocalInertia(mass, out localInertia);
  127.  
  128.             DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
  129.  
  130.             RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
  131.             RigidBody body = new RigidBody(rbInfo);
  132.  
  133.             world.AddRigidBody(body);
  134.  
  135.             return body;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement