Constrain Rotations

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Constrain Rotations

Postby LethalRaptorGames » Fri Jun 07, 2019 3:25 am

Hello, I am hoping someone can give me some tips on setting up a constraint so that a body can move about the world on all axis but will not rotate on any axis. So far I think I need the UserJoint (I have used the UpVector but that still allows rotation around the vector). I am creating a character controller if that helps.

My code so far;
Code: Select all
void MyCallBack(const NewtonJoint* joint, float timeStep, int threadIndex)
{
   auto _body = NewtonJointGetBody0(joint);
   float _matrix[16];
   NewtonBodyGetMatrix(_body, _matrix);

        //Not sure what code is need to constrain rotations / point towards an upvector
}

NewtonJoint* CreateCustomJoint(NewtonWorld* newtonWorld, NewtonBody* child, NewtonBody* parent)
{
   return NewtonConstraintCreateUserJoint(newtonWorld, 6, MyCallBack, child, parent);
}
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Julio Jerez » Fri Jun 07, 2019 10:11 am

either the dCustomBallAndSocket or the dCustomSixdof with zero angular limit should do it.

but if you are making a play controller, I suggest you try the one in the SDK

look at BasicPlayerController or AnimatedPlayer they has bee simplifies and do about 80% or more of what a play does in a game, the rest is custom funtionality that is eassy to implement in the play callback.

making play controller rather difficult and getting teh capsule upright is the eassy part, all the tweak and behaviors are much difficult to get under controll.
It is better to elaborate on one that already did all the house keep work.

the play is newton is quite sophisticated.
please try BasicPlayerController and tell me if this is a better start than starting from the beginning.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby Julio Jerez » Fri Jun 07, 2019 8:02 pm

Oh, how did I forget, the easiest way to get a joint that freezes the rotation and move to where you want, us by using the kinematic joint.
But I repeact again, making a player controller requires a lot of heuristic knowledged, is not something that you can write and analitical expression and it will just work.
I recommend you try to get the Basic Player or Animated Player demo going in your system.
Them you can make improvements on that, if you get stuck, I can help you. The controller is in fact quite advanced internally but the interface is uncanny simple, plus in a near future that controller will extended to do full body Inverse Dynamics.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby LethalRaptorGames » Sat Jun 08, 2019 6:01 am

Thanks for the info! So far the UpVector does what I want except for the rotation around the vector. Is there an easy way to stop that rotation? Otherwise I'll look into those examples in the SDK.
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Julio Jerez » Sun Jun 09, 2019 8:19 am

The up vector is a legacy joint from more than 10 years ago when I thought a player controller could be made using a joint.
The problem with using a joint to make a object act as it is was a kinematic body is that joints operates by cancelation a force, but a kinematic body operate buy intantaneus velocity changes, those two thing do not work well together.
You may see the upvector kind of working untill you start controlling it by seeing its velocity and get frustrated because do not responding to what you want because the joint try to preserve momentum and the use try to change momentum, is like mixing oil and water to get a solution.
This is the reason in Newton we created the kinematic body, which respond to velocity changes, not to forces.
I do not know what you want to do, but if you can use the upvector or the kinematic joint, then you can also use the player controller. It is much better to levorage that work, than trying to rehatch a technique that has been proven not to work.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby LethalRaptorGames » Sun Jun 09, 2019 9:38 pm

Okay thanks. I've been going through the BasicPlayerController.cpp to find out how it's all working. I see how the controller is created;

Code: Select all
dCustomPlayerController* const controller = CreateController(location, localAxis, mass, radius, height, height / 3.0f);


I also see this line in ApplyInputs();

Code: Select all
controller->SetForwardSpeed(forwarSpeed);


But where is SetForwardSpeed() defined? I can't find it anywhere.
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby LethalRaptorGames » Mon Jun 10, 2019 1:04 am

Ah, I have found it in the header file. I will keep experimenting with the BasicPlayerController and see if it works for me.
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Julio Jerez » Mon Jun 10, 2019 11:01 am

Basically you make a player controller manager, by subclassing from the dCustomPlayerContrer manager of the dCustomJoint library,
The you can create as many players as you need.
The class has method that will be called by the engine at the proper time for you to move the player.

Notice that the player has it own local cordinate system, you can manipulate that matrix to achive the instantanues rotation or the lack of that you want.
This is what makes joints hard to use for this kind of thing, a joint also has a local matrix but changing it takes time for the joint to bring the two bodies to a legal state because It does that by calculating the force the move the bodies at constant speed so that conservation of momentum is not broken.

The kinematic body can change the local matrix at will and it align the capsule to that new alignment.
Most apps do not change the local matrix, but many apps use arbitrary coordinate system, like z or y up,
Or you can also has a more elaborated systems like flying, swimming, or moving on a curve surface, etc.

I hope this make it clear, because other that start writing equation I do not know how else to make the case for the kinematic body platey in favor of joint based one.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby LethalRaptorGames » Thu Jun 13, 2019 7:10 pm

It does make sense, thanks. I have successfully implemented the basic character controller. I can see how gravity is being applied, I'm wonder though how I can change the gravity direction and have the capsule align to that direction? And the step height still work.
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Julio Jerez » Thu Jun 13, 2019 8:11 pm

That's easy you just rotate the local axis, the is not interface now I will add it tonight and show how to do in the demo.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby LethalRaptorGames » Fri Jun 14, 2019 5:31 am

That'd be great! :D
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Julio Jerez » Fri Jun 14, 2019 9:53 am

if you sync, there is a new pair of functions in teh player controller

dMatrix GetFrame() const { return m_localFrame; }
void SetFrame(const dMatrix& frame);

I add a test the rotate the frame matrix by 60 degrees, you will see the player is tilted.

it should slide because the gravity act alone the gravity direction, so the re soue be a bug.
but for the most part everything works.
Please sync and see if this is what you expected.
I will see what is wrong with the gravity been tilted later.

Edit
Oh I think I figure out why it does not slide.
In the demo for demonstration reasons. The friction callback report 2.
This value is two high for reality but not for game play.

Late I will make the friction callback pass the shape I'd so that an appropriate fiction is report back.

The secund demo use more realistic human size values so a friction of 1.0 should generate the desire behavior.

This is where a player like this start to show the versatility, you can actually constomize to almost anything you need for you project, realistic or cartoonist behavior is all possible.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby Julio Jerez » Fri Jun 14, 2019 3:00 pm

Ok now the friction function pass the contact ID, and I am setting eth friction to 1.0 instead of 2.0 until
I see material to the level mesh.

now you will see that the player will slide because the gravity is tilted 60 degrees and a friction of 1.0 can only hold it to 45 degrees.

you can play with SetFrame function and see the results.

basically when you want is to set material id to the mesh and surfaces with high traction set friction to say 2, or even 3 while surfaces with low traction (Ice, wet) you set to say 0.5 or something like that.

tell me if this works for you.

notice that a realistic friction value of 1, work very well for realistic player, check animated player with a human speed. The basic player is exaggerated because is a stress test. but many game use these values.

I also added the part the read friction coefficients from the contacts, you can play with that to set friction however you like.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Constrain Rotations

Postby LethalRaptorGames » Sat Jun 15, 2019 4:32 pm

Thanks! It all seems to make sense. I will implement it today and see how it goes. Is there anyway to visualize a collision mesh in Newton? Or is this something I need to implement? I'm thinking for debugging purposes.
LethalRaptorGames
 
Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

Re: Constrain Rotations

Postby Dave Gravel » Sat Jun 15, 2019 8:21 pm

You search a nice physics solution, if you can read this message you're at the good place :wink:
OrionX3D Projects & Demos:
https://orionx3d.sytes.net
https://www.facebook.com/dave.gravel1
https://www.youtube.com/user/EvadLevarg/videos
User avatar
Dave Gravel
 
Posts: 800
Joined: Sat Apr 01, 2006 9:31 pm
Location: Quebec in Canada.

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 16 guests

cron