private void BuildScene()
        {
            // creating two boxShapes with different sizes
            Shape boxShape = new BoxShape(JVector.One);
            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // create new instances of the rigid body class and pass 
            // the boxShapes to them
            RigidBody boxBody1 = new RigidBody(boxShape);
            RigidBody boxBody2 = new RigidBody(boxShape);

            boxBody1.Tag = Color.LightPink;
            boxBody2.Tag = Color.LightSkyBlue;

            RigidBody groundBody = new RigidBody(groundShape);

            groundBody.Tag = Color.LightGreen;

            // set the position of the box size=(1,1,1)
            // 2 and 5 units above the ground box size=(10,1,10)
            boxBody1.Position = new JVector(0, 5, 0.0f);
            boxBody2.Position = new JVector(0, 8, 0.2f);
            
            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the bodies to the world.
            world.AddBody(boxBody1);
            world.AddBody(boxBody2);
            world.AddBody(groundBody);
        }
예제 #2
0
 private void CreateCube(int height)
 {
     var boxShape = new BoxShape(JVector.One);
     var boxBody = new RigidBody(boxShape);
     boxBody.Position = new JVector(0, 0.5f + height * 2.2f, height * 0.1f);
     world.AddBody(boxBody);
 }
예제 #3
0
        public override void Build()
        {

            AddGround();

            // we need some of them!
            Demo.World.SetIterations(5);

            PseudoCloth pc = new PseudoCloth(Demo.World, 20,20, 0.5f);

            BoxShape boxShape = new BoxShape(JVector.One);

            RigidBody[] boxes = new RigidBody[4];

            int size = 19;

            for(int i=0;i<4;i++)
            {
                boxes[i] = new RigidBody(boxShape);
                boxes[i].Position = new JVector(i % 2 == 0 ? 10.0f : -0.5f, 10.5f, (i < 2) ? 10.0f : -0.5f);
               // Demo.World.AddBody(boxes[i]);



                if (i == 0)
                {

                    pc.GetCorner(size, size).IsStatic = true;
                }
                else if (i == 1)
                {

                    pc.GetCorner(size, 0).IsStatic = true;
                }
                else if (i == 2)
                {

                    pc.GetCorner(0, size).IsStatic = true;
                }
                else if (i == 3)
                {
                 
                   pc.GetCorner(0, 0).IsStatic = true;
                }

                boxes[i].IsStatic = true;
            }

            RigidBody sphereBody = new RigidBody(new SphereShape(2.0f));
            Demo.World.AddBody(sphereBody);
            sphereBody.Mass = 10.0f;
            sphereBody.Position = new JVector(5, 20, 5);

            //ConvexHullObject2 obj2 = new ConvexHullObject2(this.Demo);
            //Demo.Components.Add(obj2);

            //obj2.body.Position = new JVector(5, 30, 5);
            //Demo.World.AddBody(obj2.body);

        }
예제 #4
0
        private static CompoundShape create_shapes(RepeatedField <Collider> colliders)
        {
            if (colliders == null || colliders.Count == 0)
            {
                throw new Exception("Colliders is null or count is zero");
            }

            var shape_buffer = new List <TransformedShape>();

            foreach (var collider in colliders)
            {
                Shape      shape = null;
                Vector3    center;
                Quaternion rotation;

                switch (collider.ShapeCase)
                {
                case Collider.ShapeOneofCase.None:
                    throw new Exception("Collider without any shape");

                case Collider.ShapeOneofCase.Box:
                    var box = collider.Box;
                    shape    = new Jitter.Collision.Shapes.BoxShape(box.Length, box.Height, box.Width);
                    center   = box.Center;
                    rotation = box.Rotation;
                    break;

                case Collider.ShapeOneofCase.Sphere:
                    var sphere = collider.Sphere;
                    shape    = new Jitter.Collision.Shapes.SphereShape(sphere.Radius);
                    center   = sphere.Center;
                    rotation = sphere.Rotation;
                    break;

                case Collider.ShapeOneofCase.Capsule:
                    var capsule = collider.Capsule;
                    shape    = new Jitter.Collision.Shapes.CapsuleShape(capsule.Height, capsule.Radius);
                    center   = capsule.Center;
                    rotation = capsule.Rotation;
                    break;

                default:
                    Debug.Assert(false, "Unhandled enum value " + collider.ShapeCase);
                    throw new Exception();
                }

                var center_j          = to_j(center);
                var rotation_j        = to_j(rotation);
                var transformed_shape = new TransformedShape(shape, JMatrix.Identity, center_j);
                shape_buffer.Add(transformed_shape);

                if (collider.Children != null && collider.Children.Count > 0)
                {
                    create_shapes(collider.Children);
                }
            }

            return(new CompoundShape(shape_buffer));
        }
예제 #5
0
 public Cube(Texture texture, Vector3D position)
     : base(position, new Vector3D(1, 1, 1), 1)
 {
     this.texture = texture;
     var shape = new BoxShape(1, 1, 1);
     body = new RigidBody(shape);
     body.Position = JitterMath.ToJVector(position);
     World.world3D.AddBody(body);
     World.Add(this);
     // Create 3D Cube from Vertices and Indices
     vertices = new Vector3D[NumberOfVertices] // 6*4 = 24 edge points
     {
         new Vector3D(1, 1, 1), new Vector3D(-1, 1, 1),
         new Vector3D(-1, -1, 1), new Vector3D(1, -1, 1), // v0,v1,v2,v3 (front)
         new Vector3D(1, 1, 1), new Vector3D(1, -1, 1),
         new Vector3D(1, -1, -1), new Vector3D(1, 1, -1), // v0,v3,v4,v5 (right)
         new Vector3D(1, 1, 1), new Vector3D(1, 1, -1),
         new Vector3D(-1, 1, -1), new Vector3D(-1, 1, 1), // v0,v5,v6,v1 (top)
         new Vector3D(-1, 1, 1), new Vector3D(-1, 1, -1),
         new Vector3D(-1, -1, -1), new Vector3D(-1, -1, 1), // v1,v6,v7,v2 (left)
         new Vector3D(-1, -1, -1), new Vector3D(1, -1, -1),
         new Vector3D(1, -1, 1), new Vector3D(-1, -1, 1), // v7,v4,v3,v2 (bottom)
         new Vector3D(1, -1, -1), new Vector3D(-1, -1, -1),
         new Vector3D(-1, 1, -1), new Vector3D(1, 1, -1) // v4,v7,v6,v5 (back)
     };
     for (int i = 0; i < vertices.Length; i++)
         vertices[i] = vertices[i] / 2;
     normals = new Vector3D[NumberOfVertices];
     SetNormals(0, new Vector3D(0, 0, 1));
     SetNormals(1, new Vector3D(1, 0, 0));
     SetNormals(2, new Vector3D(0, 1, 0));
     SetNormals(3, new Vector3D(-1, 0, 0));
     SetNormals(4, new Vector3D(0, -1, 0));
     SetNormals(5, new Vector3D(0, 0, -1));
     uvs = new Vector2D[NumberOfVertices]
     {
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1),
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1),
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1),
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1),
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1),
         new Vector2D(0, 0), new Vector2D(1, 0), new Vector2D(1, 1), new Vector2D(0, 1)
     };
     indices = new short[NumberOfIndices]
     { // Front face
         0, 2, 1, 2, 0, 3,
         // Right face
         4, 6, 5, 6, 4, 7,
         // Top face
         8, 10, 9, 10, 8, 11,
         // Left face
         12, 14, 13, 14, 12, 15,
         // Bottom face
         16, 18, 17, 18, 16, 19,
         // Back face
         20, 22, 21, 22, 20, 23
     };
 }
예제 #6
0
파일: Tower.cs 프로젝트: tpb3d/TPB3D
        public override void Build()
        {
            AddGround();

            World world = Demo.World;


            Matrix halfRotationStep = Matrix.CreateRotationY(MathHelper.Pi * 2.0f / 24.0f);
            Matrix fullRotationStep = halfRotationStep * halfRotationStep;
            Matrix orientation = Matrix.Identity;

            BoxShape shape = new BoxShape(2, 1, 1);

            for (int e = 0; e < 30; e++)
            {
                orientation *= halfRotationStep;

                for (int i = 0; i < 12; i++)
                {
                    Vector3 position = Vector3.Transform(
                        new Vector3(0, 0.5f + e, 6.5f), orientation);

                    RigidBody body = new RigidBody(shape);
                    body.Orientation = Conversion.ToJitterMatrix(orientation);
                    body.Position = Conversion.ToJitterVector(position);

                    world.AddBody(body);

                    orientation *= fullRotationStep;
                }
            }




            //for (int i = 0; i < 15; i++)
            //{
            //    for (int k = 0; k < 15; k++)
            //    {
            //        for (int l = 0; l < 15; l++)
            //        {
            //            RigidBody body = new RigidBody(new BoxShape(1, 1, 1));
            //            this.Demo.World.AddBody(body);
            //            body.AffectedByGravity = false;
            //            body.Position = new JVector(i, k, l) * 2.0f;
            //            body.Restitution = 1.0f;
            //            body.Damping = RigidBody.DampingType.None;
            //            body.IsActive = false;
            //        }
            //    }
            //}


            ground.Material.StaticFriction = 1.0f;
            ground.Material.DynamicFriction = 1.0f;
        }
예제 #7
0
파일: Program.cs 프로젝트: tpb3d/TPB3D
 private RigidBody AddBox(JVector position, JVector velocity, JVector size) {
     BoxShape shape = new BoxShape(size);
     RigidBody body = new RigidBody(shape);
     world.AddBody(body);
     body.Position = position;
     body.Material.Restitution = 0.0f;
     body.LinearVelocity = velocity;
     body.IsActive = true;
     return body;
 }
예제 #8
0
 public Pickup(Engine _engine, Tracker _tracker, JVector position)
     : base(_engine, _tracker, false)
 {
     Shape boxShape = new BoxShape(1f, .5f, .5f);
     body = new RigidBody(boxShape);
     body.AffectedByGravity = false;
     body.Position = position;
     body.Tag = this;
     EnableInterfaceCalls = true;
 }
예제 #9
0
파일: PhysDuck.cs 프로젝트: dzamkov/L2D
        public PhysDuck(Path res)
        {
            Shape shape = new BoxShape(new Vector(1.0, 1.0, 1.0));
            RigidBody body = new RigidBody(shape);

            Texture txt = Texture.Load(res["Textures"]["texture.bmp"]);

            body.Position = new Vector(0.0, 0.0, 5.0);
            this._Phys = new PhysicsComponent(body);
            this._Duck = new ModelComponent(Model.LoadFile(res, "candle.obj"), this._Phys, txt);
            this._Duck.Model.Color = Color.RGB(1.0, 0.0, 0.0);
        }
예제 #10
0
        protected override RigidBody GeneratePhysicsDescription()
        {
            Shape collisionShape = new BoxShape(Scale.X, Scale.Y, Scale.Z);
            var rigidBody = new RigidBody(collisionShape)
            {
                Position = PhysicsSystem.toJVector(Position),
                Orientation = PhysicsSystem.toJMatrix(OrientationMatrix),
                IsStatic = false,
                EnableDebugDraw = true,
            };

            return rigidBody;
        }
예제 #11
0
파일: Player.cs 프로젝트: scy7he/Pong
        public Player()
        {
            Shape shape = new BoxShape(new JVector(1f, 0.3f, 2f));
            //Shape shape = new BoxShape(new JVector(2f, 2f, 1f));
            body = new RigidBody(shape);

            position = new JVector(0, 1.5f, -1f);

            body.Position = position;
            //body.IsStatic = true;

            facing = MathHelper.ToRadians((float)Math.PI/2);
        }
예제 #12
0
파일: GroundTest.cs 프로젝트: dzamkov/L2D
        public GroundTest(Path res, Vector2d Size)
        {
            Shape shape = new BoxShape(new Vector(Size.X, Size.Y, 1.0));
            RigidBody body = new RigidBody(shape);
            body.Mass = 99999f;
            body.IsStatic = true;
            body.Position = new Vector(0.0, 0.0, 0.0);
            this._Phys = new PhysicsComponent(body);
            this._Phys.Scale = new Vector(Size.X, Size.Y, 1.0);

            Texture txt = Texture.Load(res["Textures"]["texture.bmp"]);

            this._Duck = new ModelComponent(Model.LoadFile(res, "cube.obj"), this._Phys, txt);
            this._Duck.Model.Color = Color.RGB(1.0, 1.0, 1.0);
        }
예제 #13
0
        private void CreateInitialScene()
        {
            // Create the solid ground
            var groundShape = new BoxShape(9, 1, 9);
            var groundBody = new RigidBody(groundShape);
            groundBody.Position = new JVector(0, -5, 0);
            groundBody.IsStatic = true;
            world.AddBody(groundBody);

            // add the boxes to the world
            for (int i = 0; i < 17; i++)
            {
                CreateCube(i);
            }
        }
예제 #14
0
파일: World.cs 프로젝트: johang88/triton
        public Body CreateBoxBody(float length, float height, float width, Vector3 position, bool isStatic = false)
        {
            var shape = new BoxShape(length, height, width);
            var rigidBody = CreateRigidBody(shape, isStatic);

            rigidBody.Position = Conversion.ToJitterVector(ref position);

            var body = new Body(rigidBody);
            rigidBody.Tag = body;

            Bodies.Add(body);

            PhysicsWorld.AddBody(rigidBody);

            return body;
        }
예제 #15
0
        public Projectile(Engine _engine, Tracker _tracker, Vector3 _position, float _damage, float _speed, Vector3 _target, float _collisionSize)
            : base(_engine, _tracker)
        {
            damage = _damage;
            speed = _speed;
            target = _target;
            collisionSize = _collisionSize;

            //Create body and add to physics engine
            Shape boxShape = new BoxShape(size);
            body = new RigidBody(boxShape);
            body.Mass = 1f;
            body.Position = Conversion.ToJitterVector(_position);
            body.AllowDeactivation = false;
            body.AffectedByGravity = false;
            body.Tag = this;
            Engine.Physics.AddBody(body);
        }
예제 #16
0
파일: DemoGame.cs 프로젝트: tpb3d/TPB3D
        private void CreateInitialScene()
        {
            // Create the shapes
            BoxShape groundShape = new BoxShape(9, 1, 9);
            BoxShape boxShape = new BoxShape(JVector.One);

            // Create rigid bodies from them
            RigidBody groundBody = new RigidBody(groundShape);
            groundBody.IsStatic = true; // making the ground immovable
            world.AddBody(groundBody);  // finally add it to the world

            for (int i = 0; i < 7; i++)
            {
                RigidBody boxBody = new RigidBody(boxShape);         // create a new box
                boxBody.Position = new JVector(0, 0.5f + i * 2.2f, i*0.1f);  // move it
                world.AddBody(boxBody);                              // and add it
            }
           
        }
예제 #17
0
        public Player(Scene parent, Vector3 spawnPos, Vector3 viewDir, GameInput gameInput)
        {
            Parent = parent;
            this.gameInput = gameInput;

            //create hud renderer
            hud = new Hud(this);
            parent.guis.Add(hud);

            Position = spawnPos;
            PointingDirection = viewDir;

            upVector = new Vector3(0, 1, 0);
            zNear = 0.1f;
            zFar = 100;

            Shape boxShape = new BoxShape(new JVector(0.5f, 2, 0.5f));

            Body = new RigidBody(boxShape);
            Body.Position = new JVector(Position.X, Position.Y, Position.Z);
            Body.AllowDeactivation = false;

            JMatrix mMatrix = JMatrix.Identity;

            //mBody.SetMassProperties(mMatrix, 2,false);

            Jitter.Dynamics.Constraints.SingleBody.FixedAngle mConstraint = new Jitter.Dynamics.Constraints.SingleBody.FixedAngle(Body);

            parent.world.AddConstraint(mConstraint);
            parent.world.AddBody(Body);

            viewInfo = new ViewInfo(this);
            viewInfo.aspect = (float)gameWindow.Width / (float)gameWindow.Height;
            viewInfo.updateProjectionMatrix();

            tools.Add(new GameMenu(this, gameInput));
            tools.Add(new Spawner(this, gameInput));
            tools.Add(new Grabber(this, gameInput));
            tools.Add(new Remover(this, gameInput));
            tools.Add(new TerrainGun(this, gameInput));

            tool = tools[1];
        }
예제 #18
0
파일: Domino.cs 프로젝트: tpb3d/TPB3D
        public override void Build()
        {
            //this.Demo.World.Solver = Jitter.World.SolverType.Sequential;


            AddGround();


            BoxShape bShape = new BoxShape(0.5f, 4.0f, 2.0f);

            for (int i = 0; i < 10; i++)
            {
                RigidBody body = new RigidBody(bShape);
                body.Position = new JVector(i * 2.0f, 2, 0);
                this.Demo.World.AddBody(body);
            }

            ground.Material.Restitution = 0.0f;
            ground.Material.StaticFriction = 0.4f;
        }
        private void BuildScene()
        {
            // creating a box shape, representing the ground
            // and one to create the compound shape
            Shape boxShape = new BoxShape(new JVector(1,1,3));
            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // Build the CompoundShape.TransformedShape structure,
            // containing "normal" shapes and position/orientation
            // information.
            CompoundShape.TransformedShape[] transformedShapes = 
                new CompoundShape.TransformedShape[2];

            // Create a rotation matrix (90°)
            JMatrix rotated =
                Conversion.ToJitterMatrix(Matrix.CreateRotationX(MathHelper.PiOver2));

            // the first "sub" shape. A rotatated boxShape.
            transformedShapes[0] = new CompoundShape.TransformedShape(
                boxShape,rotated,JVector.Zero);

            // the second "sub" shape.
            transformedShapes[1] = new CompoundShape.TransformedShape(
                boxShape, JMatrix.Identity, JVector.Zero);

            // Pass the CompoundShape.TransformedShape structure to the compound shape.
            CompoundShape compoundShape = new CompoundShape(transformedShapes);

            RigidBody compoundBody = new RigidBody(compoundShape);

            compoundBody.Position = new JVector(0, 5, 0);

            RigidBody groundBody = new RigidBody(groundShape);
            
            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the bodies to the world.
            world.AddBody(compoundBody);
            world.AddBody(groundBody);
        }
예제 #20
0
파일: LightEntity.cs 프로젝트: gusmanb/Mir
        public LightEntity(
            LightRoomObject lightRoomObject, 
            I3DRenderUtilities threedRenderUtilities, 
            IAssetManagerProvider assetManagerProvider)
        {
            this.m_LightRoomObject = lightRoomObject;
            this.m_LightRoomObject.Deleted += this.OnDeleted;
            this.m_3DRenderUtilities = threedRenderUtilities;
            this.m_TextureAsset = assetManagerProvider.GetAssetManager().Get<TextureAsset>("ship");

            var baseX = lightRoomObject.X + 0.5f;
            var baseY = lightRoomObject.Y + 0.5f;
            var baseZ = lightRoomObject.Z + 0.5f;

            // Set up rope physics.
            this.m_RopeComponents = new RigidBody[12];
            this.m_PendingConstraints = new List<Constraint>();
            for (var i = 0; i < 12; i++)
            {
                var shape = new BoxShape(0.5f, 0.5f, 0.5f);
                var rigidBody = new RigidBody(shape);
                rigidBody.Position = new JVector(baseX, baseY - (i * 0.6f), baseZ);
                this.m_RopeComponents[i] = rigidBody;

                if (i == 0)
                {
                    rigidBody.IsStatic = true;
                }
                else
                {
                    var constraint = new PointOnPoint(
                        rigidBody,
                        this.m_RopeComponents[i - 1],
                        new JVector(baseX, baseY - (i * 0.6f) + 0.3f, baseZ)) {
                                                                                 BiasFactor = 0.8f, Softness = 0.4f
                                                                              };
                    this.m_PendingConstraints.Add(constraint);
                }
            }
        }
        private void BuildScene()
        {
            // creating two boxShapes with different sizes
            Shape boxShape = new BoxShape(JVector.One);
            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // create new instances of the rigid body class and pass 
            // the boxShapes to them
            RigidBody boxBody1 = new RigidBody(boxShape);
            RigidBody boxBody2 = new RigidBody(boxShape);

            boxBody1.Tag = Color.LightPink;
            boxBody2.Tag = Color.LightSkyBlue;

            RigidBody groundBody = new RigidBody(groundShape);

            groundBody.Tag = Color.LightGreen;

            // set the position of the box size=(1,1,1)
            // 2 and 5 units above the ground box size=(10,1,10)
            boxBody1.Position = new JVector(0, 4, 1.0f);
            boxBody2.Position = new JVector(0, 4, -1.0f);

            pointConstraint = new PointConstraint(
                boxBody1, boxBody2, new JVector(0, 4f, 0));

            // add a force to one body - so it's not that boring
            boxBody1.AddForce(JVector.One * 10);

            world.AddConstraint(pointConstraint);

            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the bodies to the world.
            world.AddBody(boxBody1);
            world.AddBody(boxBody2);
            world.AddBody(groundBody);
        }
예제 #22
0
        public override void Build()
        {
            BoxShape shape = new BoxShape(JVector.One);

            // CollisionSystemBrute        170 ms
            // CollisionSystemSAP          7   ms
            // CollisionSystemPersistenSAP 1   ms

            for (int i = 0; i < 15; i++)
            {
                for (int e = 0; e < 15; e++)
                {
                    for (int k = 0; k < 15; k++)
                    {
                        RigidBody b = new RigidBody(shape);
                        Demo.World.AddBody(b);
                        b.Position = new JVector(i, e, k) * 2.0f;
                        b.AffectedByGravity = false;
                    }
                }
            }
        }
예제 #23
0
        private Shape CreateShape()
        {
            Shape shape = null;
            boundingBox = new BoundingBox();
            List<Geometry> geom = RenderUtil.GatherGeometry(GameObject);
            List<Mesh> meshes = new List<Mesh>();
            List<Matrix4f> matrices = new List<Matrix4f>();
            Vector3f mytrans = GameObject.GetUpdatedWorldTranslation();
            if (GameObject is Geometry)
            {
                Geometry g_obj = (Geometry)GameObject;

                Mesh m = g_obj.Mesh;
                meshes.Add(m);
                Transform ttransform = new Transform();
                ttransform.SetTranslation(g_obj.GetUpdatedWorldTranslation());
                ttransform.SetRotation(g_obj.GetUpdatedWorldRotation());
                ttransform.SetScale(g_obj.GetUpdatedWorldScale());
                Matrix4f matrix = ttransform.GetMatrix();
                BoundingBox tmpBB = m.CreateBoundingBox();
                matrices.Add(matrix);
                boundingBox.Extend(tmpBB);
            }
            for (int i = 0; i < geom.Count; i++)
            {
                Geometry g = geom[i];
                if (g != GameObject)
                {
                    Mesh m = g.Mesh;
                    meshes.Add(m);
                    Transform ttransform = new Transform();
                    ttransform.SetTranslation(g.GetUpdatedWorldTranslation().Subtract(mytrans));
                    ttransform.SetRotation(g.GetUpdatedWorldRotation());
                    ttransform.SetScale(g.GetUpdatedWorldScale());
                    Matrix4f matrix = ttransform.GetMatrix();
                    BoundingBox tmpBB = m.CreateBoundingBox(matrix);
                    matrices.Add(matrix);
                    boundingBox.Extend(tmpBB);
                }
            }
            if (physicsShape == PhysicsWorld.PhysicsShape.StaticMesh)
            {
                List<JVector> jvec = new List<JVector>();
                List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>();
                List<Vector3f> vertexPositions = new List<Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].vertices.Count; v++)
                    {
                        Vector3f myvec = meshes[m].vertices[v].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                    }
                    for (int i = 0; i < meshes[m].indices.Count; i += 3)
                    {
                        tv.Add(new TriangleVertexIndices(meshes[m].indices[i + 2], meshes[m].indices[i + 1], meshes[m].indices[i]));
                    }
                }
                Octree oct = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);

                shape = trimesh;
                oct = null;
                jvec.Clear();
                tv.Clear();
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.ConvexMesh)
            {
                List<JVector> jvec = new List<JVector>();
                List<Vector3f> vertexPositions = new List<Vector3f>();

                for (int m = 0; m < meshes.Count; m++)
                {
                    for (int v = 0; v < meshes[m].indices.Count; v++)
                    {
                        Vector3f vec = meshes[m].vertices[meshes[m].indices[v]].GetPosition().Multiply(matrices[m]);
                        jvec.Add(new JVector(vec.x, vec.y, vec.z));
                    }
                }

                ConvexHullShape hullShape = new ConvexHullShape(jvec);
                shape = hullShape;
            }
            else if (physicsShape == PhysicsWorld.PhysicsShape.Box)
            {
               /* Mesh boxMesh = MeshFactory.CreateCube(boundingBox.Min, boundingBox.Max);
                List<JVector> jvec = new List<JVector>();
                List<TriangleVertexIndices> tv = new List<TriangleVertexIndices>();
                for (int v = 0; v < boxMesh.vertices.Count; v++)
                {
                    Vector3f myvec = boxMesh.vertices[v].GetPosition();
                    jvec.Add(new JVector(myvec.x, myvec.y, myvec.z));
                }
                for (int i = 0; i < boxMesh.indices.Count; i += 3)
                {
                    tv.Add(new TriangleVertexIndices(boxMesh.indices[i], boxMesh.indices[i + 1], boxMesh.indices[i + 2]));
                }

                Octree oct = new Octree(jvec, tv);
                TriangleMeshShape trimesh = new TriangleMeshShape(oct);*/
                shape = new BoxShape(boundingBox.Extent.x, boundingBox.Extent.y, boundingBox.Extent.z);
            //    shape = trimesh;
            //    tv.Clear();
             //   jvec.Clear();
             //   oct = null;
             //   boxMesh = null;
            }
            meshes.Clear();
            matrices.Clear();
            geom.Clear();
            return shape;
        }
예제 #24
0
        public void Load(Stream fs, bool leaveOpen)
        {
            // Reset whole world
            foreach (var block in blocks.ToArray())
            {
                this[block.X, block.Y, block.Z] = null;
            }

            Action<bool> assert = (b) => { if (!b) throw new InvalidDataException(); };
            using (var br = new BinaryReader(fs, Encoding.UTF8, leaveOpen))
            {
                assert(br.ReadString() == "BLOCKWORLD");
                int major = br.ReadByte();
                int minor = br.ReadByte();
                assert(major == 1);

                int typeCount = br.ReadInt32();
                Type[] types = new Type[typeCount];
                for (int i = 0; i < typeCount; i++)
                {
                    string typeName = br.ReadString();
                    types[i] = Type.GetType(typeName);
                    if (types[i] == null)
                        throw new TypeLoadException("Could not find type " + typeName);
                }

                long blockCount = br.ReadInt64();
                for (long i = 0; i < blockCount; i++)
                {
                    int x = br.ReadInt32();
                    int y = br.ReadInt32();
                    int z = br.ReadInt32();
                    Type type = types[br.ReadInt32()];

                    var block = Activator.CreateInstance(type) as Block;

                    block.Deserialize(br);

                    this[x, y, z] = block;
                }

                // Details are stored here as well
                if(minor == 1)
                {
                    int detailCount = br.ReadInt32();
                    for (int i = 0; i < detailCount; i++)
                    {
                        int id = br.ReadInt32();
                        string model = br.ReadString();
                        int parentID = br.ReadInt32();

                        var position = br.ReadVector3();
                        var rotation = br.ReadQuaternion();

                        Shape shape = null;

                        bool hasShape = br.ReadBoolean();
                        if (hasShape)
                        {
                            var size = br.ReadVector3();
                            shape = new BoxShape(size.Jitter());
                        }

                        DetailObject detail = new DetailObject(this.GetDetail(parentID), id, shape);

                        if (string.IsNullOrWhiteSpace(model) == false)
                            detail.Model = model;

                        detail.Position = position;
                        detail.Rotation = rotation;

                        this.RegisterDetail(detail);

                        int behaviourCount = br.ReadInt32();
                        for (int j = 0; j < behaviourCount; j++)
                        {
                            var typeName = br.ReadString();
                            var type = Type.GetType(typeName, true);

                            int behaviourID = br.ReadInt32();
                            bool isEnabled = br.ReadBoolean();

                            var behaviour = (Behaviour)Activator.CreateInstance(type);

                            detail.CreateBehaviour(behaviour, behaviourID, isEnabled);
                        }
                    }

                    int signalCount = br.ReadInt32();
                    for (int j = 0; j < signalCount; j++)
                    {
                        var signalDetailID = br.ReadInt32();
                        var signalBehaviourID = br.ReadInt32();
                        var signalName = br.ReadString();

                        var signal = this.GetDetail(signalDetailID).Behaviours[signalBehaviourID].Signals[signalName];

                        var slotCount = br.ReadInt32();
                        for (int k = 0; k < slotCount; k++)
                        {
                            var slotDetailID = br.ReadInt32();
                            var slotBehaviourID = br.ReadInt32();
                            var slotName = br.ReadString();

                            var slot = this.GetDetail(slotDetailID).Behaviours[slotBehaviourID].Slots[slotName];

                            signal.Connect(slot);
                        }
                    }
                }
            }
        }
예제 #25
0
        private void SetupRoomPhysics()
        {
            // Set up the static physics bodies for each wall.
            var floorShape = new BoxShape(new JVector(this.m_Room.Width, 1, this.m_Room.Depth));
            var floorBody = new RigidBody(floorShape);
            this.m_JitterWorld.AddBody(floorBody);
            floorBody.Position = new JVector(
                /*this.m_Room.X +*/ (this.m_Room.Width / 2f),
                /*this.m_Room.Y*/ - 0.5f,
                /*this.m_Room.Z +*/ (this.m_Room.Depth / 2f));
            floorBody.IsStatic = true;

            var roofShape = new BoxShape(new JVector(this.m_Room.Width, 1, this.m_Room.Depth));
            var roofBody = new RigidBody(roofShape);
            this.m_JitterWorld.AddBody(roofBody);
            roofBody.Position = new JVector(
                /*this.m_Room.X*/ + (this.m_Room.Width / 2f),
                /*this.m_Room.Y*/ + this.m_Room.Height + 0.5f,
                /*this.m_Room.Z*/ + (this.m_Room.Depth / 2f));
            roofBody.IsStatic = true;

            var leftShape = new BoxShape(new JVector(1, this.m_Room.Height, this.m_Room.Depth));
            var leftBody = new RigidBody(leftShape);
            this.m_JitterWorld.AddBody(leftBody);
            leftBody.Position = new JVector(
                /*this.m_Room.X*/ - 0.5f,
                /*this.m_Room.Y*/ + (this.m_Room.Height / 2f),
                /*this.m_Room.Z*/ + (this.m_Room.Depth / 2f));
            leftBody.IsStatic = true;

            var rightShape = new BoxShape(new JVector(1, this.m_Room.Height, this.m_Room.Depth));
            var rightBody = new RigidBody(rightShape);
            this.m_JitterWorld.AddBody(rightBody);
            rightBody.Position = new JVector(
                /*this.m_Room.X*/ + this.m_Room.Width + 0.5f,
                /*this.m_Room.Y*/ + (this.m_Room.Height / 2f),
                /*this.m_Room.Z*/ + (this.m_Room.Depth / 2f));
            rightBody.IsStatic = true;

            var backShape = new BoxShape(new JVector(this.m_Room.Width, this.m_Room.Height, 1));
            var backBody = new RigidBody(backShape);
            this.m_JitterWorld.AddBody(backBody);
            backBody.Position = new JVector(
                /*this.m_Room.X*/ + (this.m_Room.Width / 2f),
                /*this.m_Room.Y*/ + (this.m_Room.Height / 2f),
                /*this.m_Room.Z*/ - 0.5f);
            backBody.IsStatic = true;

            var frontShape = new BoxShape(new JVector(this.m_Room.Width, this.m_Room.Height, 1));
            var frontBody = new RigidBody(frontShape);
            this.m_JitterWorld.AddBody(frontBody);
            frontBody.Position = new JVector(
                /*this.m_Room.X*/ + (this.m_Room.Width / 2f),
                /*this.m_Room.Y*/ + (this.m_Room.Height / 2f),
                /*this.m_Room.Z*/ +this.m_Room.Depth + 0.5f);
            frontBody.IsStatic = true;
        }
예제 #26
0
        /*
        public Model
            BloomViewboard,
            BloomViewboard2,
            CompositeViewboard,
            AoViewboard,
            AoBlrViewboard;
         */
        public Scene(OpenTkProjectWindow mGameWindow)
        {
            this.gameWindow = mGameWindow;
            Scene = this;

            sunLight = new LightSun(new Vector3(0.1f, 0.125f, 0.2f) * 3f, this);
            sunLight.lightAmbient = new Vector3(0.1f, 0.125f, 0.2f) * 0.5f;//new Vector3(0.2f, 0.125f, 0.1f);//new Vector3(0.1f, 0.14f, 0.3f);
            sunLight.PointingDirection = Vector3.Normalize(new Vector3(674, -674, 1024));
            sunFrameBuffer = gameWindow.framebufferCreator.createFrameBuffer("shadowFramebuffer", shadowRes * 2, shadowRes * 2, PixelInternalFormat.Rgba8, false);
            sunInnerFrameBuffer = gameWindow.framebufferCreator.createFrameBuffer("shadowFramebuffer", shadowRes * 2, shadowRes * 2, PixelInternalFormat.Rgba8, false);

            //sunLight.pointingDirection = Vector3.Normalize(new Vector3(674, 674, 1024));

            // creating a new collision system and adding it to the new world
            CollisionSystem collisionSystem = new CollisionSystemSAP();
            world = new World(collisionSystem);

            // Create the groundShape and the body.
            Shape groundShape = new BoxShape(new JVector(100, waterLevel * 2, 100));
            RigidBody groundBody = new RigidBody(groundShape);

            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the ground to the world.
            world.AddBody(groundBody);
        }
예제 #27
0
        /// <summary>
        /// Adds a unit sized test cube to the physics world.
        /// Deprecated.
        /// </summary>
        /// <param name="position"></param>
        /// <param name="mass"></param>
        public void addTestBox(Vector3 position, float mass)
        {
            // these can actually be reused between objects to save memory
            Shape boxShape = new Jitter.Collision.Shapes.BoxShape(new JVector(1f, 1f, 1f));

            RigidBody body = new RigidBody(new BoxShape(new JVector(1.0f, 1.0f, 1.0f)));
            body.IsStatic = false;
            body.AffectedByGravity = true;
            body.Position = new JVector(position.X, position.Y, position.Z);
            this.World.AddBody(body);
        }
예제 #28
0
파일: JitterDemo.cs 프로젝트: tpb3d/TPB3D
        private void SpawnRandomPrimitive(JVector position, JVector velocity)
        {
            RigidBody body = null;
            int rndn = random.Next(7);

            // less of the more advanced objects
            if (rndn == 5 || rndn == 6) rndn = random.Next(7);

            switch (rndn)
            {
                case 0:
                    body = new RigidBody(new ConeShape((float)random.Next(5, 50) / 20.0f, (float)random.Next(10, 20) / 20.0f));
                    break;
                case 1:
                    body = new RigidBody(new BoxShape((float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f));
                    break;
                case 2:
                    body = new RigidBody(new SphereShape(1.0f));
                    break;
                case 3:
                    body = new RigidBody(new CylinderShape(1.0f, 0.5f));
                    break;
                case 4:
                    body = new RigidBody(new CapsuleShape(1.0f, 0.5f));
                    break;
                case 5:
                    Shape b1 = new BoxShape(new JVector(3, 1, 1));
                    Shape b2 = new BoxShape(new JVector(1, 1, 3));
                    Shape b3 = new CylinderShape(3.0f, 0.5f);

                    CompoundShape.TransformedShape t1 = new CompoundShape.TransformedShape(b1, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t2 = new CompoundShape.TransformedShape(b2, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t3 = new CompoundShape.TransformedShape(b3, JMatrix.Identity, new JVector(0, 0, 0));

                    CompoundShape ms = new CompoundShape(new CompoundShape.TransformedShape[3] { t1, t2, t3 });

                    body = new RigidBody(ms);
                    break;
                case 6:
                    ConvexHullObject obj2 = new ConvexHullObject(this);
                    Components.Add(obj2);
                    body = obj2.body;
                    body.Material.Restitution = 0.2f;
                    body.Material.StaticFriction = 0.8f;
                    break;
            }

            World.AddBody(body);

            body.Position = position;
            body.LinearVelocity = velocity;
            //body.EnableDebugDraw = true;

            lastBody = body;
        }
예제 #29
0
파일: Game1.cs 프로젝트: scy7he/Pong
        /// <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);

            if (content == null)
                content = new ContentManager(this.Services, "Content");

            /*
            player = new Player();

            world.AddBody(player.body);
            */

            // ball
            JVector position = new JVector(0f, 5f, -10f);
            Shape sphereShape = new SphereShape(1.0f);
            ball = new RigidBody(sphereShape);
            ball.Position = position;
            ball.Material.Restitution = 2.0f;
            ball.Mass = 10f;
            ball.IsStatic = true;

            // floor
            Shape boxShape = new BoxShape(new JVector(20f, 1.5f, 40f));
            floor = new RigidBody(boxShape);
            floor.Position = JVector.Zero;
            floor.IsStatic = true;

            // paddles
            Shape box = new BoxShape(new JVector(5f, 5f, 0.5f));
            paddle1 = new RigidBody(box);
            paddle2 = new RigidBody(box);
            paddle1.Position = JVector.Zero - new JVector(0f,0f,20f);
            paddle2.Position = JVector.Zero + new JVector(0f, 0f, 20f);
            paddle1.IsStatic = true;
            paddle2.IsStatic = true;

            world.AddBody(paddle1);
            world.AddBody(paddle2);
            world.AddBody(ball);
            world.AddBody(floor);
        }
        protected override void Update(GameTime gameTime)
        {
            if (codeFormVisible) return;

            KeyboardState keyState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            if (keyState.IsKeyDown(Keys.Escape)) this.Exit();

            bool leftHold = (mouseState.LeftButton == ButtonState.Pressed);
            bool spaceHold = (keyState.IsKeyDown(Keys.Space));
            bool enterHold = (keyState.IsKeyDown(Keys.Enter));
            bool mHold = (keyState.IsKeyDown(Keys.M));

            #region Turn multithreading on/off
            if (mHold)
            {
                if (!mClicked) { multithread = !multithread; mClicked = true; }
            }
            #endregion

            #region Object drag & drop

            if (leftHold && !leftClicked)
            {
                JVector ray = Conversion.ToJitterVector(RayTo(mouseState.X, mouseState.Y)); ray.Normalize();
                JVector camp = Conversion.ToJitterVector(Camera.Position);

                float fraction;
                bool result = world.CollisionSystem.Raycast(camp, ray * 100, RaycastCallback, out resBody, out hitNormal, out fraction);

                if (result)
                {
                    hitPoint = camp + fraction * ray * 100;

                    if (wp != null) world.RemoveConstraint(wp);

                    JVector lanchor = hitPoint - resBody.Position;
                    lanchor = JVector.Transform(lanchor, JMatrix.Transpose(resBody.Orientation));

                    wp = new WorldPointConstraint(resBody, lanchor);

                    world.AddConstraint(wp);
                    hitDistance = (Conversion.ToXNAVector(hitPoint) - Camera.Position).Length();
                    scrollWheel = mouseState.ScrollWheelValue;
                    wp.Anchor = hitPoint;
                }

                leftClicked = true;

            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                hitDistance += (mouseState.ScrollWheelValue - scrollWheel) * 0.001f;
                scrollWheel = mouseState.ScrollWheelValue;

                if (resBody != null)
                {
                    Vector3 ray = RayTo(mouseState.X, mouseState.Y); ray.Normalize();
                    wp.Anchor = Conversion.ToJitterVector(Camera.Position + ray * hitDistance);
                }
            }
            else
            {
                resBody = null;
                if (wp != null) world.RemoveConstraint(wp);
            }

            #endregion

            #region Show code form

            if (enterHold && !enterClicked && gameTime.TotalGameTime.TotalSeconds > 0.1f)
            {
                display.DisplayText[5] = string.Empty;

                System.Windows.Forms.Form form =
                        (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.Window.Handle);

                codeFormVisible = true;
                System.Windows.Forms.DialogResult result = codeForm.ShowDialog(form);
                codeFormVisible = false;

                enterClicked = true;
            }
            #endregion

            #region Spawn random primitive
            if (spaceHold && !spaceClicked)
            {
                int rndn = random.Next(6);

                RigidBody body;

                if (rndn == 0)
                {
                    body = new RigidBody(new ConeShape((float)random.Next(5, 50) / 20.0f, (float)random.Next(10, 20) / 20.0f));
                }
                else if (rndn == 1)
                {
                    body = new RigidBody(new BoxShape(new JVector(
                        (float)random.Next(10, 30) / 20.0f,
                        (float)random.Next(10, 30) / 20.0f, (float)random.Next(10, 30) / 20.0f)));
                }
                else if (rndn == 2)
                {
                    body = new RigidBody(new SphereShape((float)random.Next(30,100) /100.0f));
                }
                else if (rndn == 3)
                {
                    body = new RigidBody(new CylinderShape(1.0f, 0.5f));
                }
                else if (rndn == 4)
                {
                    body = new RigidBody(new CapsuleShape(1.0f, 0.5f));
                }
                else
                {
                    Shape b1 = new BoxShape(new JVector(3, 1, 1));
                    Shape b2 = new BoxShape(new JVector(1, 1, 3));
                    Shape b3 = new CylinderShape(2.0f, 0.5f);

                    CompoundShape.TransformedShape t1 = new CompoundShape.TransformedShape(b1, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t2 = new CompoundShape.TransformedShape(b2, JMatrix.Identity, JVector.Zero);
                    CompoundShape.TransformedShape t3 = new CompoundShape.TransformedShape(b3, JMatrix.Identity, new JVector(0, 0, 0));

                    CompoundShape ms = new CompoundShape(new CompoundShape.TransformedShape[3] { t1, t2, t3 });

                    body = new RigidBody(ms);
                }

                world.AddBody(body);

                body.Position = Conversion.ToJitterVector(Camera.Position);
                body.LinearVelocity = Conversion.ToJitterVector((Camera.Target - Camera.Position) * 40.0f);
                body.Update();

                spaceClicked = true;
            }
            #endregion

            spaceClicked = spaceHold;
            leftClicked = leftHold;
            enterClicked = enterHold;
            mClicked = mHold;

            int contactCount = 0;
            foreach (Arbiter ar in world.ArbiterMap.Values)
                contactCount += ar.ContactList.Count;

            display.DisplayText[0] = "Arbitercount: " + world.ArbiterMap.Values.Count.ToString() + ";" + " Contactcount: " + contactCount.ToString();
            display.DisplayText[2] = "Bodycount: " + world.RigidBodies.Count;
            display.DisplayText[3] = (multithread) ? "Multithreaded" : "Single Threaded";

            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (elapsedTime > 1.0f / 100.0f) elapsedTime = 1.0f / 100.0f;
            world.Step(elapsedTime, multithread);

            base.Update(gameTime);
        }
예제 #31
0
        /*
        public Model
            BloomViewboard,
            BloomViewboard2,
            CompositeViewboard,
            AoViewboard,
            AoBlrViewboard;
         */
        public Scene(OpenTkProjectWindow mGameWindow)
        {
            //prepare list of world textures
            int texCount = Enum.GetValues(typeof(Material.WorldTexture)).Length;
            if (worldTextures == null)
                worldTextures = new Texture[texCount];

            this.gameWindow = mGameWindow;
            Scene = this;
            //sunLight.pointingDirection = Vector3.Normalize(new Vector3(674, 674, 1024));

            // creating a new collision system and adding it to the new world
            CollisionSystem collisionSystem = new CollisionSystemSAP();
            world = new World(collisionSystem);

            // Create the groundShape and the body.
            Shape groundShape = new BoxShape(new JVector(100, waterLevel * 2, 100));
            RigidBody groundBody = new RigidBody(groundShape);

            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the ground to the world.
            world.AddBody(groundBody);
        }