IPhysics - Newton/Irrlicht framework
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact:
Hi. How is your work going ? I must say IPhysics is the only irrlicht based physics engine I managed to run in my linux project. Although I have a problem with the 1.2 (RP edit) release: I have a terrain
Now dropping a test sphere makes the sphere roll on the terrain (what is fine) but after a few seconds it begins to fall down through the terrain.
Code: Select all
#include <iostream.h>
#include <IPhysics.h>
#include <Irrlicht.h>
#include <Newton.h>
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "Newton.lib")
#pragma comment(lib, "IPhysics.lib")
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
CPhysicsCamera* camEntity = new CPhysicsCamera;
CPhysics* physics;
ICameraSceneNode* cam;
int ground;
int rubber;
int metal;
stringw solver;
class CEventReceiver_eg2 : public IEventReceiver
{
public:
CEventReceiver_eg2(ISceneManager* sceneManager, ICameraSceneNode* camera)
{
m_sceneManager = sceneManager;
}
~CEventReceiver_eg2()
{
}
bool OnEvent(SEvent event)
{
if (event.EventType == EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_0:
physics->setSolverModel(0);
solver = "Exact";
case KEY_KEY_1:
physics->setSolverModel(1);
solver = "Adaptive";
case KEY_KEY_2:
physics->setSolverModel(2);
solver = "Linear";
return true;
}
}
if(event.EventType == EET_MOUSE_INPUT_EVENT)
{
if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
vector3df camvec = (cam->getTarget() - camEntity->getPosition()).normalize() * 500;
// drop test cube from camera's position
physics->dropTestCube(m_sceneManager, camEntity->getPosition(), 2.0f, 25.0f);
}
else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
{
// drop test sphere from camera's position
physics->dropTestSphere(m_sceneManager, camEntity->getPosition(), 2.0f, 5.0f);
}
}
camEntity->onEvent(event);
return false;
}
protected:
ISceneManager* m_sceneManager;
ICameraSceneNode* m_camera;
};
int main()
{
// start Irrlicht
IrrlichtDevice* device = createDevice(EDT_OPENGL);
// pointers to driver, scenemanager
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// create an FPS camera
cam = smgr->addCameraSceneNode();
// start iphysics
physics = new CPhysics;
physics->init(device);
physics->setUpdateMode(UM_EVERY_FRAME);
// create our custom event receiver, set it as the active one
CEventReceiver_eg2 receiver(smgr, cam);
device->setEventReceiver(&receiver);
// get pointer to material manager
CMaterialManager* mm = physics->getMaterialManager();
// create materials
ground = mm->createMaterial();
rubber = mm->createMaterial();
metal = mm->createMaterial();
mm->setFriction(ground,ground,0.8f, 0.4f);
mm->setElasticity(ground,ground,0.3f);
mm->setSoftness(ground,ground,0.05f);
mm->setFriction(ground,rubber,0.5f,0.2f);
mm->setElasticity(ground,rubber,0.6f);
mm->setSoftness(ground,rubber,0.15f);
mm->setFriction(ground,metal,1.0f,0.6f);
mm->setElasticity(ground,metal,0.1f);
mm->setSoftness(ground, metal,0.01f);
IAnimatedMesh* levelmesh = smgr->getMesh("teren.3DS");
IAnimatedMeshSceneNode* levelnode = smgr->addAnimatedMeshSceneNode(levelmesh);
levelnode->setMaterialFlag(EMF_LIGHTING, false);
levelnode->setMaterialTexture(0,driver->getTexture("terrain-texture.jpg"));
SPhysicsStaticMesh level;
level.mesh = levelmesh;
level.meshnode = levelnode;
level.meshScale = vector3df(1.1f, 1.1f, 1.1f);
level.meshnode->setScale(level.meshScale);
IPhysicsEntity* levelEntity = physics->addEntity(&level);
levelEntity->setPosition(vector3df(0,0,0)*level.meshScale);
levelEntity->setMaterial(ground);
SPhysicsCamera* camera = new SPhysicsCamera;
camera->cam = cam;
camera->bodySize = vector3df(3.5f,3.5f,3.5f);
camera->mass = 20.0f;
camera->moveSpeed = 15.0f;
camera->sensitivity = 100.0f;
camEntity = physics->addEntity(camera);
camEntity->setPosition(vector3df(3.0f,30.0f, 3.0f));;
//physics->debug(true);
stringw caption;
while(device->run())
{
driver->beginScene(true, true, SColor(0,100,100,100));
caption = driver->getFPS();
caption += " ";
caption += solver;
device->setWindowCaption(caption.c_str());
smgr->drawAll();
driver->endScene();
physics->update();
}
return 0;
}
-
- Posts: 279
- Joined: Fri Dec 24, 2004 6:37 pm
Thank you nargil, work is going pretty gud and i hope i can bring out the 2.0 release soon. Ya about your problem this has been reported by other people too and i am seeing it myself in terrain example! I am trying to go through this tho, so this shud be released by v2.0!
edit: I just got a weird idea and amazingly it worked! Just increase the terrain size from
to
and increase the cube and sphere size from
and
to
and
That means just blow up the scale of all the objects to some pretty big numbers and youll see that the objects dont go through the floor! Its a problem in newton, maybe its rounding up the values because they are too small and hence causing penetration, not sure why it wud be solved after this!
edit: I just got a weird idea and amazingly it worked! Just increase the terrain size from
Code: Select all
vector3df terrainScale(10.0f, 1.0f, 10.0f);
Code: Select all
vector3df terrainScale(20.0f, 2.0f, 20.0f);
Code: Select all
// drop test cube from camera's position
physics->dropTestCube(m_sceneManager, camEntity->getPosition(), 2.0f, 25.0f);
Code: Select all
// drop test sphere from camera's position
physics->dropTestSphere(m_sceneManager, camEntity->getPosition(), 2.0f, 5.0f);
Code: Select all
// drop test cube from camera's position
physics->dropTestCube(m_sceneManager, camEntity->getPosition(), 20.0f, 25.0f);
Code: Select all
// drop test sphere from camera's position
physics->dropTestSphere(m_sceneManager, camEntity->getPosition(), 20.0f, 5.0f);
-
- Posts: 279
- Joined: Fri Dec 24, 2004 6:37 pm
You do set the update mode to:
rit?
This could be the solution to the jerky cam issue! BTW Nick_Japan did not implement one major constant, which makes the size and position you set of newton objects the same in newtonian terms! So thats why if you set size 1000 in nick_japan version it will be like multiplied by 0.3somethig constant hence decreasing the size! Hope you get the point!
Code: Select all
physics->setUpdateMode(UM_EVERY_FRAME);
This could be the solution to the jerky cam issue! BTW Nick_Japan did not implement one major constant, which makes the size and position you set of newton objects the same in newtonian terms! So thats why if you set size 1000 in nick_japan version it will be like multiplied by 0.3somethig constant hence decreasing the size! Hope you get the point!
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact:
-
- Posts: 277
- Joined: Thu Dec 15, 2005 6:11 pm
Maybe I'm mistaken here, but why would you want to set the position yourself. The point of a physics library is so that it sets the positions for you. You should apply a force to the object/node upon keypresses, but not set the position yourself. If I'm mistaken though, feel free to let me know anybody.monkeycracks wrote:@RP, how would you make a node move when a key is pressed.
By node I mean the node's IPhysics structure thing.
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact:
That's what I was thinking (I've used Newton before, in a totally different engine and language). Only ever set positions upon a rigid body's creation.kburkhart84 wrote:Maybe I'm mistaken here, but why would you want to set the position yourself. The point of a physics library is so that it sets the positions for you. You should apply a force to the object/node upon keypresses, but not set the position yourself. If I'm mistaken though, feel free to let me know anybody.
By the way, Rapchik Programmer, I don't see any functions in the IPhysics doco (I know it's outdated) for manually applying forces to objects. Is that in the new version?
-
- Posts: 279
- Joined: Fri Dec 24, 2004 6:37 pm
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact:
No problem, I'm actually working on a game that uses it.RapchikProgrammer wrote:Forces have been implemented in the newer version, it will be there in the next release but currently you cant work with forces, sorry!
@monkeycracks: Hey thanks, it feels gr8 to know that the amount of time i spent on iphysics is never gonna go down the drain!
However, this code :
Code: Select all
levelMesh[1] = smgr->getMesh("media/level one fps.x");
//levelMesh[2] = smgr->getMesh("media/level two fps.x");
levelNode[1] = smgr->addAnimatedMeshSceneNode(levelMesh[1]);
//levelNode[2] = smgr->addAnimatedMeshSceneNode(levelMesh[2]);
levelNode[1]->setMaterialFlag(EMF_LIGHTING, false);
//levelNode[2]->setMaterialFlag(EMF_LIGHTING, false);
level1.mesh = levelMesh[1];
level1.meshnode = levelNode[1];
level1.meshScale = vector3df(1.0f, 1.0f, 1.0f);
level1.meshnode->setScale(level1.meshScale);
levelEntity1 = physics->addEntity(&level1);
levelEntity1->setPosition(vector3df(0,0,0)*level1.meshScale);
cam = smgr->addCameraSceneNodeFPS();
camera = new SPhysicsCamera;
camera->cam = cam;
camera->bodySize = vector3df(3.5f,3.5f,3.5f);
camera->mass = 10.0f;
camera->moveSpeed = 3.0f;
camera->sensitivity = 100.0f;
camEntity = physics->addEntity(camera);
camEntity->setPosition(vector3df(10,85,10));
camEntity->setGround(levelEntity1->getBody());
I have narrowed it down to be in here somewhere :
Code: Select all
cam = smgr->addCameraSceneNodeFPS();
camera = new SPhysicsCamera;
camera->cam = cam;
camera->bodySize = vector3df(3.5f,3.5f,3.5f);
camera->mass = 10.0f;
camera->moveSpeed = 3.0f;
camera->sensitivity = 100.0f;
camEntity = physics->addEntity(camera);
camEntity->setPosition(vector3df(10,85,10));
camEntity->setGround(levelEntity1->getBody());
Here are all the pointers just in case you need them :
Code: Select all
IAnimatedMeshSceneNode* levelNode[2];
IAnimatedMesh* levelMesh[2];
SPhysicsStaticMesh level1;
IPhysicsEntity* levelEntity1;
CPhysicsCamera* camEntity;
SPhysicsCamera* camera;
ICameraSceneNode* cam;
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact: