Back to Content

3D Movement with JavaScript


This example shows how to move a 3d object in a Coppercube scene using JavaScript. This is only useful when using the Windows (.exe) or MacOS (.app) target of CopperCube. If you are looking for the WebGL version, take a look here.
To start, do the following:

The scene for this example.

Write the following code into your .js file
// register key events
ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
	if (keyCode == 88)
	{
		// the user pressed the key 'X', move the 'cubeMesh1' scene node a bit up
		var cube = ccbGetSceneNodeFromName("cubeMesh1");	
		var pos3d = ccbGetSceneNodeProperty(cube, "Position");
		
		pos3d.y += 2;
		
		ccbSetSceneNodeProperty(cube, "Position", pos3d);
	}
}
When you now start the application again (Tools -> 'Test As Windows Application (.exe)') the cube will move up every time you press 'X'.

This example works by accessing the JavaScript api. You can manipulate all aspects of the 3D scene with this API: textures, materials, positions, objects etc. For a full reference to all functions, see the JavaScript Scripting reference.