## Constrain Rotations

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

### Constrain Rotations

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

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

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

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

That'd be great!
LethalRaptorGames

Posts: 7
Joined: Fri Jun 07, 2019 3:13 am

### Re: Constrain Rotations

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

Posts: 11087
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Constrain Rotations

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

You search a nice physics solution, if you can read this message you're at the good place
OrionX3D Projects & Demos: