Beispiel #1
0
        public void CapsuleSphereCollision()
        {
            // r=1.5 at (0,8,0)
            var sphere = SphereBody.Create(_simulation,
                                           new Vector3(0, 8, 0),
                                           1.5f);

            // pointing straight up, should hit sphere
            var capsule1 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.Identity,
                                              1f,
                                              6f);

            // rotated 45deg, should miss sphere
            var capsule2 = CapsuleBody.Create(this._simulation,
                                              new Vector3(0, 0, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitX, (float)(0.125 * Math.Tau)),
                                              1f,
                                              6f);

            // floating, rotated 90deg, should hit sphere
            var capsule3 = CapsuleBody.Create(this._simulation,
                                              new Vector3(8, 8, 0),
                                              Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)(0.25 * Math.Tau)),
                                              1f,
                                              6f);

            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule1, sphere));
            Assert.IsFalse(PhysicsSimulation.CapsuleSphereCollision(capsule2, sphere));
            Assert.IsTrue(PhysicsSimulation.CapsuleSphereCollision(capsule3, sphere));
        }
Beispiel #2
0
        /// <summary>
        /// Adds a sphere physics body to the object.
        /// </summary>
        /// <param name="radius"></param>
        /// <param name="name"></param>
        public void SetProximityRadius(float radius, string name)
        {
            // Add an object and a sphere physics body.
            // One game object/script may require multiple physics bodies.
            var proximityObject = GameObject.Instantiate(GameObject.Zone);
            var physics         = proximityObject.AddComponent <PhysicsComponent>();
            var physicsObject   = SphereBody.Create(
                this.GameObject.Zone.Simulation,
                this.GameObject.Transform.Position,
                radius);

            physics.SetPhysics(physicsObject);

            // Listen for players entering and leaving.
            var playersInPhysicsObject = new List <Player>();

            Listen(physics.OnEnter, (component) =>
            {
                if (!(component.GameObject is Player player))
                {
                    return;
                }
                if (playersInPhysicsObject.Contains(player))
                {
                    return;
                }
                playersInPhysicsObject.Add(player);
                this.OnProximityUpdate(name, PhysicsCollisionStatus.Enter, player);
            });
Beispiel #3
0
        public void BoxSphereCollision()
        {
            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (7,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(7, 0, 0),
                                                4f);

            // r=0.5 at (5,5,0)
            var sphere2 = SphereBody.Create(_simulation,
                                            new Vector3(5, 5, 0),
                                            0.5f);

            // r=0.5 at (6,0,0)
            var sphere3 = SphereBody.Create(_simulation,
                                            new Vector3(6, 0, 0),
                                            0.5f);

            // 10x10x10 at (0,0,0)
            var boxAtZero = BoxBody.Create(_simulation,
                                           new Vector3(0, 0, 0),
                                           Quaternion.Identity,
                                           Vector3.One * 10);

            // No rotation
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereClose));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphereFar));

            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere2));
            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZero, sphere3));

            // Rotate box by 45deg around Z axis. Should now miss small sphere at (5,5,0) and hit small sphere at (6,0,0)
            var boxAtZeroRotated = BoxBody.Create(_simulation,
                                                  new Vector3(0, 0, 0),
                                                  Quaternion.CreateFromAxisAngle(Vector3.UnitZ, (float)Math.Tau / 8),
                                                  Vector3.One * 10);

            Assert.IsFalse(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere2));
            Assert.IsTrue(PhysicsSimulation.BoxSphereCollision(boxAtZeroRotated, sphere3));
        }
Beispiel #4
0
        public void SphereSphereCollision()
        {
            // r=10 at (0,0,0)
            var sphereAtZero = SphereBody.Create(_simulation, new Vector3(0, 0, 0),
                                                 10f);

            // r=10 at (100,0,0)
            var sphereFar = SphereBody.Create(_simulation,
                                              new Vector3(100, 0, 0),
                                              10f);

            // r=4 at (11,0,0)
            var sphereClose = SphereBody.Create(_simulation,
                                                new Vector3(11, 0, 0),
                                                4f);

            Assert.IsFalse(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereFar));
            Assert.IsTrue(PhysicsSimulation.SphereSphereCollision(sphereAtZero, sphereClose));
        }
Beispiel #5
0
        override protected void OnLoad(System.EventArgs e)
        {
            lastMouseState = OpenTK.Input.Mouse.GetState();

            Texture angrySquirrelTexture, angryTurtleTexture, angryDilloTexture;

            Texture[] worldTextures;
            Mesh      squirrelMesh, turtleMesh, dilloMesh;

            RectangleMesh[] worldMeshes;

            float aspectRatio = ClientSize.Width / (float)(ClientSize.Height);

            projectionMatrix.set(Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1, 1000));

            cameraOnFrame = delegate(Matrix4f mvMatrix) {
                float   radius = 40, y = 20, timeRatio = 0.5f;
                Vector3 pos = new Vector3(
                    (float)Math.Sin(time.Value * timeRatio) * radius,
                    y,
                    (float)Math.Cos(time.Value * timeRatio) * radius);
                mvMatrix.set(Matrix4.LookAt(pos, new Vector3(0, y * 0.5f * (1.5f + (float)Math.Sin(time.Value * timeRatio)), 0), new Vector3(0, 1, 0)));
            };
            cameraOnFrame(modelviewMatrix);
            cameraOnFrame = null; // enable/disable

            ambientColor.set(0.4f, 0.4f, 0.4f);
            lightColor.set(1.0f, 1.0f, 1.0f);
            lightDirUnit.set(0.5f, 1.0f, 0.5f);
            //lightDirUnit.set( 0.1f, 5.0f, 0.1f );
            lightDirUnit.normalize();
            //texture = new Texture( "E:\\drop\\logo-dark.jpg" );
            angrySquirrelTexture = new Texture("gfx/angry-squirrel.png");
            angryTurtleTexture   = new Texture("gfx/angry-turtle.png");
            angryDilloTexture    = new Texture("gfx/angry-armadillo.png");

            worldTextures = new Texture[] {
                new Texture("gfx/drzewka-1.png"),
                new Texture("gfx/drzewka-3.png"),
                new Texture("gfx/sky.png"),
                new Texture("gfx/grass.png"),
                new Texture("gfx/drzewka-2.png"),
                new Texture("gfx/drzewka-4.png")
            };

            CreateShaders();

            squirrelMesh         = new SphereMesh(BALL_RADIUS, 20, 20, true);
            squirrelMesh.Texture = angrySquirrelTexture;

            /*
             * squirrelMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationZ( time.Value ) );
             *  //transform.setTranslationY( BALL_RADIUS + JUMP_HEIGHT * 0.5f * (1 + (float) Math.Sin( time.Value )) ); // hover
             *  //transform.setTranslationY( BALL_RADIUS + JUMP_HEIGHT * (float) Math.Abs( Math.Sin( time.Value ) ) ); // hover
             * };
             * squirrelMesh.UpdateAction( squirrelMesh );
             */
            //sm.writeOBJ();
            meshes.Add(squirrelMesh);

            turtleMesh         = new SphereMesh(BALL_RADIUS * 5, 10, 10, true);
            turtleMesh.Texture = angryTurtleTexture;

            /*
             * turtleMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationX( -time.Value ) );
             * };
             * turtleMesh.UpdateAction( turtleMesh );
             */
            meshes.Add(turtleMesh);

            dilloMesh         = new SphereMesh(BALL_RADIUS * 2, 20, 20, true);
            dilloMesh.Texture = angryDilloTexture;

            /*
             * dilloMesh.UpdateAction = delegate( Mesh model ) {
             *  Matrix4f transform = model.Transform;
             *  transform.setScaleAndRotation( Matrix4.CreateRotationX( -time.Value ) );
             * };
             * dilloMesh.UpdateAction( dilloMesh );
             */
            meshes.Add(dilloMesh);

            float boxX = 100, boxY = 50, boxZ = 100, shiftX = 0.5f * boxX, shiftY = 0.5f * boxY, shiftZ = 0.5f * boxZ;

            worldMeshes = new RectangleMesh[] {
                new RectangleMesh(Vector3f.OZ, boxX, boxY),
                new RectangleMesh(Vector3f.OZ, boxX, -boxY),
                new RectangleMesh(Vector3f.OY, boxX, boxZ),
                new RectangleMesh(Vector3f.OY, -boxX, boxZ),
                new RectangleMesh(Vector3f.OZ, -boxX, boxY),
                new RectangleMesh(Vector3f.OZ, boxX, boxY)
            };
            for (int i = worldMeshes.Length - 1; i >= 0; i--)
            {
                Mesh m = worldMeshes[i];
                m.Texture = worldTextures[i];
                meshes.Add(m);
            }
            Matrix4f trans;

            trans = worldMeshes[0].Transform;
            trans.setZero();
            trans.Data[2]  = 1;
            trans.Data[5]  = 1;
            trans.Data[8]  = 1;
            trans.Data[15] = 1;
            trans.setTranslation(shiftX, shiftY, 0);
            trans = worldMeshes[1].Transform;
            trans.setZero();
            trans.Data[2]  = -1;
            trans.Data[5]  = -1;
            trans.Data[8]  = -1;
            trans.Data[15] = 1;
            worldMeshes[1].Transform.setTranslation(-shiftX, shiftY, 0);
            worldMeshes[2].Transform.setTranslation(0, boxY, 0);
            worldMeshes[3].Transform.setTranslation(0, 0, 0);
            worldMeshes[4].Transform.setTranslation(0, shiftY, shiftZ);
            worldMeshes[5].Transform.setTranslation(0, shiftY, -shiftZ);

            foreach (Mesh m in meshes)
            {
                m.init();
            }

            //bodyManager.Gravity.setZero();
            /*SphereBody*/
            squirrelBody = new SphereBody(1, BALL_RADIUS);
            squirrelBody.Transform.setTranslation(40, 40, 40);
            squirrelBody.RotationSpeed = 1f;
            /*SphereBody*/
            turtleBody = new SphereBody(25, BALL_RADIUS * 5);
            turtleBody.Transform.setTranslation(0, 20, 0);
            //turtleBody.Velocity.Y = 5;
            turtleBody.RotationSpeed = 1f;
            /*SphereBody*/
            dilloBody = new SphereBody(25, BALL_RADIUS * 2);
            //dilloBody.Transform.setTranslation( -20, 100, -30 );
            dilloBody.Transform.setTranslation(0, 30, 0);
            //dilloBody.Velocity.Y = -5;
            dilloBody.RotationSpeed = 1f;
            Vector3f fixPoint = new Vector3f(0, 42, 9);

            /*
             * // spring - vertical harmonic oscillator
             * dilloBody.Forces.Add( delegate( Body obj ) {
             *  Vector3f disp = obj.Transform.getDisplacement( fixPoint );
             *  float k = 20.0f;
             *  disp.scale( k );
             *  obj.applyForce( disp );
             * } );
             */

            // springy pendulum - 3D harmonic oscillator
            dilloBody.Forces.Add(delegate(Body obj) {
                Vector3f disp = obj.Transform.getDisplacement(fixPoint);
                float k       = 10.0f, l = 15.0f;
                disp.scale(k * (disp.length() - l));
                obj.applyForce(disp);
            });

            planeBodies = new PlaneBody[] {
                new PlaneBody(new Plane3f(new Vector3f(0, 1, 0), 0)),
                new PlaneBody(new Plane3f(new Vector3f(0, -1, 0), boxY)),
                new PlaneBody(new Plane3f(new Vector3f(1, 0, 0), shiftX)),
                new PlaneBody(new Plane3f(new Vector3f(-1, 0, 0), shiftX)),
                new PlaneBody(new Plane3f(new Vector3f(0, 0, 1), shiftZ)),
                new PlaneBody(new Plane3f(new Vector3f(0, 0, -1), shiftZ))
            };

            foreach (PlaneBody pb in planeBodies)
            {
                pb.Friction = 0.1f;
                bodyManager.addBody(pb);
            }
            bodyManager.addBody(squirrelBody, squirrelMesh);
            bodyManager.addBody(turtleBody, turtleMesh);
            bodyManager.addBody(dilloBody, dilloMesh);

            VSync = VSyncMode.On;
        }