[solved]How to change hinge joint rotation speed?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[solved]How to change hinge joint rotation speed?

Postby Neo » Thu May 09, 2013 6:23 am

Currently , CustomHinge is my choice to impelement a motorized turret in my game.
By calling NewtonUserJointAddAngularRow, a turret can rotate itself to the desired angle.

However, I found that the rotation speed is almost the same...So I wonder how can I change the rotation speed
of a CustomHinge? Just to vary the rotation speed of different kinds of turret, some of them rotate very fast, and some slow.
Thx in advance!
Last edited by Neo on Sat May 11, 2013 3:22 pm, edited 1 time in total.
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby Julio Jerez » Thu May 09, 2013 7:10 am

that function is the one to use, you select the angular speed and divide by the time step,

speedAceel = (desided speed - joint anagle speed) / timestep

and that is what you pass to that junction.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to change hinge joint rotation speed?

Postby JoeJ » Thu May 09, 2013 7:19 am

Neo wrote:However, I found that the rotation speed is almost the same...


I assume you set error angle, as you would do an a hard limit? You must set this to zero:

// if the motor capability is on, then set angular acceleration with zero angular correction
NewtonUserJointAddAngularRow (joint, 0.0f, &axis[0]);

// override the angular acceleration for this Jacobian to the desired acceleration
NewtonUserJointSetRowAcceleration (joint, speedAceel );
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to change hinge joint rotation speed?

Postby Neo » Thu May 09, 2013 8:17 am

JoeJ wrote:// override the angular acceleration for this Jacobian to the desired acceleration
NewtonUserJointSetRowAcceleration (joint, speedAceel );

Oops, I didn't know this function before :oops:
I'll try it , reply later :wink:
Thx
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby Neo » Thu May 09, 2013 8:31 am

Errr..another problem: how can I get current joint anagle speed?
It seem that I set the wrong acceleration, turret spins constantly like mad. Then the whole joint behaves weirld..
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby JoeJ » Thu May 09, 2013 10:48 am

I think there is a function in Custon Joint baseclass or hinge that gives 'joint angle speed', take a look there.

You can also look at my code here, i hope it's correct because i needed to eliminate the second dynamic body where i originally use it.




sVec3 tAV (0, 0.3, 0); // the angular velocity you want for the turret - for example constant twist over y axis

sVec3 omega0;
BodyGetAngVel (body0, omega0);
sVec3 rowAV = tAV - omega0; // take current ang vel into account

float len = rowAV.Length();
sVec3 axis = rowAV / len; // the final axis we want to set acceleration around

float relAccel = len * invTimestep;
// note: it may be better to use a fixed axis for a turret like an up vector.
// for that you could do: relAccel = rowAV.Dot(upVector) - try if that's better

NewtonUserJointAddAngularRow (joint, 0.0f, &axis[0]);
NewtonUserJointSetRowAcceleration (joint, relAccel);
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to change hinge joint rotation speed?

Postby Neo » Thu May 09, 2013 12:07 pm

Still not working...
Could you please tell me what API you called under the wrapper function "BodyGetAngVel"?
And how about "invTimestep" stand for? Why does it require a multiply , instead of divide?
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby JoeJ » Thu May 09, 2013 1:23 pm

invTimestep = 1.0 / timestep
BodyGetAngVel() = NewtonBodyGetOmega()

How do you do that?
Anyway it must be done inside the callback of a user joint.
You modify / derive the CustomHinge from the custom joint library? Then you need to add code in the SubmitConstraints() method.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to change hinge joint rotation speed?

Postby Neo » Thu May 09, 2013 1:57 pm

Yeah, these functions are all actually called in SubmitConstraintsCallback.
Alright, it's NewtonBodyGetOmega, I thought it was NewtonHingeGetJointOmega...
So apparently i've called wrong function.
OK, now the joint can rotate at proper speed, not spinning crazy or actting weirld :D
BTW, How can I stop it when turret reach the disired angle? It seems that NewtonHingeCalculateStopAlpha cannot be called in SubmitConstraintsCallback...
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby JoeJ » Thu May 09, 2013 2:48 pm

NewtonHingeGetJointOmega should do some work similar as my code - i've learned all that stuff from joint lib code.
Now that you can set target angvel, you simply need to calculate the angvel to reach a target orientation (angle) at target time.

Very simple example:

currentAngle = 0.5 radians
targetAngle = 2.0
turnAngle = targetAngle - currentAngle

if (turnAngle > 0.01) turnAngle = 0.01; // set some max turning speed for turret

tAV = vector (0, turnAngle, 0);

See if you can get something running with that and then try to integrate the timestep in the equation,
so it runs same speed no matter what timestep you fanally choose.
You know how to calculate the angles, yes?
Next problem you may get is overspin: Turret reaches target orientation at the next timestep,
but it has velocity to do the same motion again -> oszillation.
To avoid that you need to slow down if turnAngle is small.

Then you could use two hinges for vertical motion too.
Somewhere in the forum i posted code to calculate necessary force for a projectile to hit a target.
So you could throw a rock - looks cool, if that fits to your game :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: How to change hinge joint rotation speed?

Postby Neo » Fri May 10, 2013 6:48 am

Joej, I found a way to stop the joint from overspin: just set the accel as the inverse of current omega. It will cause joint stop immediately
without oszillation.

And.. where is your projectile code's post? I'm very intersted in that, maybe it would be useful to implement a super long range cannon :D
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: How to change hinge joint rotation speed?

Postby JoeJ » Fri May 10, 2013 7:17 am

http://newtondynamics.com/forum/viewtopic.php?f=9&t=6870

Later i found that the code may crash if input to some trig function was outside [-1...1], so be sure to clamp values.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron