Basic animation in virtual reality


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

Arkwood said, whilst removing the Oculus Rift virtual reality headset and easing his grip of the Touch controllers.

‘The problem with your virtual world is there are no jump scares.’

I looked at him quizzically. He continued.

‘You know. Nothing frightening ever happens.’

I went away wounded. How dare he criticise my VR experience. ‘If he wants scary, I’ll give him fuckin scary. So scary he’ll have the brown sauce running down his legs.’

I made an axe in Blender with the help of tutorial Blender Modeling – Axe.


And I imported the axe into my Microsoft Visual Studio application, which uses the Oculus SDK for Windows and OpenGL graphics library.

I’ll need a bit of basic animation for the axe. Here’s the C++ code that will do the trick.

class AnimationClient
	bool axe_up = false;


	// update z position of axe
	float AxePos(float z) {
		return z += 0.002f;

	// update x rotation of axe
	float AxeRot(float x) {

		if (x > 0.7f) {
			axe_up = true;
		else if (x < 0.0f) {
			axe_up = false;

		if (axe_up) {
			return x -= 0.008f;

		return x += 0.008f;

A simple class that allows the axe to move in a straight line across the room by updating its z position. The axe can also make a chopping motion by updating its x rotation (the axe_up variable tells us whether the axe is chopping downward or upward).

We can call the AxePos method on each render cycle for the meshes of our axe.

Models[2]->Meshes[0]->Pos.z = animationClient->AxePos(Models[2]->Meshes[0]->Pos.z);

And we can call the AxeRot method similarly.

mesh0_rot_x = animationClient->AxeRot(mesh0_rot_x);
Models[2]->Meshes[0]->Rot = OVR::Quatf(Axis_X, mesh0_rot_x);

Splendid. Now our axe can whizz across the virtual room, chopping away at Arkwood.


I told my Belgian chum to put the Rift headset back on and grab the Touch controllers. Here’s the video of how he got on.

He was so impressed that he duly shat himself. Mission accomplished.