Пример #1
0
        public RopeTester()
        {
            const float Length  = 36;
            const float Damping = 0.95f;
            const float Gravity = 20;

            vec2[] points = new vec2[18];

            for (int i = 0; i < points.Length; i++)
            {
                points[i] = new vec2(60, 120) + new vec2(Length * i * 1.25f, 0);
            }

            rope = new VerletRope2D(points, Length, Damping, Gravity);

            MessageSystem.Subscribe(this, CoreMessageTypes.Mouse, (messageType, data, dt) =>
            {
                var mouse = (MouseData)data;

                if (mouse.Query(GLFW_MOUSE_BUTTON_LEFT, InputStates.PressedThisFrame))
                {
                    var array = rope.Points;
                    var index = new Random().Next(array.Length - 2) + 1;

                    array[index].Position += new vec2(0, 20);
                }
            });
        }
Пример #2
0
        public override void Initialize(Scene scene, JToken data)
        {
            // TODO: Pull dimensions from the model.
            const float Width  = 2.5f;
            const float Height = 0.35f;
            const float Depth  = 11.37f;

            var p1     = Utilities.ParseVec3(data["P1"].Value <string>());
            var p2     = Utilities.ParseVec3(data["P2"].Value <string>());
            var count  = data["SegmentCount"].Value <int>();
            var length = data["SegmentLength"].Value <float>();

            Debug.Assert(count > 0, "Segment count must be positive.");
            Debug.Assert(length > 0, "Segment length must be positive..");

            var points = new vec2[count + 1];

            // This value effectively stretches points beyond their target length, which results in a stiffer bridge.
            float scale      = Utilities.Distance(p2, p1) / count / length;
            float yIncrement = (p2.y - p1.y) / count;

            for (int i = 0; i < points.Length; i++)
            {
                points[i] = new vec2(length * i * scale, -yIncrement * i);
            }

            position = p1;
            rope     = new VerletRope2D(points, length, Damping, PhysicsConstants.Gravity);

            var angle = Utilities.Angle(p1.swizzle.xz, p2.swizzle.xz);

            flatOrientation = quat.FromAxisAngle(angle, vec3.UnitY);

            // Since rope endpoints are fixed, planks are only created for non-endpoints.
            bodies = new RigidBody[count - 1];
            models = new Model[bodies.Length];

            var world = scene.World;
            var shape = new BoxShape(Width, Height, Depth);

            shape.Tag = new Rectangle(Width, Depth);

            for (int i = 0; i < bodies.Length; i++)
            {
                bodies[i] = CreateBody(scene, shape, RigidBodyTypes.PseudoStatic, RigidBodyFlags.None, false, false);

                var model = CreateModel(scene, "Cube.obj", false);
                model.Scale = new vec3(Width, Height, Depth);
                models[i]   = model;
            }

            // Since rope bridges don't have a controlling body, the pre-step callback must be attached to the world
            // instead.
            world.Events.PreStep += PreStep;

            base.Initialize(scene, data);
        }