Code: Select all
#include <Irrlicht.h>
#include <Newton.h>
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "Newton.lib")
using namespace irr;
using namespace video;
using namespace core;
using namespace scene;
static NewtonWorld* nWorld;
static NewtonCollision *collision;
static NewtonBody* body1;
static NewtonBody* body2;
static IVideoDriver* driver;
static IrrlichtDevice* device;
static ISceneManager* smgr;
static ISceneNode* sphereNode;
static ISceneNode* cubeNode;
static ICameraSceneNode* cam;
unsigned int clock;
static void PhysicsApplyForceAndTorque (const NewtonBody* body);
static void PhysicsBodyDestructor (const NewtonBody* body);
static void IntitScene()
{
device = createDevice(EDT_DIRECT3D9,dimension2d<s32>(640,480),32,false,false,false);
driver = device->getVideoDriver();
smgr = device->getSceneManager();
// simple camera
cam = smgr->addCameraSceneNode(0,vector3df(0,0,-50),vector3df(0,0,0),-1);
// physics begins
nWorld = NewtonCreate (NULL,NULL);
// create sphere
collision = NewtonCreateSphere (nWorld, 5, 5, 5, NULL);
sphereNode = smgr->addSphereSceneNode(5);
sphereNode->setMaterialTexture(0,driver->getTexture("../data/box.jpg"));
sphereNode->setMaterialFlag(EMF_LIGHTING,false);
body1 = NewtonCreateBody(nWorld,collision);
NewtonBodySetUserData (body1,sphereNode);
NewtonBodySetForceAndTorqueCallback (body1, PhysicsApplyForceAndTorque);
NewtonBodySetMassMatrix (body1, 1.0f, 1.0f, 1.0f, 1.0f);
matrix4 mat;
mat.setTranslation(vector3df(0,15,0));
NewtonBodySetMatrix(body1, mat.pointer());
NewtonReleaseCollision (nWorld, collision);
//create box
collision = NewtonCreateBox(nWorld, 5, 5, 5, NULL);
cubeNode = smgr->addCubeSceneNode(10);
cubeNode->setMaterialTexture(0,driver->getTexture("../data/crate.jpg"));
cubeNode->setMaterialFlag(EMF_LIGHTING,false);
body2 = NewtonCreateBody(nWorld,collision);
NewtonBodySetUserData (body2,cubeNode);
NewtonBodySetForceAndTorqueCallback (body2, PhysicsApplyForceAndTorque);
NewtonBodySetMassMatrix (body2, 1.0f, 1.0f, 1.0f, 1.0f);
matrix4 mat1;
mat1.setTranslation(vector3df(0,30,20));
NewtonBodySetMatrix(body2, mat1.pointer());
NewtonReleaseCollision (nWorld, collision);
}
void PhysicsApplyForceAndTorque (const NewtonBody* body)
{
float mass;
vector3df force (0.0f, -mass * 9.8f , 0.0f);
vector3df torque (0.0f, 0.0f, 0.0f);
NewtonBodySetForce(body, &force.X);
}
void DrawScene()
{
if (device->getTimer()->getTime() > clock + 10)
{
clock = device->getTimer()->getTime();
NewtonUpdate(nWorld, 0.01f);
}
float matrix[4][4];
NewtonBodyGetMatrix(body1, &matrix[0][0]);
matrix4 mat;
memcpy(mat.pointer(), matrix, sizeof(float)*16);
sphereNode->setPosition(mat.getTranslation());
sphereNode->setRotation(mat.getRotationDegrees());
}
int main()
{
IntitScene();
while(device->run())
{
DrawScene();
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
driver->endScene();
}
NewtonDestroy(nWorld);
device->drop();
return 0;
}