Ejemplo n.º 1
0
        public void ForceFieldShapes()
        {
            var unitPos = new Vector3(0, 0, 0);
            var unitRot = new Quaternion(0, 0, 0, 1);
            var unitScl = new Vector3(1, 1, 1);

            Vector3 alongAxis;
            Vector3 aroundAxis;
            Vector3 awayAxis;

            float falloff;

            // Sphere
            var shapeSphere = new Sphere();

            shapeSphere.PreUpdateField(unitPos, unitRot, unitScl);

            falloff = shapeSphere.GetDistanceToCenter(new Vector3(0.1f, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0.1f, falloff);
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);
            Assert.Equal(new Vector3(0, 0, -1), aroundAxis);
            Assert.Equal(new Vector3(1, 0, 0), awayAxis);

            falloff = shapeSphere.GetDistanceToCenter(new Vector3(0.5f, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0.5f, falloff);
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);
            Assert.Equal(new Vector3(0, 0, -1), aroundAxis);
            Assert.Equal(new Vector3(1, 0, 0), awayAxis);

            falloff = shapeSphere.GetDistanceToCenter(new Vector3(1, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1f, falloff);
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);
            Assert.Equal(new Vector3(0, 0, -1), aroundAxis);
            Assert.Equal(new Vector3(1, 0, 0), awayAxis);

            // Box
            var shapeBox = new Cube();

            shapeBox.PreUpdateField(unitPos, unitRot, unitScl);

            falloff = shapeBox.GetDistanceToCenter(new Vector3(0.3f, 0, 0.4f), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0.4f, falloff); // Bigger than the two
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);
            Assert.Equal(new Vector3(0.8f, 0, -0.6f), aroundAxis);
            Assert.Equal(new Vector3(0.6f, 0, 0.8f), awayAxis);

            falloff = shapeBox.GetDistanceToCenter(new Vector3(0.5f, 0, 0.4f), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0.5f, falloff);
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);

            falloff = shapeBox.GetDistanceToCenter(new Vector3(1, 0, 0.4f), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1f, falloff);
            Assert.Equal(new Vector3(0, 1, 0), alongAxis);

            // Cylinder
            var shapeCylinder = new Cylinder();

            shapeCylinder.PreUpdateField(unitPos, unitRot, unitScl);

            falloff = shapeCylinder.GetDistanceToCenter(new Vector3(0, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0, falloff);

            falloff = shapeCylinder.GetDistanceToCenter(new Vector3(0, 0.5f, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0, falloff); // No falloff along the Y-axis

            falloff = shapeCylinder.GetDistanceToCenter(new Vector3(0, 1, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1, falloff);

            falloff = shapeCylinder.GetDistanceToCenter(new Vector3(0.5f, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0.5f, falloff);

            falloff = shapeCylinder.GetDistanceToCenter(new Vector3(1, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1f, falloff);

            // Torus
            var shapeTorus = new Torus();

            shapeTorus.PreUpdateField(unitPos, unitRot, unitScl);

            falloff = shapeTorus.GetDistanceToCenter(new Vector3(0, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1, falloff); // This is actually outside the torus

            falloff = shapeTorus.GetDistanceToCenter(new Vector3(0.5f, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1, falloff); // This is on the torus surface, inner circle
            Assert.Equal(new Vector3(0, 0, -1), alongAxis);
            Assert.Equal(new Vector3(-1, 0, 0), awayAxis);
            Assert.Equal(new Vector3(0, 1, 0), aroundAxis);

            falloff = shapeTorus.GetDistanceToCenter(new Vector3(1, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(0, falloff); // This is on the torus axis
            Assert.Equal(new Vector3(0, 0, -1), alongAxis);

            falloff = shapeTorus.GetDistanceToCenter(new Vector3(1.5f, 0, 0), new Vector3(0, 1, 0), out alongAxis, out aroundAxis, out awayAxis);
            Assert.Equal(1, falloff); // This is on the torus surface, outer circle
            Assert.Equal(new Vector3(0, 0, -1), alongAxis);
            Assert.Equal(new Vector3(1, 0, 0), awayAxis);
            Assert.Equal(new Vector3(0, -1, 0), aroundAxis);
        }