Advertisement
Guest User

Untitled

a guest
Sep 7th, 2014
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import com.nolimitscoaster.*;
  2. import nlvm.math3d.*;
  3.  
  4. /* **************************************************************************
  5. This is a generic object attachment script.
  6.  
  7. Directions to use this script.
  8. 1) Copy and paste attach_object.nlvm to the same directory as your .nl2sco file.
  9. 2) Open the NL2SCO Editor and go to the Scripts tab.
  10. 3) Select attach_object.nlvm from the Script Class dialog.
  11. 4) Save the file and Refresh the scene object.
  12.  
  13. View the in-game Help for creating .nl2sco files for your 3D models.
  14.  
  15. It is unlikely that your object will be positioned exactly where you want it at first.
  16. You can modify the positional offset off the 3D model by adding or subtracting
  17. from xOffset, yOffset, zOffset below.
  18. *************************************************************************** */
  19.  
  20.  
  21. public class attach_object extends Script
  22. {
  23. // ====== CUSTOMIZABLE VARIABLES =======
  24. // Note: Decimal point "float" values must be followed by an 'f' (e.g. 0.5f, -9.205f). Whole values do not need the 'f'.
  25. private static final float xOffset = 0; // left/right
  26. private static final float yOffset = 1.5f; //up/down
  27. private static final float zOffset = 0; //back/forth
  28. private static final float range = 15; // The first train within this range of the scene object will receive the attachment. (range is in meters)
  29. private static final int carToAttach = 0; // Defines the car on the train that will receive the attachment. (0 = lead car or zero car)
  30. // ======================================
  31.  
  32. private SceneObject sco; // Scenery object handle.
  33. private Train train; // Train handle.
  34. private Vector3f posOut = new Vector3f(0,0,0); // Will store the scenery object's position based on the car's position.
  35. private Vector3f pitchHeadBankOut = new Vector3f(0,0,0); // Will store the scenery object's orientation based on the car's orientation.
  36. private Matrix4x4f carMatrix = new Matrix4x4f(); // Will store the car's orientation.
  37.  
  38. //**************************************************************************************
  39. // onInit is called when the scene object is loaded. Use this for initialization.
  40. // onInit should return true if initialization succeeds, else false.
  41. // If onInit returns false then the script will be disabled automatically and onNextFrame will never be called.
  42. //**************************************************************************************
  43. public bool onInit()
  44. {
  45. // ********************************************
  46. // Get the scene object handle.
  47. // ********************************************
  48. sco = sim.getSceneObjectForEntityId(getParentEntityId());
  49. if (sco == null)
  50. {
  51. System.err.println("attach_object.nlvm: This script must be assigned to a scene object.");
  52. return false;
  53. }
  54.  
  55. // ********************************************
  56. // Determine if there is a track within range of the scenery object.
  57. // ********************************************
  58. TrackPos trackPos = sim.findNearestCoasterTrack(sco.getTranslation(), range);
  59. if (trackPos == null)
  60. {
  61. System.err.println("attach_object.nlvm: No track found within range of " + range + " meters.");
  62. return false;
  63. }
  64.  
  65. // ********************************************
  66. // Determine if there is a train within range of the scenery object.
  67. // ********************************************
  68. Coaster coaster = trackPos.getCoaster();
  69. train = coaster.findNearestTrain(sco.getTranslation(), range);
  70. if (train == null)
  71. {
  72. System.err.println("attach_object.nlvm: No train found within range of " + range + " meters.");
  73. return false;
  74. }
  75.  
  76. return true;
  77. }
  78.  
  79. public void onNextFrame(float tick)
  80. {
  81. // ********************************************
  82. // Obtain the orientation matrix of the last car
  83. // ********************************************
  84. train.getCarMatrix(carToAttach, carMatrix);
  85.  
  86. // ********************************************
  87. // Convert the matrix obtained from getCarMatrix()
  88. // to the Euler angle vector used in setRotation()
  89. // ********************************************
  90. Tools.matrixToPitchHeadBankPos(carMatrix, pitchHeadBankOut, posOut);
  91.  
  92. // ********************************************
  93. // Calculate XYZ offset
  94. // ********************************************
  95. Matrix4x4f matrix = new Matrix4x4f();
  96. matrix.initTrans(xOffset, yOffset, zOffset);
  97. carMatrix.multRight(matrix);
  98.  
  99. // ********************************************
  100. // Update the scene object's rotation and
  101. // position for this frame.
  102. // ********************************************
  103. Tools.matrixToPitchHeadBankPos(carMatrix, pitchHeadBankOut, posOut);
  104. sco.setRotation(pitchHeadBankOut);
  105. sco.setTranslation(posOut);
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement