Elevator with OpenGL

Alistair screamed ‘Stop!’

In my post Climbing stairs with OpenGL I had demonstrated how to extend collision detection to allow for the plodding up and down a flight of stairs.

‘I told you to use an elevator instead!’ he said, the froth of fury collecting at the corners of his mouth.

I opened my Microsoft Visual Studio C++ application (with OpenGL graphics library and the Oculus SDK for Windows) and added an elevator to my virtual world.

As with the stairs, my elevator struct inherits from a collision struct – thus can detect when a player strolls onto the elevator platform. But what extra functionality does the elevator struct provide?

struct ElevatorClient : CollisionClient
    const float MinHeight; 
    const float MaxHeight;
    float CurrentHeight;
    bool IsActive;
    bool IsUpward;
    const float MoveIncrement;

    ElevatorClient(Vector3f min, Vector3f max, CollisionName name, float minHeight, float maxHeight) :
        CollisionClient(min, max, name),

    float Move() {

        if (!IsActive) {
            IsActive = true;
            IsUpward = !IsUpward;

        if (IsUpward) {
            CurrentHeight += MoveIncrement;

            if (CurrentHeight >= MaxHeight) {
                CurrentHeight = MaxHeight;
        else {
            CurrentHeight -= MoveIncrement;

            if (CurrentHeight <= MinHeight) {
                CurrentHeight = MinHeight;

        return CurrentHeight;


The elevator struct provides a min and max height for the elevator, as well as the current height of the elevator. There is a flag to determine if the elevator is active, as well as whether it is currently in an upward direction. There is also a struct member variable to specify the movement increments, thus granting a smooth movement of the elevator.

The elevator struct also has a Move member function to handle its movement. First it checks whether it is currently active and, if not, the elevator direction is flipped. Next it updates its current height with the movement increment appropriate to the direction (taking care to adhere to min and max height restrictions).

Nice. Now all we need do is instantiate our elevator, passing in collision settings and the min and max height.

ElevatorClient * elevatorClient = new ElevatorClient(Vector3f(-4.5f, 3.0f, 20.5f), Vector3f(-3.5f, 11.0f, 21.5f), CollisionObject, 4.0f, 8.0f);

And on each render of the virtual world, the collision detection checks whether the player has strolled onto the elevator platform. If so, the elevator calls its Move member function, passing back its current height for updating the player and platform.

Vector3f ElevatorDetection(Vector3f Pos)
    for (int i = 0; i < numMyElevators; ++i) {
        if (MyElevators[i]->Detect(Pos)) {
            Pos.y = MyElevators[i]->Move();

            for (int i = 0; i < numMyModels; ++i) {
                if (MyModels[i]->MyMeshes[0]->MeshName == "Elevator") {
                    MyModels[i]->MyMeshes[0]->Pos.y = Pos.y;

        } else {
            MyElevators[i]->IsActive = false;

    return Pos;

Alistair put on the Oculus Rift virtual reality headset and walked up the stairs to the middle floor, and over to the elevator platform. He strolled onto the platform and the elevator collision detection kicked in.

The platform and his eyeline rose in perfect harmony, as the elevator took him smoothly up from the middle floor to the top floor of the building.

Once he had strolled off the platform onto the top floor the elevator became inactive, waiting for its next use. He strolled back onto the platform and the elevator took him back down from the top floor to the middle floor.

Here’s a video of Alistair using the elevator:

It works a treat.

At the moment, the elevator moves between two floors. Shouldn’t take too much work to get it moving sweetly between a series of floors.