Page 1 of 1

[Newton] how apply force?

Posted: Tue May 22, 2007 6:15 pm
by theFALCO
How do I apply force on an object? By this I mean that I have an object and I whant it to get force as if it was hit by another object (but without having to create and simulate the object that hits). Something like Hit( float* posision_of_force, float* force, NewtonBody* target );

Posted: Wed May 23, 2007 12:06 pm
by JonLT
This is what i did:

Code: Select all

addLocalForce(core::vector3df force, core::vector3df position, NewtonBody *body)
{
    float com[3];
    NewtonBodyGetCentreOfMass(body, com);
    core::vector3df r(com[0] - position.X,
                      com[1] - position.Y,
                      com[2] - position.Z);
    core::vector3df torque;
    torque = r.crossProduct(force);
    //add force
    //add torque
}
Then you just have to figure out how you wanna add force and torque. I did this by having a force array and a torque array for every body and then just adding the values to the array, and then applying in the force and torque callback. But you could also just use NewtonBodyAddForce and NewtonBodyAddTorque

Posted: Wed May 23, 2007 1:20 pm
by roxaz
and i must remind that you can add or set force only and only in force and torque callback

Posted: Wed May 23, 2007 1:38 pm
by JonLT
oh! Yes then you need to figure out a way of doing that.

Posted: Wed May 23, 2007 2:28 pm
by roxaz
i can tell way i did it. my dynamic object has float and torque arrays as its members. during runtime i simply set values i to those arrays and callbacks automaticly apply force to body and set those arrays to 0 so we add force just once. callbacks look like this:

Code: Select all

void Player::Player_ForceAndTorqueHandler(const NewtonBody *body) 
{ 
	NewtonBodyAddForce(body, force); 
	NewtonBodyAddTorque(body, torque);

	force[0] = 0.0f;
	force[1] = 0.0f;
	force[2] = 0.0f;

	torque[0] = 0.0f; 
	torque[1] = 0.0f; 
	torque[2] = 0.0f;
}

void _cdecl Player::Player_ForceAndTorque(const NewtonBody *body) 
{ 
	tmpP = (Player*)NewtonBodyGetUserData(body);
	tmpP->Player_ForceAndTorqueHandler(body);
}
as you see in real callback (Player_ForceAndTorque) i get user data from vody and that user data contains pointer to my player class. after that Player_ForceAndTorqueHandler is called and there all fun is done. Why i dont implement handlers functions directly in callbacks? that way i can access all my object members from handler, and from callback i cant because callback is static. i gues you know that static functions can not access non static members of the class :)

Posted: Wed May 23, 2007 5:08 pm
by theFALCO
or...
extend your SObject by adding vector<vector3df> ForceList and apply forces by adding to that list and in the callback add it's elements to the net force, then .clear() the list

thanks, I didn't know how to calculate the torque deepending on the position

one question: Is the force always F? I mean, doesn't it get smaller if you hit, let's say, the edge?

Posted: Wed May 23, 2007 5:22 pm
by roxaz
it gets smaller, foce can even be reversed (when object bounce of the wall). I hope i understood you correctly :)

Posted: Wed May 23, 2007 5:50 pm
by theFALCO
So how do I calculate how the force should change?

Posted: Wed May 23, 2007 6:44 pm
by Ico
I'm not thaaat good at physics (so correct me if I'm wrong) but you have to apply a momentum by basically rotating your object around it's centroid.

It matters how far besides the centroid you place your force (leverage! think of a seesaw).

Or what do you mean by "calculate how the force should change"? Are you talking about it getting smaller and smaller?

http://en.wikipedia.org/wiki/Impulse + Friction

Take a look at wikipedia - there are tons of entries and formulas about physics. :)

Posted: Wed May 23, 2007 7:31 pm
by theFALCO
I mean:
you have a box

1. you hit it in the exact middle - it goes wooooooooo... staight ahead
2. you hit it on the edge - it goes closer and rotates <- how to calculate how closer?

Posted: Wed May 23, 2007 9:12 pm
by roxaz
theFALCO wrote:So how do I calculate how the force should change?
you dont have to. Newton does this itself

Posted: Wed May 23, 2007 9:25 pm
by JonLT
The force acting on a body can be devided into components Fx, Fy, Fz. The net force on the body is the sum of these components and is applied at the center of mass.
If you are thinking of something hitting your body and then making it move you might see that the body moves less if the thing hits the body on the edge. That is simply because the thing slides off the body and therefor not all the force is transfered to the body. But if the thing hits the body right at the center the thing does not slide of and all the force is tranfered.
I think this is how it works.