[Newton] how apply force?

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
theFALCO
Posts: 6
Joined: Sat Mar 17, 2007 8:41 pm

[Newton] how apply force?

Post 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 );
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post 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
Last edited by JonLT on Wed May 23, 2007 1:37 pm, edited 1 time in total.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

and i must remind that you can add or set force only and only in force and torque callback
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

oh! Yes then you need to figure out a way of doing that.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post 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 :)
theFALCO
Posts: 6
Joined: Sat Mar 17, 2007 8:41 pm

Post 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?
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

it gets smaller, foce can even be reversed (when object bounce of the wall). I hope i understood you correctly :)
theFALCO
Posts: 6
Joined: Sat Mar 17, 2007 8:41 pm

Post by theFALCO »

So how do I calculate how the force should change?
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

Post 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. :)
theFALCO
Posts: 6
Joined: Sat Mar 17, 2007 8:41 pm

Post 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?
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

theFALCO wrote:So how do I calculate how the force should change?
you dont have to. Newton does this itself
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post 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.
Post Reply