Problem with NewtonBodyAddForce

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Mancuso Raffaele
Posts: 70
Joined: Sat Dec 17, 2005 4:43 pm
Location: licata (AG) italy
Contact:

Problem with NewtonBodyAddForce

Post by Mancuso Raffaele »

hi all. I'm building an OOP framework to integrate newton with irrlicht. When i add a force before the loop, it's all ok. But when i add a force in the loop or in a IEventReceiver derived class, the force is not added. i suspect that Newton call NewtonApllyForceAndTorqueEvent function one time. Why? here are a part of my code:

Code: Select all

class SceneNode {
public:
//[...] costructor,destructor and others function 
void AddForce(irr::core::vector3df force);
//attributes
raf::Newt::World* world;
NewtonCollision* collision;
NewtonBody* body;
irr::scene::ISceneNode* node;
std::vector<irr::core::vector3df> forces;
static void __cdecl PhisicsSetForceAndTorqueEvent (const NewtonBody* body);
static void __cdecl PhisicsSetTransformEvent(const NewtonBody* body, const float* matrix);
};//class ARES_API SceneNode {
//----------------------------------------Phisics.cpp file--------------------
void __cdecl raf::Newt::SceneNode::PhisicsSetTransformEvent(const NewtonBody* body, const float* matrix) {
	NewtonBodySetMatrix(body,matrix);
	core::matrix4 irr_mat;
	memcpy(irr_mat.M, matrix, sizeof(float)*16);
	irr_mat.setTranslation(irr_mat.getTranslation()*NewtonToIrr);
	raf::Newt::SceneNode* p_node=(raf::Newt::SceneNode*)NewtonBodyGetUserData(body);
	scene::ISceneNode* node=p_node->node;
	node->setPosition(irr_mat.getTranslation());
	node->setRotation(irr_mat.getRotationDegrees());	
}

void __cdecl raf::Newt::SceneNode::PhisicsSetForceAndTorqueEvent (const NewtonBody* body) 
{
raf::Newt::SceneNode* p_node=(raf::Newt::SceneNode*)NewtonBodyGetUserData(body);
for(int i=0;i<p_node->forces.size();i++) {
     raf::Newt::dVector f;
     f.m_x=p_node->forces[i].X;
     f.m_y=p_node->forces[i].Y;
     f.m_z=p_node->forces[i].Z;
     NewtonBodyAddForce(body,&f.m_x);
}
return;	
}
void raf::Newt::SceneNode::AddForce(irr::core::vector3df force) {
	this->forces.push_back(force);
}
//----------------------------a part of main.cpp---------------------------
class MyeventReceiver_class:public IEventReceiver{
public:
bool OnEvent(SEvent event) {
  //camera->OnEvent(event);
  if(event.EventType==EET_KEY_INPUT_EVENT&&
  event.KeyInput.Key==KEY_KEY_S&&
  event.KeyInput.PressedDown==false) {
       p_node->AddForce(core::vector3df(0,-100,-100));
}
return false;
}
};
[...]
device->setEventReceiver(&MyEventClass);
while(device->run()) {
 nWorld->Update(device);//NewtonUpdate(this->world, this->timer.GetElapsedSeconds());
driver->beginScene(true,true,video::SColor(255,0,0,0));
smgr->drawAll();
driver->endScene();	
} 
When i press s key, not happens!! Why?
I can public all of my code.
Thanks, Raffaele ( Ares FPS)
magicat
Posts: 19
Joined: Sun Apr 09, 2006 6:52 am

Post by magicat »

Its been a while since I've used Newton, but it could be like you said, it might only allow you to call AddForce once per AddForceAndTorqueEvent. I think you would be better off adding up all your forces(just add the vectors) into a total force vector in your loop, then calling NewtonBodySetForce() with the total force. Also, it looks like to me in your code you're only applying the x direction force ? Anyways, adding up the forces then using SetForce() would be the cleanest way to do it from what I remember of Newton. Something to remember with your system you've got going is that you're gonna need a way to remove forces from your force vector, so you might wanna take that into consideration.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

The add force function only takes effect from the callback function.....read the manual
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Mancuso Raffaele
Posts: 70
Joined: Sat Dec 17, 2005 4:43 pm
Location: licata (AG) italy
Contact:

Post by Mancuso Raffaele »

I think you would be better off adding up all your forces(just add the vectors) into a total force vector in your loop, then calling NewtonBodySetForce() with the total force
very good idea. Thanks :D
Also, it looks like to me in your code you're only applying the x direction force ?
Why? Take a look in the code

Code: Select all

raf::Newt::dVector f;
f.m_x=p_node->forces[i].X;
f.m_y=p_node->forces[i].Y;
f.m_z=p_node->forces[i].Z;
NewtonBodySetForce(body,&f.m_x);
The add force function only takes effect from the callback function.....read the manual
yes, I read the newton tutorial. in fact i add force with the callback function raf::Newt::SceneNode::PhisicsSetForceAndTorqueEvent. This function read from a vector (std::vector<irr::core::vector3df> forces) all forces applied to a body and applied them sequentially. The problem is that i don't want that a force is applied to a body immediately when the body is created, but when i click a key. in this case the callback function PhisicsSetForceAndTorqueEvent is not called (because it is called one time, when the body is created), so the force is not applied. Maybe i need remove and recreate the body in the same position\rotation and applie the force, but it is not a great metod. Is there a metod to force Newton to call PhisicsSetForceAndTorqueEvent every time NewtonUpdate is called?
thanks,
Raffaele ( Ares FPS )
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, this is not an Irrlicht related question !!!
A Newton forum would help better...

Anyways, try to use the ApplyForceEventHandler(...) callback
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply