Board index Roller Coaster Games NoLimits Coaster 2 NoLimits Coaster 2 Scripting How To Open a Door Script

How To Open a Door Script

Post your Nolimits 2 scripts here! Also any discussion regarding scription for NoLimits Coaster 2 should be placed in this forum

Post January 16th, 2014, 3:54 pm

Posts: 192
Points on hand: 2,211.00 Points
Hello
I was wondering if someone could help me out. I have this door I made and would like for it to open when the train hits a trigger. I am using 3ds max. How would I script this? Do I create an animation? Thanks.

Image

Post January 16th, 2014, 6:56 pm
Metazoanhaddock User avatar
Moderator
Moderator

Posts: 1758
Points on hand: 3,382.00 Points
Bank: 52,337.00 Points
Location: Seattle, Washington
theres a sample door script included in the simulator
Head Moderator
Director, Structural Engineer - PEAK Amusements
Site Contest Judge (Supporting)
Chatroom Admin

Post January 16th, 2014, 8:38 pm

Posts: 192
Points on hand: 2,211.00 Points

Post January 17th, 2014, 6:29 pm

Posts: 1830
Points on hand: 3,960.00 Points
Location: London, United Kingdom

Hey Jaynen i figured this out for you using the sample script included in NL!

Short video
Rarwh

Post January 17th, 2014, 8:33 pm

Posts: 143
Points on hand: 457.00 Points
Location: Indianapolis, USA

Wow! how do that! Which's one you made these door with lights? Blender or 3dsobject?


Lyle

Post January 18th, 2014, 12:37 am

Posts: 79
Points on hand: 1,594.00 Points
Location: Reynoldsburg, ohio, USA
Can you teach us beginners how to do just that with scripting

Post January 18th, 2014, 4:21 am

Posts: 1830
Points on hand: 3,960.00 Points
Location: London, United Kingdom

@Lyle & Jaynen ..

I made the doors in 3D max, consisting of two objects. the left door and the right door. In 3D max you can Name the objects individually, so i named them "DoorLeft" & "DoorRight" and exported them as a 3d object (.3ds) and put it in my park folder.

I then copied the door script from the NL2 objects folder, and put it in my park folder, opened it up and renamed
This line, with my new objects names.

leftDoor = sco.getElementForName("DoorLeft"); rightDoor = sco.getElementForName("DoorRight");


Then in the NL2 editor, in the Scenery section, I opened up the NL2SCO editor and added all the settings necessary. Then in the scripting tab attached the new script.

Added the NL scene file to the park, and it worked :)

As for lights, go to the lights tab and you can attach lights relative to the individual objects.
Rarwh

Post January 18th, 2014, 9:39 am

Posts: 143
Points on hand: 457.00 Points
Location: Indianapolis, USA

Casper,

Thanks, Does 3Ds max have freeware or payware? I didn't remember we talked last time by Jaynen.


Lyle

Post January 18th, 2014, 11:20 am

Posts: 192
Points on hand: 2,211.00 Points
Hey thanks for the post. I got some time and right now I am trying to figure this out. Hopefully I will have something to post soon.

Post January 18th, 2014, 11:33 am

Posts: 143
Points on hand: 457.00 Points
Location: Indianapolis, USA


Post January 18th, 2014, 12:44 pm

Posts: 192
Points on hand: 2,211.00 Points
Okay so I got this script to work and I must say its awesome. It detects where the train is so it wont hit it. You can adjust the angle and speed and trigger distance very easily as well. Also I know I have to work on the lighting.


Post January 18th, 2014, 12:53 pm

Posts: 143
Points on hand: 457.00 Points
Location: Indianapolis, USA

Awesome! I telling you one thing. when train arrive near by Door and Train stop by 50 sec. or less then will doors open and trains to way.. What you think?

Lyle

Post January 18th, 2014, 1:29 pm

Posts: 1830
Points on hand: 3,960.00 Points
Location: London, United Kingdom

Yup! That looks awesome Jaynen,

@lyle, no freeware. You could somehow obtain it.. or download numerous Free 3D programs like blender or sketchup
Rarwh

Post January 18th, 2014, 2:41 pm

Posts: 192
Points on hand: 2,211.00 Points

Post March 30th, 2014, 5:58 pm

Posts: 143
Points on hand: 457.00 Points
Location: Indianapolis, USA


Post April 4th, 2014, 11:29 am

Posts: 1
Points on hand: 98.00 Points
@Casper
I tried doing what you said, but when the doors open, they open around the centre of the object.
So as if in your video they opened around the centre of the door, and not the edges.
Could you upload a video/screenshots of the 3ds object you made.
(I am using sketchup if that could be the problem)

Post August 29th, 2016, 2:40 am

Posts: 48
Points on hand: 1,683.00 Points
Heey all,

Im currently also trying to make a custom door and i would love to script it. I tried the giving method but I can't get it to work. I'm using sketchup and tried making two different objects (leftdoor and rightdoor) and combine them in one object and save as .dae but when I use it with the script it says "element not found: Left" (and Right probably too but it first encountered Left). This also happened when I tried to make the doors two different components in the sketchup model. Does anyone know a way to make the doors in sketchup so they can be used for the script? Would love to hear your answers :D

Thanks from Emperors Productions

Post August 31st, 2016, 12:04 pm
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
If i'm right, you named the door "leftdoor"and "rightdoor". The names should be just "left" and "right". It's actually just what the error says, it can't find "left" because there isn't an object called just"left".

Post September 4th, 2016, 6:05 am

Posts: 48
Points on hand: 1,683.00 Points
Yes thank you I found it. I tried it once with the name "Left" but now I see it's with a lowercase "l" instead. :p

Post September 4th, 2016, 8:43 am

Posts: 168
Points on hand: 3,893.00 Points
Is there also a sliding door script? Or any way to make an object slide any direction on a trigger :P
I'm only annoying on purpose I swear

Post September 4th, 2016, 11:20 am
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
There is no standard sliding door script but if you know a bit of scripting, it's not that hard to change the script so that the doors will slide.

This is the original door script with 6 lines of code changed so that the doors will slide.

import com.nolimitscoaster.*;
import nlvm.math3d.*;

public class DoorScript extends Script implements TrackTriggerListener
{
  private SceneObject sco;
  private SceneObjectElement leftDoor;
  private SceneObjectElement rightDoor;
 
  private static final int STATE_IDLE = 0;
  private static final int STATE_OPENING = 1;
  private static final int STATE_OPENING_REVERSED = 2;
  private static final int STATE_CLOSING = 3;
 
  // rotate up to 90 degrees
  private static final float MAX_ANGLE = (float)(Math.PI / 2.0);
  private static final float SPEED = 1.6f;
 
  // rotate around Y-Axis
  private static final Vector3f AXIS = new Vector3f(0,1,0);
 
  private int state;
  private float rot;
 
  private TrackTrigger beforeDoorTrigger;
  private TrackTrigger behindDoorTrigger;
 
  private StaticSound doorSound;
 
  private static final String scriptName = "DoorScript";
 
  // must be constant string expression and marked as final so that the compiler will no complain with a warning when used for
  // StaticSound.loadFromFile
  private final static String doorSoundFile = "doorsound.ogg";
 
  public bool onInit()
  {
    sco = sim.getSceneObjectForEntityId(getParentEntityId());
    if (sco == null)
    {
      System.err.println(scriptName + ": the script is not part of a scene object");
      return false;
    }
   
    leftDoor = sco.getElementForName("left");
    if (leftDoor == null)
    {
      System.err.println(scriptName + ": element not found: left");
      return false;
    }   

    rightDoor = sco.getElementForName("right");
    if (rightDoor == null)
    {
      System.err.println(scriptName + ": element not found: right");
      return false;
    } 

    doorSound = StaticSound.loadFromFile(doorSoundFile, 0);
    if (doorSound == null)
    {
      System.err.println(scriptName + ": could not load sound '" + doorSoundFile + "'");
      return false;
    }

    Vector3f pos = new Vector3f();

    sco.getTranslation(pos);


    doorSound.setPosition(pos);

    // Setting to 'Local' will enable environmental (reverb) effects if this sound's position should be e.g. in a tunnel
    doorSound.setEnvironmentMode(StaticSound.E_ENVMODE_LOCAL);

    // These parameters affect the distance attenuation
    doorSound.setDistanceParameters(2.0f, 0.8f);

    final float range = 10;

    // create triggers...
    TrackPos trackPos = sim.findNearestCoasterTrack(pos, range);
    if (trackPos == null)
    {
      System.err.println(scriptName + ": no coaster found within a range of "+ range + " meters");
      return false;
    }
    else
    {
      Coaster coaster = trackPos.getCoaster();
     
      System.out.println(scriptName + ": Coaster found: " + coaster);

      System.out.println("Coaster Style is: " + coaster.getCoasterStyleId());
     
      // create a trigger 5 meters in front of the track pos
      beforeDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, -5.0);
     
      // create a trigger 5 meters after the track pos
      behindDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, +5.0);
     
      // tell the triggers that they should call our onTrainEntering/onTrainLeaving methods
      // We have to implement the TrackTriggerListener interface
      beforeDoorTrigger.addTrackTriggerListener(this);
      behindDoorTrigger.addTrackTriggerListener(this);
    }
   
    state = STATE_IDLE;
    rot = 0;
   
    return true;
  }
 
  public void onNextFrame(float fTickTime)
  {
    switch (state)
    {
    case STATE_IDLE:
      // do nothing
      break;
    case STATE_OPENING:
      {
        rot += fTickTime * SPEED;
        if (rot >= MAX_ANGLE)
        {
          rot = MAX_ANGLE;
          state = STATE_IDLE;
        }
        //leftDoor.setRotation(rot, AXIS);
      leftDoor.setTranslation(new Vector3f(-rot, 0, 0));
        //rightDoor.setRotation(-rot, AXIS);
      rightDoor.setTranslation(new Vector3f(rot, 0, 0));
      }
      break;
    case STATE_OPENING_REVERSED:
      {
        rot -= fTickTime * SPEED;
        if (rot <= -MAX_ANGLE)
        {
          rot = -MAX_ANGLE;
          state = STATE_IDLE;
        }
        //leftDoor.setRotation(rot, AXIS);
      leftDoor.setTranslation(new Vector3f(-rot, 0, 0));
        //rightDoor.setRotation(-rot, AXIS);
      rightDoor.setTranslation(new Vector3f(rot, 0, 0));
      }
      break;     
    case STATE_CLOSING:
      {
        if (rot > 0)
        {
          rot -= fTickTime * SPEED;
          if (rot <= 0)
          {
            rot = 0;
            state = STATE_IDLE;
          }
        }
        else if (rot < 0)
        {
          rot += fTickTime * SPEED;
          if (rot >= 0)
          {
            rot = 0;
            state = STATE_IDLE;
          }
        }
        //leftDoor.setRotation(rot, AXIS);
      leftDoor.setTranslation(new Vector3f(-rot, 0, 0));
        //rightDoor.setRotation(-rot, AXIS);
      rightDoor.setTranslation(new Vector3f(rot, 0, 0));
      }
      break;
    }
  }
 
  /**
   * This method is part of the TrackTriggerListener Interface implementation
   *
   * It will be called if the interface is registered for a trigger using the
   * addTrackTriggerListener method.
   */
  public void onTrainEntering(TrackTrigger trigger, Train train)
  {
    if (trigger == beforeDoorTrigger)
    {
      if (train.getSpeed() > 0)
      {
        state = STATE_OPENING;
        doorSound.play();

        //printTrainPositionAndRotation(train);
      }
    }
    else if (trigger == behindDoorTrigger)
    {
      if (train.getSpeed() < 0)
      {
        state = STATE_OPENING_REVERSED;
        doorSound.play();
      }
    }
  }
 
  /**
   * This method is part of the TrackTriggerListener Interface implementation
   *
   * It will be called if the interface is registered for a trigger using the
   * addTrackTriggerListener method.
   */ 
  public void onTrainLeaving(TrackTrigger trigger, Train train)
  {
    if (trigger == beforeDoorTrigger)
    {
      if (train.getSpeed() < 0)
      {
        state = STATE_CLOSING;
        doorSound.play();
      }
    }
    else if (trigger == behindDoorTrigger)
    {
      if (train.getSpeed() > 0)
      {
        state = STATE_CLOSING;
        doorSound.play();
      }
    }
  }

  // This method demonstrates how to get the position and rotation of the first bogey
  private void printTrainPositionAndRotation(Train train)
  {
    if (train.getBogieCount() > 0)
    {
      Vector3f pos = new Vector3f();
      Vector3f front = new Vector3f();
     
      train.getBogieOrientationAndPosition(0, front, null, null, pos);

      System.out.println(scriptName + ": First bogey is at " + pos);

      double azimuthRad = Math.atan2(front.x, -front.z);
      double elevationRad = Math.atan2(front.y, Math.sqrt(front.x* front.x + front.z* front.z));

      System.out.println(scriptName + ": Yaw is " + Math.toDegrees(azimuthRad) + " degrees");
      System.out.println(scriptName + ": Pitch is " + Math.toDegrees(elevationRad) + " degrees");
    }
  }
}


Return to NoLimits Coaster 2 Scripting

 


  • Related topics
    Replies
    Views
    Last post