コード例 #1
0
        bool ResetToInitialPosition(RigidBodyComponent rigidBody)
        {
            ObjectPrivate objectPrivate = ScenePrivate.FindObject(rigidBody.ComponentId.ObjectId);

            if (objectPrivate != null && objectPrivate.IsValid)
            {
                try
                {
                    var motionType = rigidBody.GetMotionType();
                    WaitFor(rigidBody.SetMotionType, RigidBodyMotionType.MotionTypeKeyframed);
                    rigidBody.SetAngularVelocity(Vector.Zero);
                    rigidBody.SetLinearVelocity(Vector.Zero);
                    rigidBody.SetOrientation(objectPrivate.InitialRotation);
                    rigidBody.SetPosition(objectPrivate.InitialPosition);
                    rigidBody.SetMotionType(motionType);
                    return(true);
                }
                catch
                {
                    Log.Write(LogLevel.Error, __SimpleTag, "Position Reset: error resetting object position.");
                }
            }

            return(false);
        }
コード例 #2
0
        void ApplyTranslation(Vector startPosition, Vector targetPosition, float speed, ref bool isComplete)
        {
            Vector totalOffset = targetPosition - startPosition;

            if (totalOffset.Length() <= precision)
            {
                isComplete = true;
                return;
            }
            Vector moveDirection = totalOffset.Normalized();

            Vector currentOffset = targetPosition - GetPositionOfCOM();

            if (currentOffset.Length() < precision)
            {
                RigidBody.SetLinearVelocity(Vector.Zero);
                isComplete = true;
                return;
            }

            float distanceToTarget = moveDirection.Dot(ref currentOffset);

            if (distanceToTarget < 0) // overshot
            {
                RigidBody.SetLinearVelocity(Vector.Zero);
                isComplete = true;
                return;
            }

            if (EaseInOut > 0 && MoveDuration > 4f * timestep)
            {
                float speedMod = SpeedCurve(distanceToTarget, (movedPosition - returnPosition).Length());
                speed = speed * speedMod;
            }

            if (distanceToTarget < speed * timestep) // slow down if we think we will overshoot next timestep
            {
                speed = distanceToTarget / timestep;
            }

            Vector velocity = speed * moveDirection;

            RigidBody.SetLinearVelocity(velocity);
        }
コード例 #3
0
        void ResetToInitialPosition()
        {
            var motionType = RigidBody.GetMotionType();

            WaitFor(RigidBody.SetMotionType, RigidBodyMotionType.MotionTypeKeyframed);
            RigidBody.SetAngularVelocity(Vector.Zero);
            RigidBody.SetLinearVelocity(Vector.Zero);
            RigidBody.SetOrientation(ObjectPrivate.InitialRotation);
            RigidBody.SetPosition(ObjectPrivate.InitialPosition);
            RigidBody.SetMotionType(motionType);
        }
コード例 #4
0
    void ResetAfterTimeout()
    {
        Wait(ResetTimeout);

        _rb.SetMotionType(RigidBodyMotionType.MotionTypeKeyframed);
        _rb.SetAngularVelocity(Vector.Zero);
        _rb.SetLinearVelocity(Vector.Zero);
        _rb.SetPosition(ObjectPrivate.InitialPosition);
        _rb.SetOrientation(ObjectPrivate.InitialRotation);
        _rb.SetMotionType(RigidBodyMotionType.MotionTypeDynamic);
    }
コード例 #5
0
 private void UpdateLoop()
 {
     while (true)
     {
         //this is only one object, so we have it hard coded in to only follow the first agent., you could split this up later
         Boolean got1 = false;
         foreach (AgentPrivate agent in ScenePrivate.GetAgents())
         {
             if (got1 == false)
             {
                 ObjectPrivate      agentObejct = ScenePrivate.FindObject(agent.AgentInfo.ObjectId);
                 AnimationComponent anim;
                 if (agentObejct.TryGetFirstComponent(out anim))
                 {
                     Sansar.Vector fwd = anim.GetVectorAnimationVariable("LLCameraForward");
                     //Builds a rotation from the fwd vector
                     Quaternion newRot = Quaternion.FromLook(fwd, Sansar.Vector.Up);
                     //This is a basic check to make sure the camera rotation isnt all 0s
                     if (fwd.LengthSquared() > 0)
                     {
                         try
                         {
                             RigidBody.SetAngularVelocity(Vector.Zero);
                             RigidBody.SetLinearVelocity(Vector.Zero);
                             RigidBody.SetPosition(agentObejct.Position + lightPositionOffset);
                             //Order of multiplication matters here I think, start with the base rotation,
                             //multiply by and base offset rotation for the light, then multiply by the rotation of the fwd
                             //Keep in mind that multiplying quad A by quad b will rotate quad A by quad b
                             RigidBody.SetOrientation(QuaternionToVector(startRot * lightOnObjectDirection * newRot).Normalized());
                         }
                         catch
                         {
                         }
                     }
                 }
                 got1 = true;
             }
         }
         Wait(TimeSpan.FromSeconds(.05));
     }
 }
コード例 #6
0
 void OnDrop(HeldObjectData data)
 {
     try {
         unsubscribe?.Invoke();
         if (ResetPositionOnDrop)
         {
             RigidBody.SetPosition(OriginalPosition);
             RigidBody.SetOrientation(OriginalOrientation);
             RigidBody.SetLinearVelocity(Sansar.Vector.Zero);
             RigidBody.SetAngularVelocity(Sansar.Vector.Zero);
         }
     }
     catch (NullReferenceException nre) { if (DebugSpam)
                                          {
                                              Log.Write("OnDrop", nre.Message);
                                          }
     }                                                                                       // ignore exceptions for not found agents.
     catch (Exception e) { if (DebugSpam)
                           {
                               Log.Write("OnDrop", e.ToString());
                           }
     }
 }
コード例 #7
0
    private void UpdateLoop()
    {
        float Tick      = 0.01f;
        float AngleStep = 5 * OpenAngle * Tick / SecondsToOpen;

        TimeSpan Moment = TimeSpan.FromSeconds(Tick);

        float Angle = 0.0f;

        while (true)
        {
            Wait(Moment);

            // If we're in the process of opening or closing, we'll adjust the angle a little toward the target
            if (Opening)
            {
                if (OpenOut)
                {
                    Angle -= AngleStep;
                    if (Angle < -OpenAngle)
                    {
                        Angle = -OpenAngle;
                    }
                }
                else
                {
                    Angle += AngleStep;
                    if (Angle > OpenAngle)
                    {
                        Angle = OpenAngle;
                    }
                }

                // If it's been a while since we've heard an open signal, let's start closing the door
                if (DateTime.Now.Subtract(LatestOpenSignal).TotalSeconds >= SecondsBeforeClose)
                {
                    Opening = false;
                    if (CloseSound != null)
                    {
                        ScenePrivate.PlaySound(CloseSound, PlaySettings.PlayOnce);
                    }
                }
            }
            else
            {
                if (OpenOut)
                {
                    Angle += AngleStep;
                    if (Angle > 0f)
                    {
                        Angle = 0f;
                    }
                }
                else
                {
                    Angle -= AngleStep;
                    if (Angle < 0f)
                    {
                        Angle = 0f;
                    }
                }
            }

            // Compute the new position and rotation of the door
            Quaternion Rotation = Quaternion.FromEulerAngles(new Vector(0, 0, Angle, 0));
            Quaternion NewOri   = ClosedOrientation * Rotation;
            Vector     NewPos   = ClosedPosition - HingeOffset.Rotate(ref Rotation);

            // Update the door's position and orientation
            DoorBody.SetAngularVelocity(Vector.Zero);
            DoorBody.SetLinearVelocity(Vector.Zero);
            DoorBody.SetPosition(NewPos);
            DoorBody.SetOrientation(QuaternionToVector(NewOri));
        }
    }