I am new in irrlicht and at the moment I am implementing a small scen with Newton where a box moves by a scen in x that is simply a floor and some boxes. The code is executed, but the objects go away downwards and it does not control the collision to me between the objects.
Code is the following one:
Code: Select all
#include <irrlicht.h>
#include <newton.h>
#include <iostream>
#include <dvector.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
// Define all the variables we will be needing
NewtonWorld *nWorld;
IAnimatedMeshSceneNode *carro;
IAnimatedMeshSceneNode *mapa;
const float NewtonToIrr= 32.0f;
IrrlichtDevice* device;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;
int go=0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == KEY_ESCAPE && !event.KeyInput.PressedDown )
{
device->closeDevice();
return true;
}
//vector3df v = cube->getPosition();
if (event.KeyInput.Key == KEY_KEY_W && go!=2)
{
//v.Z+=10;
//cube->setPosition(v);
go=1;
return true;
}
if (event.KeyInput.Key == KEY_KEY_S && go!=1)
{
//v.Z-=10;
//cube->setPosition(v);
go=2;
return true;
}
if (event.KeyInput.Key == KEY_KEY_A && go!=4)
{
//v.X-=10;
//cube->setPosition(v);
go=3;
return true;
}
if (event.KeyInput.Key == KEY_KEY_D && go!=3)
{
//v.X+=10;
//cube->setPosition(v);
go=4;
return true;
}
}
return false;
}
};
void moviendo(ISceneNode* cube)
{
vector3df v = cube->getPosition();
if (go == 1 )
{
v.Z+=1;
cube->setPosition(v);
}
if (go == 2)
{
v.Z-=1;
cube->setPosition(v);
}
if (go == 3)
{
v.X-=1;
cube->setPosition(v);
}
if (go == 4)
{
v.X+=1;
cube->setPosition(v);
}
}
static void setForceAndTorque(const NewtonBody* body)
{
float mass;
float Ixx;
float Iyy;
float Izz;
NewtonBodyGetMassMatrix(body,&mass, &Ixx, &Iyy, &Izz);
core::vector3df force(0,-mass*9.8,0.0);
core::vector3df torque(0,0,0);
NewtonBodyAddForce(body, &force.X);
NewtonBodyAddTorque(body, &torque.X);
}
static void setTransform(const NewtonBody* body, const float* matrix)
{
//get objeto
ISceneNode* node=(ISceneNode*)NewtonBodyGetUserData(body);
// copiar la matriz a una matriz de Irrlicht
core::matrix4 mat;
memcpy(mat.M, matrix, sizeof(float)*16);
//set transformacion
node->setPosition(mat.getTranslation()* NewtonToIrr);
}
int main(){
MyEventReceiver receiver;
// OpenGL device.
device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32, false, false, false, &receiver); device->setWindowCaption(L"IrrNewton Template");
// Video driver device.
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
//Add a Newton World.
nWorld = NewtonCreate(NULL, NULL);
// Set up default material properties for newton
int i = NewtonMaterialGetDefaultGroupID(nWorld);
NewtonMaterialSetDefaultFriction (nWorld, i, i, 0.8f, 0.4f);
NewtonMaterialSetDefaultElasticity (nWorld, i, i, 0.3f);
NewtonMaterialSetDefaultSoftness (nWorld, i, i, 0.05f);
NewtonMaterialSetCollisionCallback (nWorld, i, i, NULL, NULL, NULL, NULL);
// Add an FPS camera to the scene.
ICameraSceneNode *nCamera = smgr->addCameraSceneNodeFPS();
// Add a light so it'll be easier to distinguish the angles of the test map.
smgr->addLightSceneNode(0, vector3df(0.0f, 100.0f, 100.0f));
// add skybox
smgr->addSkyBoxSceneNode(driver->getTexture("irrlicht2_up.bmp"),driver->getTexture("irrlicht2_dn.bmp"),driver->getTexture("irrlicht2_lf.bmp"),driver->getTexture("irrlicht2_rt.bmp"),driver->getTexture("irrlicht2_ft.bmp"),driver->getTexture("irrlicht2_bk.bmp"));
// Load the world into Irrlicht.
mapa = smgr->addAnimatedMeshSceneNode(smgr->getMesh("mapa.x"));
mapa->setMaterialFlag(EMF_LIGHTING,false);
// Load the box into Irrlicht.
carro = smgr->addAnimatedMeshSceneNode(smgr->getMesh("car.x"));
carro->setMaterialFlag(EMF_LIGHTING,false);
////////////////////////////////////////////
// DEFINICIONES DE NEWTON
////////////////////////////////////////////
// Define Newton Collision
NewtonCollision* myShape= NewtonCreateBox(nWorld, 10,10,10,NULL);
// Crea Newton Body
NewtonBody* myBody= NewtonCreateBody(nWorld, myShape);
// Set Propiedades de Body
NewtonBodySetMassMatrix(myBody, 1.0f, 1.0f, 1.0f, 1.0f);
NewtonBodySetForceAndTorqueCallback(myBody,setForceAndTorque);
// Integracion con Irrlicht Objeto
NewtonBodySetUserData(myBody, (void*)carro);
// Transformacion Callback
NewtonBodySetTransformCallback(myBody,setTransform);
//////////////// OTRO OBJETO ///////////////////
// Define Newton Collision
NewtonCollision* myShape2= NewtonCreateBox(nWorld, 10,10,10,NULL);
// Crea Newton Body
NewtonBody* myBody2= NewtonCreateBody(nWorld, myShape);
// Set Propiedades de Body
NewtonBodySetMassMatrix(myBody2, 1.0f, 1.0f, 1.0f, 1.0f);
NewtonBodySetForceAndTorqueCallback(myBody2,setForceAndTorque);
// Integracion con Irrlicht Objeto
NewtonBodySetUserData(myBody2, (void*)mapa);
// Transformacion Callback
NewtonBodySetTransformCallback(myBody2,setTransform);
////////////////////////////////////////////////////////////
// Start the game loop, in here is where all code will be.
while(device->run()){
driver->beginScene(true, true, SColor(0,200,200,200));
moviendo(carro);
// Update the Newton world every so often. If you disable this, nothing will happen with the physics, this is great for pausing games.
NewtonUpdate(nWorld, 0.01);
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
NewtonDestroy(nWorld);
device->drop();
return 0;
}
thanks for the aid.
Note: the translation to ingles I did it by means of Babel Fish ;)