protected override void OnTransformChanged()
        {
            if (rigidBody != null && !updatePropertiesWithoutUpdatingBody)
            {
                var bodyTransform = Transform.Value;

                if (rigidBodyCreatedTransformScale != bodyTransform.Scale)
                {
                    RecreateBody();
                }
                else
                {
                    //update transform
                    rigidBody.Position = Physics2DUtility.Convert(bodyTransform.Position.ToVector2());
                    rigidBody.Rotation = -(float)MathEx.DegreeToRadian(bodyTransform.Rotation.ToAngles().Yaw);

                    //update constraints
                    if (rigidBody.JointList != null)
                    {
                        foreach (var c in GetLinkedCreatedConstraints())
                        {
                            c.RecreateConstraint();
                        }
                    }
                }
            }

            base.OnTransformChanged();
        }
        protected override Joint OnCreateConstraint(Component_PhysicalBody2D creationBodyA, Component_PhysicalBody2D creationBodyB)
        {
            var anchor = Physics2DUtility.Convert(TransformV.Position.ToVector2());

            var angle = -MathEx.DegreeToRadian(TransformV.Rotation.ToAngles().Yaw);
            var axis  = Physics2DUtility.Convert(new Vector2(Math.Cos(angle), Math.Sin(angle)));

            return(new PrismaticJoint(creationBodyA.Physics2DBody, creationBodyB.Physics2DBody, anchor, anchor, axis, true));
        }
Пример #3
0
        /// <summary>
        /// Converts the current instance of <see cref="AnglesF"/> into the equivalent <see cref="QuaternionF"/> structure.
        /// </summary>
        /// <returns>The equivalent <see cref="QuaternionF"/> structure.</returns>
        public QuaternionF ToQuaternion()
        {
            float a;

            a = MathEx.DegreeToRadian(Yaw) * .5f;
            float sz = MathEx.Sin(a);
            float cz = MathEx.Cos(a);

            a = MathEx.DegreeToRadian(Pitch) * .5f;
            float sy = MathEx.Sin(a);
            float cy = MathEx.Cos(a);

            a = MathEx.DegreeToRadian(Roll) * .5f;
            float sx = MathEx.Sin(a);
            float cx = MathEx.Cos(a);

            float sxcy = sx * cy;
            float cxcy = cx * cy;
            float sxsy = sx * sy;
            float cxsy = cx * sy;

            return(new QuaternionF(cxsy * sz - sxcy * cz, -cxsy * cz - sxcy * sz, sxsy * cz - cxcy * sz, cxcy * cz + sxsy * sz));
        }
Пример #4
0
        /// <summary>
        /// Converts the current instance of <see cref="Angles"/> into the equivalent <see cref="Quaternion"/> structure.
        /// </summary>
        /// <returns>The equivalent <see cref="Quaternion"/> structure.</returns>
        public Quaternion ToQuaternion()
        {
            double a;

            a = MathEx.DegreeToRadian(Yaw) * .5;
            double sz = Math.Sin(a);
            double cz = Math.Cos(a);

            a = MathEx.DegreeToRadian(Pitch) * .5;
            double sy = Math.Sin(a);
            double cy = Math.Cos(a);

            a = MathEx.DegreeToRadian(Roll) * .5;
            double sx = Math.Sin(a);
            double cx = Math.Cos(a);

            double sxcy = sx * cy;
            double cxcy = cx * cy;
            double sxsy = sx * sy;
            double cxsy = cx * sy;

            return(new Quaternion(cxsy * sz - sxcy * cz, -cxsy * cz - sxcy * sz, sxsy * cz - cxcy * sz, cxcy * cz + sxsy * sz));
        }
Пример #5
0
        public virtual Viewport.CameraSettingsClass GetCameraSettings(Viewport viewport, Component_Camera cameraDefault)
        {
            Viewport.CameraSettingsClass result = null;
            GetCameraSettingsEvent?.Invoke(this, viewport, cameraDefault, ref result);

            if (result == null)
            {
                if (FreeCamera)
                {
                    //free camera

                    result = new Viewport.CameraSettingsClass(viewport, cameraDefault.AspectRatio, cameraDefault.FieldOfView, cameraDefault.NearClipPlane, cameraDefault.FarClipPlane, freeCameraPosition, freeCameraDirection.GetVector(), Vector3.ZAxis, ProjectionType.Perspective, 1, cameraDefault.Exposure, cameraDefault.EmissiveFactor);
                }
                else if (UseBuiltInCamera.Value == BuiltInCameraEnum.FirstPerson)
                {
                    //first person camera

                    //Character
                    var character = ObjectControlledByPlayer.Value as Component_Character;
                    if (character != null)
                    {
                        character.GetFirstPersonCameraPosition(out var position, out var forward, out var up);

                        var eyePosition = character.TransformV * character.EyePosition.Value;
                        var direction   = character.LookToDirection.GetVector();

                        result = new Viewport.CameraSettingsClass(viewport, cameraDefault.AspectRatio, cameraDefault.FieldOfView, cameraDefault.NearClipPlane, cameraDefault.FarClipPlane, eyePosition, direction, up, ProjectionType.Perspective, 1, cameraDefault.Exposure, cameraDefault.EmissiveFactor);
                    }
                }
                else if (UseBuiltInCamera.Value == BuiltInCameraEnum.ThirdPerson)
                {
                    //third person camera

                    //Character
                    if (Scene.Mode.Value == Component_Scene.ModeEnum._3D)
                    {
                        var character = ObjectControlledByPlayer.Value as Component_Character;
                        if (character != null)
                        {
                            var lookAt = character.TransformV.Position;

                            var d         = new SphericalDirection(MathEx.DegreeToRadian(ThirdPersonCameraHorizontalAngle.Value), MathEx.DegreeToRadian(ThirdPersonCameraVerticalAngle.Value));
                            var direction = -d.GetVector();

                            var from = lookAt - direction * ThirdPersonCameraDistance.Value;

                            result = new Viewport.CameraSettingsClass(viewport, cameraDefault.AspectRatio, cameraDefault.FieldOfView, cameraDefault.NearClipPlane, cameraDefault.FarClipPlane, from, direction, Vector3.ZAxis, ProjectionType.Perspective, 1, cameraDefault.Exposure, cameraDefault.EmissiveFactor);
                        }
                    }

                    //Character2D
                    if (Scene.Mode.Value == Component_Scene.ModeEnum._2D)
                    {
                        var character = ObjectControlledByPlayer.Value as Component_Character2D;
                        if (character != null)
                        {
                            var lookAt = character.TransformV.Position;
                            var from   = lookAt + new Vector3(0, 0, 10);

                            result = new Viewport.CameraSettingsClass(viewport, cameraDefault.AspectRatio, cameraDefault.FieldOfView, cameraDefault.NearClipPlane, cameraDefault.FarClipPlane, from, -Vector3.ZAxis, Vector3.YAxis, ProjectionType.Orthographic, cameraDefault.Height, cameraDefault.Exposure, cameraDefault.EmissiveFactor);
                        }
                    }
                }
            }

            //Component_CameraManagement
            if (result == null)
            {
                var m = GetCurrentCameraManagement();
                if (m != null)
                {
                    result = m.GetCameraSettings(this, viewport, cameraDefault);
                }
            }

            return(result);
        }
        void CreateBody()
        {
            if (!CanCreate)
            {
                return;
            }

            duringCreateDestroy = true;

            var physicsWorldData = GetPhysicsWorldData(true);

            if (physicsWorldData != null)
            {
                if (rigidBody != null)
                {
                    Log.Fatal("Component_RigidBody2D: CreateBody: rigidBody != null.");
                }
                if (!EnabledInHierarchy)
                {
                    Log.Fatal("Component_RigidBody2D: CreateBody: !EnabledInHierarchy.");
                }

                var bodyTransform      = Transform.Value;
                var bodyTransformScale = new Transform(Vector3.Zero, Quaternion.Identity, bodyTransform.Scale);

                //get shapes. calculate local transforms with applied body scaling.
                var componentShapes = new List <Tuple <Component_CollisionShape2D, Transform> >();
                foreach (var child in GetComponents <Component_CollisionShape2D>(false, false, true))
                {
                    GetComponentShapesRecursive(child, bodyTransformScale * child.TransformRelativeToParent.Value, componentShapes);
                }

                if (componentShapes.Count > 0)
                {
                    //use local variable to prevent double update inside properties.
                    Body body = new Body();

                    foreach (var shapeItem in componentShapes)
                    {
                        var shape    = shapeItem.Item1;
                        var fixtures = shape.CreateShape(body, shapeItem.Item2, rigidBodyLocalPoints);
                        if (fixtures != null)
                        {
                            foreach (var fixture in fixtures)
                            {
                                fixture.Tag                 = shape;
                                fixture.Friction            = (float)shape.Friction;
                                fixture.Restitution         = (float)shape.Restitution;
                                fixture.CollisionCategories = shape.CollisionCategories;
                                fixture.CollidesWith        = shape.CollidesWith;
                                fixture.CollisionGroup      = (short)shape.CollisionGroup;
                            }
                        }
                    }

                    if (body.FixtureList.Count != 0)
                    {
                        body.Position      = Physics2DUtility.Convert(bodyTransform.Position.ToVector2());
                        body.Rotation      = -(float)MathEx.DegreeToRadian(bodyTransform.Rotation.ToAngles().Yaw);
                        body.FixedRotation = FixedRotation;

                        switch (MotionType.Value)
                        {
                        case MotionTypeEnum.Static: body.BodyType = BodyType.Static; break;

                        case MotionTypeEnum.Dynamic: body.BodyType = BodyType.Dynamic; break;

                        case MotionTypeEnum.Kinematic: body.BodyType = BodyType.Kinematic; break;
                        }

                        body.IsBullet = CCD;

                        if (MotionType.Value == MotionTypeEnum.Dynamic)
                        {
                            UpdateMassAndInertia(body);

                            body.SleepingAllowed = AllowSleep;
                            body.LocalCenter     = Physics2DUtility.Convert(LocalCenter);

                            body.LinearDamping   = (float)LinearDamping;
                            body.AngularDamping  = (float)AngularDamping;
                            body.LinearVelocity  = Physics2DUtility.Convert(LinearVelocity);
                            body.AngularVelocity = (float)MathEx.DegreeToRadian(AngularVelocity);
                            body.IgnoreGravity   = !EnableGravity;
                        }

                        rigidBody     = body;
                        rigidBody.Tag = this;

                        physicsWorldData.world.Add(rigidBody);
                        rigidBodyCreatedTransformScale = bodyTransform.Scale;
                    }
                }

                SpaceBoundsUpdate();
            }

            duringCreateDestroy = false;
        }
Пример #7
0
 public Radian InRadians()
 {
     return(MathEx.DegreeToRadian(value));
 }