Author Topic: Rotating Velocity (Push)  (Read 823 times)

Whenever my train hits a corner, the velocity is set to "0 0 0". This is because if I don't set it to that, the train will be pushed off the corner track by the velocity it had before the change.

What I was wondering is whether there is any way to rotate the velocity so it matches the new facing of the train.

The train turns in 45 degree, 22.5 and 90 degree increments via this function:
Quote
function TrainTurn(%v,%m,%this) {
   if(!isObject(%v) && !%v.isTrain == 1 && %this != %v.lastrotbrick)
      return;
   
    %v.lastrotbrick = %this;
    
   %eulerRot = axisToEuler(getWords(%v.getTransform(),3,6));
   %deg = getWord(%eulerRot,2);
    %pos = %this.getPosition();
    
    %deg += %m;
    
    %new = eulerToAxis("0 0 " @ %deg);
   %v.setTransform(%pos SPC %new);
    %v.setVelocity("0 0 0");
}
Example use in code:
Quote
TrainTurn($MaiTrain,45,$MaiTrain.curtrack);
« Last Edit: March 16, 2013, 10:35:28 AM by chrisbot6 »

It's simple when you're just rotating a 2D vector.
In your case there isn't a need to rotate the Z component.

%ox is the original X component, %oy is the original Y component and %theta is the rotation in radians.
%nx is the new X component and %ny is the new Y component.

%cs = mCos( %theta );
%sn = mSin( %theta );

%nx = %ox * %cs - %oy * %sn;
%ny = %ox * %sn + %oy * %cs;


Example of practical usage:

%vel = %obj.getVelocity();

%ox = getWord( %vel, 0 );
%oy = getWord( %vel, 1 );

// ...

%obj.setVelocity( %nx SPC %ny SPC getWord( %vel, 2 ) );

It's simple when you're just rotating a 2D vector.
In your case there isn't a need to rotate the Z component.

%ox is the original X component, %oy is the original Y component and %theta is the rotation in radians.
%nx is the new X component and %ny is the new Y component.

%cs = mCos( %theta );
%sn = mSin( %theta );

%nx = %ox * %cs - %oy * %sn;
%ny = %ox * %sn + %oy * %cs;


Example of practical usage:

%vel = %obj.getVelocity();

%ox = getWord( %vel, 0 );
%oy = getWord( %vel, 1 );

// ...

%obj.setVelocity( %nx SPC %ny SPC getWord( %vel, 2 ) );

Oh, thanks. Trying this, then.

EDIT: This worked, ty.
« Last Edit: March 16, 2013, 03:50:37 PM by chrisbot6 »