Tags

, , , , , , , , , , , , ,

Peters was eating a pie. Nothing unusual there, as it was Christmas day and time to let the drawstrings loose. And he stuffs his gut all year anyhow. But I was particularly fascinated by the way the crumbs exploded on his fat face as he shovelled the meat-filled pastry into his gob.

‘Ah, that reminds me,’ I announced, my finger prodding empty air, ‘I need to do some collision detection.’

It was for my virtual world, which contains a cube and a cone:

oculusrift_collisiondetection_cubeandcone

I can put on my Oculus Rift virtual reality headset and stroll about the virtual world (with a pair of Oculus Touch Controller virtual hands in front of me, ready to grab at virtual objects).

But I do not want to be able to stroll through virtual objects, as that would spoil the immersive experience:

oculusrift_collisiondetection_cube_inside

You see, I have walked right inside the cube! I need to stop this happening. What I need is collision detection.

Now, there are many ways to do collision detection, as highlighted by the Mozilla Developer Network 3D collision detection article. I am going to start simple and use Point versus AABB.

AABB stands for Axis-Aligned Bounding Box. A box shape is perfect for my cube. All I need do is determine whether my position in the virtual world (the Point) is within the cube (the AABB).

Here’s the C++ code. First we initialize a variable to keep track of our previous position in the virtual world. We also define the minimum and maximum X Y and Z positions for the cube’s bounding box.

// previous position
Vector3f PrevPos = { 0.0f, 0.0f, 0.0f };

// AABB for cube
const ovrVector3f CubeMin = { 2.2, 0.0, -2.4 };
const ovrVector3f CubeMax = { 5.7, 3.5,  1.1 };

And on every render cycle, we can check whether we have strolled into the cube.

// use Point versus AABB
if ((Pos.x >= CubeMin.x && Pos.x <= CubeMax.x) &&
    (Pos.y >= CubeMin.y && Pos.y <= CubeMax.y) &&
    (Pos.z >= CubeMin.z && Pos.z <= CubeMax.z)) {
				
	// collision repositioning
	Pos = PrevPos;
} else {
	PrevPos = Pos;
}

Quite simple really.

We check whether our current position puts us inside the cube. If so, we reposition ourselves outside the cube by grabbing our previous position. Otherwise we set our previous position to our current position.

Perfect. Collision detection is in place and we can no longer stroll through the cube:

oculusrift_collisiondetection_cube_outside

I went into the kitchen to tell Peters the good news and caught him balancing precariously on a stool, reaching for a tray of pies on the top shelf.

‘Thank you Peters!’ I announced in a booming voice, wanting to let my Dutch lodger know that the crumbs exploding on his face had inspired my collision detection code.

But all I did was startle him, his rotund frame wobbling off the stool and smacking the tiled kitchen floor. He was out for the count.

Nevertheless, I picked up one of the scattered pies and took a bite out of it. Then I held it aloft.

‘To you!’ I celebrated his twisted limbs that lay in a pool of blood, ‘To you!’

Ciao!

Advertisements