Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using IrrlichtLime;
- using IrrlichtLime.Video;
- using IrrlichtLime.Scene;
- using BulletSharp;
- namespace lime_bulletsharpTry
- {
- class Program
- {
- static void Main(string[] args)
- {
- IrrlichtDevice device = IrrlichtDevice.CreateDevice(DriverType.Direct3D9, new IrrlichtLime.Core.Dimension2Di(1024, 768));
- CameraSceneNode camera = device.SceneManager.AddCameraSceneNodeFPS(null, 180, 0.1f);
- camera.Position = new IrrlichtLime.Core.Vector3Df(0, 50, -50);
- camera.Target = new IrrlichtLime.Core.Vector3Df(0);
- DefaultCollisionConfiguration bulletCollisionConfiguration = new DefaultCollisionConfiguration();
- CollisionDispatcher bulletCollisionDispatcher = new CollisionDispatcher(bulletCollisionConfiguration);
- BroadphaseInterface bulletBroadphase = new DbvtBroadphase();
- DiscreteDynamicsWorld bulletWorld = new DiscreteDynamicsWorld(bulletCollisionDispatcher, bulletBroadphase, null, bulletCollisionConfiguration);
- bulletWorld.Gravity = new Vector3(0, -100, 0);
- bulletCreateRigidBody(
- bulletWorld,
- 0,
- Matrix.Identity,
- new BoxShape(50, 1, 50));
- for (int i = 0; i < 16; i++)
- {
- for (int j = 0; j < 16; j++)
- {
- bulletCreateRigidBody(
- bulletWorld,
- 1.0f,
- Matrix.Translation(- 16 + j * 2, 2 + i * 2, 0),
- new BoxShape(1));
- }
- }
- uint curTime = 0, lastTime = 0;
- while (device.Run())
- {
- // calc bullet world {{{
- lastTime = curTime;
- curTime = device.Timer.Time;
- float deltaTime = (curTime - lastTime) / 1000.0f;
- bulletWorld.StepSimulation(deltaTime);
- // }}}
- device.VideoDriver.BeginScene(true, true, new Color(0x112244));
- device.SceneManager.DrawAll();
- Material material = new Material();
- material.Lighting = false;
- device.VideoDriver.SetMaterial(material);
- // draw bullet world {{{
- foreach (CollisionObject collObj in bulletWorld.CollisionObjectArray)
- {
- IrrlichtLime.Core.Matrix m = new IrrlichtLime.Core.Matrix();
- m.SetElementArray(collObj.WorldTransform.ToArray());
- device.VideoDriver.SetTransform(TransformationState.World, m);
- if (collObj.CollisionShape is BoxShape)
- {
- BoxShape boxShape = collObj.CollisionShape as BoxShape;
- Vertex3D[] vertices = new Vertex3D[8];
- for (int i = 0; i < boxShape.VertexCount; i++)
- {
- Vector3 v;
- boxShape.GetVertex(i, out v);
- vertices[i] = new Vertex3D(v.X, v.Y, v.Z);
- vertices[i].Color = new Color(80 + i * 20, 80 + i * 20, 80 + i * 20);
- }
- ushort[] indices = new ushort[] {
- 0, 1, 2, 1, 3, 2, // back
- 4, 6, 5, 5, 6, 7, // front
- 0, 4, 1, 1, 4, 5, // top
- 1, 5, 7, 1, 7, 3, // left
- 0, 2, 4, 2, 6, 4, // right
- 6, 2, 3, 6, 3, 7 // bottom
- };
- device.VideoDriver.DrawVertexPrimitiveList(vertices, indices);
- }
- else
- {
- device.Logger.Log("Unable visualize " + collObj.CollisionShape.GetType().ToString(), LogLevel.Error);
- }
- }
- // }}}
- device.GUIEnvironment.BuiltInFont.Draw(
- "FPS: " + (int)(1.0f / deltaTime),
- new IrrlichtLime.Core.Vector2Di(10),
- Color.OpaqueCyan);
- device.VideoDriver.EndScene();
- }
- bulletWorld.Dispose();
- bulletBroadphase.Dispose();
- bulletCollisionDispatcher.Dispose();
- bulletCollisionConfiguration.Dispose();
- device.Drop();
- }
- public static RigidBody bulletCreateRigidBody(DiscreteDynamicsWorld world, float mass, Matrix startTransform, CollisionShape shape)
- {
- bool isDynamic = (mass != 0.0f);
- Vector3 localInertia = Vector3.Zero;
- if (isDynamic)
- shape.CalculateLocalInertia(mass, out localInertia);
- DefaultMotionState myMotionState = new DefaultMotionState(startTransform);
- RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
- RigidBody body = new RigidBody(rbInfo);
- world.AddRigidBody(body);
- return body;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement