예제 #1
0
        public void AddJoint(PhysicsJointMain joint)
        {
            string bodyOne              = joint.BodyOne;
            string bodyTwo              = joint.BodyTwo;
            int    collisionGroup       = joint.CollisionGroup;
            bool   isAngleSpringEnabled = joint.AngleSpringEnabled;
            float  springConstant       = joint.AngleSpringConstant;
            float  dampeningConstant    = joint.AngleSpringDampningConstant;
            float  angleLowerLimit      = (float)joint.AngleLowerLimit;
            float  angleUpperLimit      = (float)joint.AngleUpperLimit;


            Point   center            = joint.GetCenter();
            Vector2 ptCollisionCenter = new Vector2((float)center.X, (float)center.Y);


            if (!PhysicsObjects.ContainsKey(bodyOne))
            {
                throw new Exception("Cannot add joint for an invalid BodyOne value of '" + bodyOne + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?");
            }
            if (!PhysicsObjects.ContainsKey(bodyTwo))
            {
                throw new Exception("Cannot add joint for an invalid BodyTwo value of '" + bodyTwo + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?");
            }

            Body body1 = PhysicsObjects[bodyOne].BodyObject;
            Body body2 = PhysicsObjects[bodyTwo].BodyObject;

            Geom          geom1         = PhysicsObjects[bodyOne].GeometryObject;
            Geom          geom2         = PhysicsObjects[bodyTwo].GeometryObject;
            RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter);

            if (isAngleSpringEnabled)
            {
                AngleSpring angleSpring = new AngleSpring(body1, body2, springConstant, dampeningConstant);
                Simulator.Add(angleSpring);
            }

            if (angleUpperLimit != -1 && angleLowerLimit != -1)
            {
                float           upperAngle      = (float)DegreesToRadians(angleUpperLimit);
                float           lowerAngle      = (float)DegreesToRadians(angleLowerLimit);
                AngleLimitJoint angleLimitJoint = new AngleLimitJoint(body1, body2, lowerAngle, upperAngle);
                Simulator.Add(angleLimitJoint);
            }

            if (collisionGroup > 0)
            {
                geom1.CollisionGroup = collisionGroup;
                geom2.CollisionGroup = collisionGroup;
            }

            // get rid of the UI representation of the joint
            joint.VisualElement.Visibility = Visibility.Collapsed;
        }
예제 #2
0
        /// <summary>
        /// Adds a single joint objectt from a Canvas into the simulation.
        /// </summary>
        /// <param name="cnvContainer">The Canvas to add joints from</param>
        public void AddJoint(PhysicsJoint joint)
        {
            if (!PhysicsObjects.ContainsKey(joint.BodyOne))
            {
                throw new Exception("A PhysicsJoint exists with an invalid BodyOne value of '" + joint.BodyOne + "'.");
            }
            if (!PhysicsObjects.ContainsKey(joint.BodyTwo))
            {
                throw new Exception("A PhysicsJoint exists with an invalid BodyTwo value of '" + joint.BodyTwo + "'.");
            }


            Body body1 = PhysicsObjects[joint.BodyOne].BodyObject;
            Body body2 = PhysicsObjects[joint.BodyTwo].BodyObject;

            Geom geom1 = PhysicsObjects[joint.BodyOne].GeometryObject;
            Geom geom2 = PhysicsObjects[joint.BodyTwo].GeometryObject;


            Vector2 ptCollisionCenter = new Vector2((float)(joint.GetCenter().X), (float)(joint.GetCenter().Y));

            RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter);

            joint.RevoluteJointObject = revoluteJoint;

            if (joint.AngleSpringEnabled)
            {
                float       springConstant    = joint.AngleSpringConstant;
                float       dampeningConstant = joint.AngleSpringDampningConstant;
                AngleSpring angleSpring       = new AngleSpring(body1, body2, springConstant, dampeningConstant);
                Simulator.Add(angleSpring);
            }

            if (joint.CollisionGroup > 0)
            {
                geom1.CollisionGroup = joint.CollisionGroup;
                geom2.CollisionGroup = joint.CollisionGroup;
            }

            joint.Visibility = Visibility.Collapsed;
        }
예제 #3
0
        public void AddFluidContainer(FluidContainerMain fluidContainerMain)
        {
            double x      = Convert.ToDouble(fluidContainerMain.VisualElement.GetValue(Canvas.LeftProperty));
            double y      = Convert.ToDouble(fluidContainerMain.VisualElement.GetValue(Canvas.TopProperty));
            float  width  = (float)fluidContainerMain.VisualElement.ActualWidth;
            float  height = (float)fluidContainerMain.VisualElement.ActualHeight;

            WaveController waveController = fluidContainerMain.WaveControllerObject;

            waveController.Position           = new Vector2((float)x, (float)y);
            waveController.Width              = width;
            waveController.Height             = height;
            waveController.NodeCount          = fluidContainerMain.NodeCount;
            waveController.DampingCoefficient = (float)fluidContainerMain.DampingCoefficient;
            waveController.Frequency          = (float)fluidContainerMain.Frequency;

            waveController.WaveGeneratorMax  = (float)fluidContainerMain.WaveGeneratorMax;
            waveController.WaveGeneratorMin  = (float)fluidContainerMain.WaveGeneratorMin;
            waveController.WaveGeneratorStep = (float)fluidContainerMain.WaveGeneratorStep;

            waveController.Initialize();

            Vector2 vecTopLeft     = new Vector2((float)x, (float)y);
            Vector2 vecBottomRight = new Vector2((float)x + width, (float)y + height);

            AABB waterAABB = new AABB(vecTopLeft, vecBottomRight);
            AABBFluidContainer  waterContainer      = new AABBFluidContainer(waterAABB);
            FluidDragController fluidDragController = new FluidDragController();

            foreach (KeyValuePair <string, PhysicsSprite> item in PhysicsObjects)
            {
                PhysicsSprite sprite = item.Value;
                fluidDragController.AddGeom(sprite.GeometryObject);
            }

            fluidDragController.Initialize(waterContainer, (float)fluidContainerMain.FluidDensity, (float)fluidContainerMain.LinearDragCoefficient, (float)fluidContainerMain.RotationalDragCoefficient, new Vector2((float)fluidContainerMain.GravityHorizontal, (float)fluidContainerMain.GravityVertical));
            Simulator.Add(fluidDragController);
        }