Adding stairs with OpenGL

Alistair poured some wine from the flute into a shallow glass. ‘It’s not a pun. I just prefer a firm escalator. Or an elevator, yes.’

But my virtual world needed a way of getting from the ground floor to the top floor. So I opened the C++ Microsoft Visual Studio application (with OpenGL graphics library and the Oculus SDK for Windows) and put in some stairs.

With the Oculus Rift virtual reality headset on my head, I stood on the ground floor balcony and admired a new set of steps to help me get to the top floor.

I strolled forward, so that the steps were straight in front of me.

Moving forward, I witnessed myself plodding up the steps, one by one.

Until I got to the top of the stairs and found myself on the top floor of my virtual building.

Just as easily, I stepped back down the stairs.

Until I arrived at the ground floor balcony, back amongst the grass.

Alistair cooked up some beans as asked if I was taking the piss.

No way. Here is the C++ code. I am using the same collision detection class currently employed for walls and objects, to detect whether I have collided with one of the steps of the stairs.

struct CollisionClient
    const Vector3f Min;
    const Vector3f Max;
    CollisionName Name;
    bool Collision = false;

    CollisionClient(Vector3f min, Vector3f max, CollisionName name) :

    bool Detect(Vector3f Pos) {

        if ((Pos.x >= Min.x && Pos.x <= Max.x) &&
            (Pos.y >= Min.y && Pos.y <= Max.y) &&
            (Pos.z >= Min.z && Pos.z <= Max.z)) {
            // collision
            Collision = true;
        else {
            // no collision
            Collision = false;

        return Collision;

But rather that using the collision detection class directly, I inherit from it so as to supply a Y coordinate for elevation.

struct ElevationClient : CollisionClient
    float YCoord;

    ElevationClient(Vector3f min, Vector3f max, CollisionName name, float yCoord) :
        CollisionClient (min, max, name),

Now it’s simply a matter of instantiating an elevation class, which in this case contains a Y coordinate of 2.0 for elevation.

ElevationClient * ec = new ElevationClient(Vector3f(2.5f, 0.0f, 20.0f), Vector3f(3.5f, 5.0f, 22.0f), CollisionObject, 2.0);

And if I detect a collision on the step of the stairs – by way of me walking onto it whilst wearing the Rift headset – I simply elevate my virtual body to match the Y coordinate.

for (int i = 0; i < numMyElevations; ++i) {
    if (MyElevations[i]->Detect(Pos)) {
        Pos.y = MyElevations[i]->YCoord;

And that’s it. I can walk up and down the stairs in my virtual world and each step will reposition my eyeline accordingly. It feels like I am naturally plodding up and down a set of steps, going from one floor of a building to another.

Alistair urinated on the stairs. We both watched his excretion trickle down each step, from the top floor to the ground floor.

I asked, ‘Do you have a permit for that?’

‘I am not a tourist,’ he stated defiantly, ‘I live here.’

It’s true. If anything, he is our guide. He would just prefer that we did not creak up and down his spine.