Basics to control a ball

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Basics to control a ball

Postby KamiGazz » Sun Feb 06, 2011 2:50 am

Hi,
first, thanks for taking the time to read this.

I'm not a pro coder but I've been playing with C++ for a while, and combined that pleasure with Truevision3D for the few past years. I integrated ODE to my projects up until Newton 2.0 was integrated to TV3D's engine, which totally got all the process simplier and left me some more time to study other aspects and go further. I do have some "basic" knowledge of physics, forces, friction, etc... but the vector/matrix/quaternion side of maths still have way too many secrets for me.

I would like a few pointers, or just at least to know if I am in the right direction... Too explain briefly, I'm trying to control a sphere via my analog joystick. So as of now, physics are initialized, sphere body and floor static body are created, gravity is set and materials / interactionfrictions are configured for each body.

After a few tests, I realized that applying torque instead of force would be optimal to move as I'd like to get the slippering effect when switching direction while already moving at a good speed.

So I managed to get the value of the joystick and convert it to a vector to apply a torque:

Code: Select all
vecTorque.x = clInput->stGamePad.Leftstick.X / 100;
vecTorque.y = 0;
vecTorque.z =  clInput->stGamePad.Leftstick.Y / 100;
clShell->SetTorque( vecTorque, true);



Where I struggle next is:

1) The directions are all screwed up! In order to test, I apply to another mesh, as rotations, the same torque vector and the direction of the roration is perfectly corresponding to my joystick position but the torque is going in a different way!

2) I know I have to apply a counter torque to slow my sphere when I don't apply any torque since friction won't make it stop. I tried to get the angular velocity vector, inverse it, normalize it, and apply it back to the sphere as torque to provide a constant slowdown but... I get a constant back n forth movement instead.

Code: Select all
vec = clShell->GetAngularVelocity(true);
vec = vec * -1;
vec = pTVMaths->VNormalize(&vec);
clShell->AddTorque(vec * iSlowFactor, true);


For your information, I cannot use the controller joint nor the userjoint as it is not implemented in the truevision wrapper, that is why I'm trying to manage all this through applying forces / torques.

So... Am I on a good start or totally in the woods? Are my issues more about materials setting and forces fine tuning or on the maths and techniques aspect?
Any help would be really appreciated!
Thx
KamiGazz
 
Posts: 8
Joined: Sun Feb 06, 2011 2:13 am

Re: Basics to control a ball

Postby ledahut » Mon Feb 07, 2011 5:20 am

The directions are all screwed up! In order to test, I apply to another mesh, as rotations, the same torque vector and the direction of the roration is perfectly corresponding to my joystick position but the torque is going in a different way!

I am getting the same error with force.
If I apply force in standard direction x or y or z its ok, but with special vectors (camera direction...) my object won't really go to the right direction.
When I apply velocity with the same vector no problem. So I use Impulse for the moment.
Maybe your problem with torque is the same than mine with force. (I need to check with older newton version theses strange vectors).

I know I have to apply a counter torque to slow my sphere when I don't apply any torque since friction won't make it stop. I tried to get the angular velocity vector, inverse it, normalize it, and apply it back to the sphere as torque to provide a constant slowdown but... I get a constant back n forth movement instead.

To slow down my body I check if no key is pushed (or joystick at origin for you), and call this little algorithme.
If getVelocity > 3 then
setVelocity(getvelocity / 2);

You can do the same with setRotation getRotation.
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France

Re: Basics to control a ball

Postby Julio Jerez » Mon Feb 07, 2011 9:20 am

Code: Select all
vecTorque.x = clInput->stGamePad.Leftstick.X / 100;
vecTorque.y = 0;
vecTorque.z =  clInput->stGamePad.Leftstick.Y / 100;
clShell->SetTorque( vecTorque, true);


basically a toque spins a body along one instatnaneos axis;
you are alling a fix torque along the X and Y axis, I do not knwo what the result of the code you poste will do.
but if it work for you the is fine.

for brakes oll you need to do is to apply a negatuve torque along the dirtion of teh angular velocity vector,
that will reduce the velocity of the ball.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Basics to control a ball

Postby KamiGazz » Tue Feb 08, 2011 2:21 pm

Thanks for your answers.

if I understood correctly, applying a torque to more than one axis at a time is not a good thing. So what would be the solution? If I want my sphere roll on the floor in a diagonal direction, let say Front-Right, my previous technique was AddTorque(5,0,5, true). The solution would be to rotation my sphere 45 degrees on the Y axis and then AddTorque(5,0,0, true) ??

I guess that would work but I'll test it tonight to see if it nice looking or if it looks weird to have the mesh rotating before the movement.

However, this solution confuses me when it comes to the slowdown part.
I was going to use Ledahut suggestion but after hanging aroud the forum, I learned that ajusting directly the velocity was not optimal, force and torque should be used instead but...
When the sphere is in movement, depending on the ground slope, the angular velocity vector would not be on only 1 axis right?
That being said, if I apply a negative torque according to the angular velocity vector, it means that I would not apply torque on only 1 axis.... which was mentionned as being wrong previously!?!?

Is there something I misunderstood?


PS: I hope I'm not to exasperating, I know these are probably basics notions. If it can help, I should explain what I am trying to do.
I want to control a rolling ball with another ball inside. Let's imagine that the materials of the inside ball provoques a lot of friction with the inside of the Outter ball, by rotating the inside ball quickly, it should force the outside ball to go in the same direction, or to slowdown if the inside rotation is the opposite of the outter ball movement direction. I tried a few things and realized if was kinda hard to create a physic body of an empty sphere, so I thought it was easier to manipulation the inside ball mesh without physics and simply calculate the torque to be applied to the outter ball accordingly to the inside ball rotation speed and direction.
KamiGazz
 
Posts: 8
Joined: Sun Feb 06, 2011 2:13 am

Re: Basics to control a ball

Postby ledahut » Wed Feb 09, 2011 4:47 am

I don't understand why Julio said
basically a toque spins a body along one instatnaneos axis;
because in you code sample you just set 'torque' one time, so one axis vector (a resulting vector composition) in this iteration.

ajusting directly the velocity was not optimal, force and torque should be used instead

Yes that right. This is were timestep is used:
Code: Select all
NewtonBodySetForce(body, (MyWantedSpeed-ActualSpeed)/TimeStep);

Remember physic: v(t)=dx/dt (v= speed; dx = distance; dt TimeStep).
NewtonBodySetForce wait for an acceleration:
a(t)=dv/dt²
So dv/dt=(MyWantedSpeed-ActualSpeed).
Setting velocity with that function is the right way.
This can also work with Omega/Torque.
ledahut
 
Posts: 98
Joined: Mon Jun 21, 2010 8:03 am
Location: France


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest