Exemple #1
0
        /**
         * <summary>Applies a drag force on the object, based on the movement of the cursor.</summary>
         * <param name = "force">The force vector to apply</param>
         * <param name = "mousePosition">The position of the mouse</param>
         * <param name = "_distanceToCamera">The distance between the object's centre and the camera</param>
         */
        public override void ApplyDragForce(Vector3 force, Vector3 mousePosition, float _distanceToCamera)
        {
            distanceToCamera = _distanceToCamera;

            // Scale force
            force *= speedFactor * distanceToCamera / 50f;

            // Limit magnitude
            if (force.magnitude > maxSpeed)
            {
                force *= maxSpeed / force.magnitude;
            }

            if (dragMode == DragMode.LockToTrack)
            {
                if (track != null)
                {
                    switch (track.dragMovementCalculation)
                    {
                    case DragMovementCalculation.DragVector:
                        track.ApplyDragForce(force, this);
                        break;

                    case DragMovementCalculation.CursorPosition:
                        float mousePositionAlong = track.GetScreenPointProportionAlong(mousePosition);
                        float totalPositionAlong = mousePositionAlong + screenToWorldOffset;

                        if (track.preventEndToEndJumping)
                        {
                            bool inDeadZone = (totalPositionAlong >= 1f || totalPositionAlong <= 0f);
                            if (endLocked)
                            {
                                if (!inDeadZone)
                                {
                                    endLocked = false;
                                }
                            }
                            else
                            {
                                if (inDeadZone)
                                {
                                    endLocked = true;
                                }
                            }

                            if (track.Loops || !endLocked)
                            {
                                lastFrameTotalCursorPositionAlong = totalPositionAlong;
                            }
                            else
                            {
                                totalPositionAlong = lastFrameTotalCursorPositionAlong;
                            }
                        }

                        track.ApplyAutoForce(totalPositionAlong, speedFactor / 10f, this);
                        break;
                    }
                }
            }
            else
            {
                Vector3 newRot = Vector3.Cross(force, KickStarter.CameraMain.transform.forward);

                if (dragMode == DragMode.MoveAlongPlane)
                {
                    if (alignMovement == AlignDragMovement.AlignToPlane)
                    {
                        if (plane)
                        {
                            _rigidbody.AddForceAtPosition(Vector3.Cross(newRot, plane.up), transform.position + (plane.up * grabDistance));
                        }
                        else
                        {
                            ACDebug.LogWarning("No alignment plane assigned to " + this.name, this);
                        }
                    }
                    else
                    {
                        _rigidbody.AddForceAtPosition(force, transform.position - (KickStarter.CameraMain.transform.forward * grabDistance));
                    }
                }
                else if (dragMode == DragMode.RotateOnly)
                {
                    newRot /= Mathf.Sqrt((grabPoint.position - transform.position).magnitude) * 2.4f * rotationFactor;

                    if (moveWithRigidbody)
                    {
                        _rigidbody.AddTorque(newRot);
                    }
                    else
                    {
                        //transform.Rotate (newRot, Space.World);
                        thisFrameTorque = newRot;
                    }

                    if (allowZooming)
                    {
                        UpdateZoom();
                    }
                }
            }
        }
Exemple #2
0
        public override void ApplyDragForce(Vector3 force, Vector3 mousePosition, float _distanceToCamera)
        {
            distanceToCamera = _distanceToCamera;

            // Scale force
            force *= speedFactor * distanceToCamera / 50f;

            // Limit magnitude
            if (force.magnitude > maxSpeed)
            {
                force *= maxSpeed / force.magnitude;
            }

            switch (dragMode)
            {
            case DragMode.LockToTrack:
                if (track)
                {
                    switch (track.dragMovementCalculation)
                    {
                    case DragMovementCalculation.DragVector:
                        if (!UsesRigidbody)
                        {
                            force = Vector3.Slerp(lastFrameForce, force, Time.deltaTime * 10f);
                        }

                        track.ApplyDragForce(force, this);
                        lastFrameForce = force;
                        if (!UsesRigidbody)
                        {
                            RunInteraction(true);
                        }
                        break;

                    case DragMovementCalculation.CursorPosition:
                        float mousePositionAlong = track.GetScreenPointProportionAlong(mousePosition, grabPositionRelative, this);

                        float totalPositionAlong = mousePositionAlong + screenToWorldOffset;

                        if (track.preventEndToEndJumping)
                        {
                            bool inDeadZone = (totalPositionAlong >= 1f || totalPositionAlong <= 0f);
                            if (endLocked)
                            {
                                if (!inDeadZone)
                                {
                                    endLocked = false;
                                }
                            }
                            else
                            {
                                if (inDeadZone)
                                {
                                    endLocked = true;
                                }
                            }

                            if (track.Loops || !endLocked)
                            {
                                lastFrameTotalCursorPositionAlong = totalPositionAlong;
                            }
                            else
                            {
                                totalPositionAlong = lastFrameTotalCursorPositionAlong;
                            }
                        }

                        switch (dragTrackDirection)
                        {
                        case DragTrackDirection.ForwardOnly:
                            if (totalPositionAlong < GetPositionAlong())
                            {
                                return;
                            }
                            break;

                        case DragTrackDirection.BackwardOnly:
                            if (totalPositionAlong > GetPositionAlong())
                            {
                                return;
                            }
                            break;

                        default:
                            break;
                        }

                        RunInteraction(true);
                        lastFrameTotalPositionAlong = totalPositionAlong;
                        track.ApplyAutoForce(totalPositionAlong, speedFactor * 0.02f / simulatedMass, this, false);
                        break;
                    }
                }
                break;

            case DragMode.MoveAlongPlane:
            {
                Vector3 newRot = Vector3.Cross(force, KickStarter.CameraMainTransform.forward);
                if (alignMovement == AlignDragMovement.AlignToPlane)
                {
                    if (plane)
                    {
                        _rigidbody.AddForceAtPosition(Vector3.Cross(newRot, plane.up), Transform.position + (plane.up * grabDistance));
                    }
                    else
                    {
                        ACDebug.LogWarning("No alignment plane assigned to " + this.name, this);
                    }
                }
                else
                {
                    _rigidbody.AddForceAtPosition(force, Transform.position - (KickStarter.CameraMainTransform.forward * grabDistance));
                }
            }
            break;

            case DragMode.RotateOnly:
            {
                Vector3 newRot = Vector3.Cross(force, KickStarter.CameraMainTransform.forward);
                newRot /= Mathf.Sqrt((grabPoint.position - Transform.position).magnitude) * 2.4f * rotationFactor;

                if (UsesRigidbody)
                {
                    _rigidbody.AddTorque(newRot);
                }
                else
                {
                    Vector3 rawTorque = newRot;
                    thisFrameTorque = torqueDampingLerp.Update(thisFrameTorque, rawTorque, toruqeDamping);
                }

                if (allowZooming)
                {
                    UpdateZoom();
                }
            }
            break;
            }
        }