Beispiel #1
0
        public static void AddModel(Search3DBase<FacetRef> search, KModel model)
        {
            for (int facetIndex = 0; facetIndex < model.Facets.Count; facetIndex++)
            {
                var facet = model.Facets[facetIndex];

                search.AddTriangle(new FacetRef() { Model = model, faId = facetIndex },
                    model.Vertices[facet.Ve0].Pt, model.Vertices[facet.Ve1].Pt, model.Vertices[facet.Ve2].Pt);
            }
        }
Beispiel #2
0
        public void Update(Search3DBase<FacetRef> levelSearch)
        {
            // Gravity
            Velocity.Z -= 0.005f;

            // Resistance (friction)
            if (IsOnTheGround)
            {
                const float lmod = 256*0.02f;
                var T1 = lmod*Velocity.LengthSquared();
                if (T1 < 0.05f) T1 = 0.05f;
                Velocity *= (1 - T1);
                // max 1.5 pixels!
            }

            // Move
            PositionLast = Position;
            Position += Velocity;

            // Collision
            IsOnTheGround = false;
            Vector3d ptClosest;
            while (levelSearch.ClosestPointToPoint(Position, Radius, out ptClosest) != null)
            {
                var vOut = Position - (Vector3)ptClosest;
                var dirOut = vOut.Normalized();
                var penetration = Radius - vOut.Length();

                Position += (penetration + 0.001f) * dirOut;

                var velocityTangential = Velocity.ProjectOrthogonal(dirOut);
                var velocityNormal = Velocity - velocityTangential;
                velocityNormal *= -0.5f;
                if (velocityNormal.Length() < 0.01f)
                    velocityNormal = Vector3.Zero;
                Velocity = velocityTangential + velocityNormal;

                if (dirOut.Z > 0.5)
                    IsOnTheGround = true;
            }

            // Rotate
            var move = Position - PositionLast;
            var axis = new Vector2(-move.Y, move.X);
            var angle = new Vector2(move.X, move.Y).Length() / Radius;
            if (angle > 0.1 * Math.PI / 180)
            {
                Orientation = Orientation * Matrix.CreateFromAxisAngle(new Vector3(axis, 0).Normalized(), angle);
                //ToDo: enforce orthogonality
            }
        }
Beispiel #3
0
        /// <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);

            _font = Content.Load<SpriteFont>(@"Fonts\TestFont");

            // TODO: use this.Content to load your game content here
            _imageKK = Content.Load<Texture2D>(@"Images\KK");
            var imageFloor = Content.Load<Texture2D>(@"Images\FloorWoodLight");
            var imageBox = Content.Load<Texture2D>(@"Images\BoxSides");
            var imageBall = Content.Load<Texture2D>(@"Images\football");
            var imageSpikyBall = Content.Load<Texture2D>(@"Images\spiky");

            _levelSearch = new Search3DSimple<FacetRef>();

            var model = new KModel();
            model.TexCoordsAreInPixels = false;
            int size = 100;
            model.AddQuad(
                model.AddVertex(new Vector3(-size, -size, 0)),
                model.AddVertex(new Vector3(size, -size, 0)),
                model.AddVertex(new Vector3(-size, size, 0)),
                model.AddVertex(new Vector3(size, size, 0)),
                new Rectangle(0, 0, 10, 10));
            _drawableKK = new DrawableModel(model, imageFloor, GraphicsDevice, false);
            ModelSearch.AddModel(_levelSearch, model);

            model = new KModel();
            var boxes = new List<Vector3>
            {
                new Vector3(1, 1, 0), new Vector3(2, 1, 0), new Vector3(2, 1, 1), new Vector3(2, 1, 2),
                new Vector3(3, 1, 0), new Vector3(3, 1, 1), new Vector3(3, 1, 2),
                new Vector3(-2, 1, 0), new Vector3(-2, 1, 1),
                new Vector3(-3, -3, 0), new Vector3(-3, -3, 1), new Vector3(-3, -3, 2), new Vector3(-3, -3, 3),
                new Vector3(-3, -6, 0), new Vector3(-3, -6, 1), new Vector3(-3, -6, 2), new Vector3(-3, -6, 3),
                new Vector3(-3, -4, 3), new Vector3(-3, -5, 3),
            };
            for (int j = 0; j < 5; j++)
                for (int i = -20; i < 20; i++)
                {
                    //boxes.Add(new Vector3(i, -20, j));
                    boxes.Add(new Vector3(-20, i + 1, j));
                    boxes.Add(new Vector3(i + 1, 20, j));
                    boxes.Add(new Vector3(20, i, j));
                }
            foreach (var corner in boxes)
                model.AddBox(new BoundingBox(corner, corner + new Vector3(1, 1, 1)), new Rectangle(0, 128, 225, 225), false);
            /*            model.AddBox(new BoundingBox(new Vector3(3, 3, 1), new Vector3(5, 5, 3)), new Rectangle(0, 0, 64, 64), true);
                        model.AddBox(new BoundingBox(new Vector3(-5, -5, 1), new Vector3(-3, -3, 3)), new Rectangle(0, 0, 64, 64), true); */
            model.AddBox(new BoundingBox(new Vector3(2, -5, 0), new Vector3(2, -5, 0) + new Vector3(2.5f, 2.5f, 2.5f)), new Rectangle(0, 128, 225, 225), false);
            _drawableBox = new DrawableModel(model, imageBox, GraphicsDevice, false);
            ModelSearch.AddModel(_levelSearch, model);

            model = GeometricalPrimitives.CreateBall(2, new Rectangle(0, 0, 1, 1));
            model.TexCoordsAreInPixels = false;
            _drawableBall = new DrawableModel(model, imageBall, GraphicsDevice, true);

            model = GeometricalPrimitives.CreateSpikyBall(3, 0.2f, new Rectangle(0, 0, 1, 1));
            model.TexCoordsAreInPixels = false;
            _drawableSpikyBall = new DrawableModel(model, imageSpikyBall, GraphicsDevice, true);

            SceneRenderer.EnableShadows(GraphicsDevice, Content.Load<Effect>(@"Shaders\ShadowCasting"), 4, 2048);

            InitLights();
        }