Hello World
In this first example, we're going to create a very simple setup: one cube falling onto another. The falling cube is going to be a
dynamic entity, meaning it is affected by gravity and collisions, and the one underneath is going to be a static entity, meaning it
will stay in the same place no matter what, but other things will collide with it. Note that this would normally be a level or a room -
we're just using a cube as an example.
The source code:
// start Irrlicht
IrrlichtDevice* device = createDevice(EDT_OPENGL);
// pointers to driver, scenemanager
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// create a camera
ICameraSceneNode* camera = smgr->addCameraSceneNode();
// start iphysics
CPhysics physics;
physics.init();
// create a static cube
SPhysicsCube staticCube;
staticCube.bodyType = EBT_STATIC;
staticCube.size_x = 10.0f;
staticCube.size_y = 10.0f;
staticCube.size_z = 10.0f;
// create the cube node - note that the size matches the sizes in the SPhyiscsCube struct
ISceneNode* staticCubeNode = smgr->addCubeSceneNode(10.0f);
staticCubeNode->setMaterialFlag(EMF_LIGHTING, false);
// set the node in the SPhysicsCube to the newly created scene node
staticCube.node = staticCubeNode;
IPhysicsEntity* staticCubeEntity = physics.addEntity(&staticCube);
staticCubeEntity->setPosition(vector3df(0, 0.0f, 100.0f));
// create a dynamic cube
SPhysicsCube dynamicCube;
dynamicCube.bodyType = EBT_DYNAMIC;
dynamicCube.mass = 10.0f;
dynamicCube.size_x = 10.0f;
dynamicCube.size_y = 10.0f;
dynamicCube.size_z = 10.0f;
// apart from the bodyType being EBT_DYNAMIC, everything else is the same
ISceneNode* dynamicCubeNode = smgr->addCubeSceneNode(10.0f);
dynamicCubeNode->setMaterialFlag(EMF_LIGHTING, false);
dynamicCube.node = dynamicCubeNode;
IPhysicsEntity* dynamicCubeEntity = physics.addEntity(&dynamicCube);
dynamicCubeEntity->setPosition(vector3df(0, 100.0f, 100.0f));
u32 oldTime = 0;
while(device->run())
{
driver->beginScene(true, true, SColor(0,100,100,100));
smgr->drawAll();
driver->endScene();
// worry about accurate timing later
physics.update(10);
}
Explanation
The first few lines will be familiar to anyone who is using Irrlicht: start up the device, store some pointers to save lots of typing
later on. We then start IPhysics by creating a class instance and calling init().
The next section creates a static cube. We create an SPhysicsCube and start to fill in all the fields. We set the x, y, and z values
to 10 to match the size of the scene node we create below, which is just created using Irrlicht's built-in cube primitive. We also set
the body type to a static body, so it won't move no matter what hits it. Note that we don't bother setting the mass, as it won't be used.
The section after that creates a dynamic cube. The method for doing this is identical to creating the static cube, except that we set
the body type to dynamic and fill in the mass field.
The pointers to IPhysicsEntity that are returned to the calls to addEntity() provide a simple interface for setting the position and
rotation of the entities we have created. We set them both 100 units in front of the camera, so we can see them, and also set the
dynamic cube 100 units above the static cube, so it can drop onto it.
|