Browse Source

- MoveTo and Rotate to now both use a "speed" parameter rather than a "time" parameter. This always caused people confusion and is used as speed by many.
- Modified the toys that use this including adding a "trackMouse" option to both "MoveTo" and "RotateTo" toys.
- The DeathBallToy needs looking at, it's not really optimized for T2D MIT yet.

MelvMay-GG 12 years ago
parent
commit
a3dc8b8

+ 29 - 14
engine/source/2d/sceneobject/SceneObject.cc

@@ -1634,19 +1634,26 @@ void SceneObject::onEndCollision( const TickContact& tickContact )
 
 //-----------------------------------------------------------------------------
 
-bool SceneObject::moveTo( const Vector2& targetWorldPoint, const U32 time, const bool autoStop, const bool warpToTarget )
+bool SceneObject::moveTo( const Vector2& targetWorldPoint, const F32 speed, const bool autoStop, const bool warpToTarget )
 {
     // Check in a scene.
     if ( !getScene() )
     {
-        Con::warnf("Cannot move object (%d) to a point as it is not in a scene.", getId() );
+        Con::warnf("SceneObject::moveTo() - Cannot move object (%d) to a point as it is not in a scene.", getId() );
         return false;
     }
 
     // Check not a static body.
     if ( getBodyType() == b2_staticBody )
     {
-        Con::warnf("Cannot move object (%d) to a point as it is a static body.", getId() );
+        Con::warnf("SceneObject::moveTo() - Cannot move object (%d) to a point as it is a static body.", getId() );
+        return false;
+    }
+
+    // Check speed.
+    if ( speed <= 0.0f )
+    {
+        Con::warnf("SceneObject::moveTo() - Speed '%f' is invalid.", speed );
         return false;
     }
 
@@ -1657,11 +1664,12 @@ bool SceneObject::moveTo( const Vector2& targetWorldPoint, const U32 time, const
         mMoveToEventId = 0;
     }
 
-    // Calculate relative position.
-    const Vector2 relativePosition = targetWorldPoint - getPosition();
+    // Calculate the linear velocity for the specified speed.
+    Vector2 linearVelocity = targetWorldPoint - getPosition();
+    const F32 distance = linearVelocity.Normalize( speed );
 
-    // Calculate linear velocity to use over time.
-    const Vector2 linearVelocity = relativePosition / (time * 0.001f);
+    // Calculate the time it will take to reach the target.
+    const U32 time = (U32)((distance / speed) * 1000.0f);
 
     // Set the linear velocity.
     setLinearVelocity( linearVelocity );
@@ -1675,19 +1683,26 @@ bool SceneObject::moveTo( const Vector2& targetWorldPoint, const U32 time, const
 
 //-----------------------------------------------------------------------------
 
-bool SceneObject::rotateTo( const F32 targetAngle, const U32 time, const bool autoStop, const bool warpToTarget )
+bool SceneObject::rotateTo( const F32 targetAngle, const F32 speed, const bool autoStop, const bool warpToTarget )
 {
     // Check in a scene.
     if ( !getScene() )
     {
-        Con::warnf("Cannot rotate object (%d) to an angle as it is not in a scene.", getId() );
+        Con::warnf("SceneObject::rotateTo() - Cannot rotate object (%d) to an angle as it is not in a scene.", getId() );
         return false;
     }
 
     // Check not a static body.
     if ( getBodyType() == b2_staticBody )
     {
-        Con::warnf("Cannot move object (%d) to an angle as it is a static body.", getId() );
+        Con::warnf("SceneObject::rotateTo() - Cannot move object (%d) to an angle as it is a static body.", getId() );
+        return false;
+    }
+
+    // Check speed.
+    if ( speed <= 0.0f )
+    {
+        Con::warnf("SceneObject::rotateTo() - Speed '%f' is invalid.", speed );
         return false;
     }
 
@@ -1704,11 +1719,11 @@ bool SceneObject::rotateTo( const F32 targetAngle, const U32 time, const bool au
     // Calculate delta angle.
     const F32 deltaAngle = mAtan( mSin( relativeAngle ), mCos( relativeAngle ) );
 
-    // Calculate angular velocity over time.
-    const F32 angularVelocity = deltaAngle / (time * 0.001f);
-
     // Set angular velocity.
-    setAngularVelocity( angularVelocity );
+    setAngularVelocity( deltaAngle > 0.0f ? speed : -speed );
+
+    // Calculate the time it will take to reach the angle.
+    const U32 time = (U32)(mFabs(deltaAngle / speed) * 1000.0f);
 
     // Create and post event.
     SceneObjectRotateToEvent* pEvent = new SceneObjectRotateToEvent( targetAngle, autoStop, warpToTarget );

+ 2 - 2
engine/source/2d/sceneobject/SceneObject.h

@@ -394,8 +394,8 @@ public:
     inline F32              getAngularDamping(void) const               { if ( mpScene ) return mpBody->GetAngularDamping(); else return mBodyDefinition.angularDamping; }
 
     /// Move/Rotate to.
-    bool                    moveTo( const Vector2& targetWorldPoint, const U32 time = 1000, const bool autoStop = true, const bool warpToTarget = true );
-    bool                    rotateTo( const F32 targetAngle, const U32 time = 1000, const bool autoStop = true, const bool warpToTarget = true );
+    bool                    moveTo( const Vector2& targetWorldPoint, const F32 speed, const bool autoStop = true, const bool warpToTarget = true );
+    bool                    rotateTo( const F32 targetAngle, const F32 speed, const bool autoStop = true, const bool warpToTarget = true );
     void                    cancelMoveTo( const bool autoStop = true );
     void                    cancelRotateTo( const bool autoStop = true );
     inline bool             isMoveToComplete( void ) const              { return mMoveToEventId == 0; }

+ 20 - 30
engine/source/2d/sceneobject/SceneObject_ScriptBinding.h

@@ -1610,13 +1610,13 @@ ConsoleMethod(SceneObject, getAngularDamping, F32, 2, 2, "() - Gets the angular
 
 //-----------------------------------------------------------------------------
 
-ConsoleMethod(SceneObject, moveTo, bool, 3, 7,  "(worldPoint X/Y, [time = 1000], [autoStop = true], [warpToTarget = true]) - Moves the object to the specified world point.\n"
+ConsoleMethod(SceneObject, moveTo, bool, 4, 7,  "(worldPoint X/Y, time, [autoStop = true], [warpToTarget = true]) - Moves the object to the specified world point.\n"
                                                 "The point is moved by calculating the initial linear velocity required and applies it.\n"
                                                 "The object may never reach the point if it has linear damping applied or collides with another object.\n"
-                                                "@param worldPoint/Y - The world point to move the object to.\n"
-                                                "@param time - The time (milliseconds) taken to move the object to the specified point."
-                                                "@param autoStop? - Whether to automatically set the linear velocity to zero when time has elapsed or not\n"
-                                                "@param warpToTarget? - Whether to move instantly to the target point after the specified time or not in-case the target was not quite reached.\n"
+                                                "@param worldPoint/Y The world point to move the object to.\n"
+                                                "@param speed The speed (in m/s) to use to move to the specified point."
+                                                "@param autoStop? Whether to automatically set the linear velocity to zero when time has elapsed or not\n"
+                                                "@param warpToTarget? Whether to move instantly to the target point after the specified time or not in-case the target was not quite reached.\n"
                                                 "@return Whether the move could be started or not.")
 {
     // World point.
@@ -1641,17 +1641,12 @@ ConsoleMethod(SceneObject, moveTo, bool, 3, 7,  "(worldPoint X/Y, [time = 1000],
         return false;
     }
 
-    if ( argc <= nextArg )
-    {
-        return object->moveTo( worldPoint );
-    }
-
-    // Time.
-    const U32 time = dAtoi(argv[nextArg++]);
+    // Speed.
+    const F32 speed = dAtof(argv[nextArg++]);
 
     if ( argc <= nextArg )
     {
-        return object->moveTo( worldPoint, time );
+        return object->moveTo( worldPoint, speed );
     }
 
     // Auto stop?
@@ -1659,40 +1654,35 @@ ConsoleMethod(SceneObject, moveTo, bool, 3, 7,  "(worldPoint X/Y, [time = 1000],
 
     if ( argc <= nextArg )
     {
-        return object->moveTo( worldPoint, time, autoStop );
+        return object->moveTo( worldPoint, speed, autoStop );
     }
 
     // Warp to target?
     const bool warpToTarget = dAtob(argv[nextArg++]);
 
-    return object->moveTo( worldPoint, time, autoStop, warpToTarget );
+    return object->moveTo( worldPoint, speed, autoStop, warpToTarget );
 }
 
 //-----------------------------------------------------------------------------
 
-ConsoleMethod(SceneObject, rotateTo, bool, 3, 6,    "(angle, [time = 1000], [autoStop = true], [warpToTarget = true]) - Rotates the object to the specified angle.\n"
+ConsoleMethod(SceneObject, rotateTo, bool, 4, 6,    "(angle, speed, [autoStop = true], [warpToTarget = true]) - Rotates the object to the specified angle.\n"
                                                     "The angle is rotated to by calculating the initial angular velocity required and applies it.\n"
                                                     "The object may never reach the point if it has angular damping applied or collides with another object.\n"
-                                                    "@param angle- The angle to rotate the object to.\n"
-                                                    "@param time - The time (milliseconds) taken to rotate the object to the specified angle."
-                                                    "@param autoStop? - Whether to automatically set the angular velocity to zero when time has elapsed or not\n"
-                                                    "@param warpToTarget? - Whether to rotate instantly to the target angle after the specified time or not in-case the target was not quite reached.\n"
+                                                    "@param angle The angle to rotate the object to.\n"
+                                                    "@param speed The speed (in degree/s) to use to rotate to the specified angle."
+                                                    "@param autoStop? Whether to automatically set the angular velocity to zero when time has elapsed or not\n"
+                                                    "@param warpToTarget? Whether to rotate instantly to the target angle after the specified time or not in-case the target was not quite reached.\n"
                                                     "@return Whether the rotation could be started or not.")
 {
     // Fetch angle.
     const F32 angle = mDegToRad(dAtof(argv[2]));
 
-    if ( argc == 3 )
-    {
-        return object->rotateTo( angle );
-    }
-
-    // Time.
-    const U32 time = dAtoi(argv[3]);
+    // Speed.
+    const F32 speed = mDegToRad(dAtof(argv[3]));
 
     if ( argc == 4 )
     {
-        return object->rotateTo( angle, time );
+        return object->rotateTo( angle, speed );
     }
 
     // Auto stop?
@@ -1700,13 +1690,13 @@ ConsoleMethod(SceneObject, rotateTo, bool, 3, 6,    "(angle, [time = 1000], [aut
 
     if ( argc == 5 )
     {
-        return object->rotateTo( angle, time, autoStop );
+        return object->rotateTo( angle, speed, autoStop );
     }
 
     // Warp to target.
     const bool warpToTarget = dAtob(argv[5]);
 
-    return object->rotateTo( angle, time, autoStop, warpToTarget );
+    return object->rotateTo( angle, speed, autoStop, warpToTarget );
 
 }
 

+ 4 - 4
modules/DeathBallToy/1/main.cs

@@ -38,7 +38,7 @@ function DeathBallToy::create( %this )
     DeathBallToy.WorldLeft = -50;
     DeathBallToy.WorldRight = 140;
 
-    DeathBallToy.rotateTime = 0;
+    DeathBallToy.rotateSpeed = 360;
     DeathBallToy.maxBallSpeed = 10;
     DeathBallToy.ballSpeed = 5;
     DeathBallToy.soldierSpeed = 1;
@@ -461,7 +461,7 @@ function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
     %origin = Deathball.getPosition();
     %angle = -mRadToDeg( mAtan( getWord(%worldPosition,0)-getWord(%origin,0), getWord(%worldPosition,1)-getWord(%origin,1) ) );
 
-    Deathball.RotateTo( %angle, DeathBallToy.rotateTime );
+    Deathball.RotateTo( %angle, DeathBallToy.rotateSpeed );
 
     %adjustedSpeed = DeathBallToy.ballSpeed / DeathBallToy.maxBallSpeed;
 
@@ -479,7 +479,7 @@ function SandboxWindow::onTouchUp(%this, %touchID, %worldPosition)
     %origin = Deathball.getPosition();
     %angle = -mRadToDeg( mAtan( getWord(%worldPosition,0)-getWord(%origin,0), getWord(%worldPosition,1)-getWord(%origin,1) ) );
 
-    Deathball.RotateTo( %angle, DeathBallToy.rotateTime );
+    Deathball.RotateTo( %angle, DeathBallToy.rotateSpeed );
 
     %adjustedSpeed = (DeathBallToy.ballSpeed / DeathBallToy.maxBallSpeed) * 3000;
 
@@ -493,7 +493,7 @@ function SandboxWindow::onTouchDragged(%this, %touchID, %worldPosition)
     %origin = Deathball.getPosition();
     %angle = -mRadToDeg( mAtan( getWord(%worldPosition,0)-getWord(%origin,0), getWord(%worldPosition,1)-getWord(%origin,1) ) );
 
-    Deathball.RotateTo( %angle, DeathBallToy.rotateTime );
+    Deathball.RotateTo( %angle, DeathBallToy.rotateSpeed );
 
     %adjustedSpeed = DeathBallToy.ballSpeed / DeathBallToy.maxBallSpeed;
 

+ 9 - 1
modules/DeathBallToy/1/scripts/faceObjectBehavior.cs

@@ -44,7 +44,15 @@ function FaceObjectBehavior::updateFace(%this)
     %origin = %this.owner.getPosition();
     %angle = -mRadToDeg( mAtan( getWord(%this.target.getPosition(),0)-getWord(%origin,0), getWord(%this.target.getPosition(),1)-getWord(%origin,1) ) );
     
-    %this.owner.rotateTo(%angle, %this.turnSpeed);
+    if ( %this.turnSpeed > 0.0 )
+    {
+        %this.owner.rotateTo(%angle, %this.turnSpeed);
+    }
+    else
+    {
+        %this.owner.setAngle(%angle);
+    }
+    
       
     %this.schedule(200, updateFace);
 }

+ 29 - 5
modules/MoveToToy/1/main.cs

@@ -26,10 +26,12 @@ function MoveToToy::create( %this )
     activatePackage( MoveToToyPackage );    
 
     // Initialize the toys settings.
-    MoveToToy.moveTime = 1000;
+    MoveToToy.moveSpeed = 50;
+    MoveToToy.trackMouse = true;
 
     // Add the custom controls.
-    addNumericOption("Move time", 1000, 10000, 100, "setMoveTime", MoveToToy.moveTime, true, "Sets the time it takes to move to the target position.");
+    addNumericOption("Move Speed", 1, 150, 1, "setMoveSpeed", MoveToToy.moveSpeed, true, "Sets the linear speed to use when moving to the target position.");
+    addFlagOption("Track Mouse", "setTrackMouse", MoveToToy.trackMouse, false, "Whether to track the position of the mouse or not." );
 
     // Reset the toy initially.
     MoveToToy.reset();        
@@ -148,9 +150,16 @@ function MoveToToy::createTarget( %this )
 
 //-----------------------------------------------------------------------------
 
-function MoveToToy::setMoveTime( %this, %value )
+function MoveToToy::setMoveSpeed( %this, %value )
 {
-    %this.moveTime = %value;
+    %this.moveSpeed = %value;
+}
+
+//-----------------------------------------------------------------------------
+
+function MoveToToy::setTrackMouse( %this, %value )
+{
+    %this.trackMouse = %value;
 }
 
 //-----------------------------------------------------------------------------
@@ -164,7 +173,22 @@ function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
     MoveToToy.TargetObject.Position = %worldPosition;
     
     // Move the sight to the touched position.
-    MoveToToy.SightObject.MoveTo( %worldPosition, MoveToToy.moveTime );
+    MoveToToy.SightObject.MoveTo( %worldPosition, MoveToToy.moveSpeed );
+}
+
+//-----------------------------------------------------------------------------
+
+function SandboxWindow::onTouchMoved(%this, %touchID, %worldPosition)
+{
+    // Finish if not tracking the mouse.
+    if ( !MoveToToy.trackMouse )
+        return;
+        
+    // Set the target to the touched position.
+    MoveToToy.TargetObject.Position = %worldPosition;
+    
+    // Move the sight to the touched position.
+    MoveToToy.SightObject.MoveTo( %worldPosition, MoveToToy.moveSpeed );     
 }
     
 };

+ 30 - 5
modules/RotateToToy/1/main.cs

@@ -26,10 +26,12 @@ function RotateToToy::create( %this )
     activatePackage( RotateToToyPackage );    
     
     // Initialize the toys settings.
-    RotateToToy.rotateTime = 500;
+    RotateToToy.rotateSpeed = 360;
+    RotateToToy.trackMouse = true;
 
     // Add the custom controls.
-    addNumericOption("Rotate time", 10, 10000, 10, "setRotateTime", RotateToToy.rotateTime, true, "Sets the time it takes to move to the target angle.");
+    addNumericOption("Rotate Speed", 1, 720, 1, "setRotateSpeed", RotateToToy.rotateSpeed, false, "Sets the angular speed to use to rotate to the target angle.");
+    addFlagOption("Track Mouse", "setTrackMouse", RotateToToy.trackMouse, false, "Whether to track the angle to the mouse or not." );
     
     // Reset the toy initially.
     RotateToToy.reset();      
@@ -112,9 +114,16 @@ function RotateToToy::createTarget( %this )
 
 //-----------------------------------------------------------------------------
 
-function RotateToToy::setRotateTime( %this, %value )
+function RotateToToy::setRotateSpeed( %this, %value )
 {
-    %this.rotateTime = %value;
+    %this.rotateSpeed = %value;
+}
+
+//-----------------------------------------------------------------------------
+
+function RotateToToy::setTrackMouse( %this, %value )
+{
+    %this.trackMouse = %value;
 }
 
 //-----------------------------------------------------------------------------
@@ -129,7 +138,23 @@ function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
     %angle = -mRadToDeg( mAtan( getWord(%worldPosition,0)-getWord(%origin,0), getWord(%worldPosition,1)-getWord(%origin,1) ) );
     
     //Rotate to the touched angle.
-    RotateToToy.TargetObject.RotateTo( %angle, RotateToToy.rotateTime );
+    RotateToToy.TargetObject.RotateTo( %angle, RotateToToy.rotateSpeed );
+}
+
+//-----------------------------------------------------------------------------
+
+function SandboxWindow::onTouchMoved(%this, %touchID, %worldPosition)
+{
+    // Finish if not tracking the mouse.
+    if ( !RotateToToy.trackMouse )
+        return;
+        
+    // Calculate the angle to the mouse.
+    %origin = RotateToToy.TargetObject.getPosition();
+    %angle = -mRadToDeg( mAtan( getWord(%worldPosition,0)-getWord(%origin,0), getWord(%worldPosition,1)-getWord(%origin,1) ) );
+    
+    //Rotate to the touched angle.
+    RotateToToy.TargetObject.RotateTo( %angle, RotateToToy.rotateSpeed );        
 }
     
 };